# dynamodb

Creates one or more DynamoDB tables with encryption, point-in-time recovery, optional streams, GSIs, LSIs, and Application Auto Scaling for provisioned capacity tables.

Supports both PAY_PER_REQUEST and PROVISIONED billing modes. Tables in PROVISIONED mode can opt into read/write auto scaling with configurable min/max capacity and target utilization.

## Usage

```hcl
module "dynamodb" {
  source  = "hcassc.jfrog.io/iac-terraform-modules-virtual/database/dynamodb/aws"
  version = "0.1.0"

  tables = {
    orders = {
      name      = "prod-orders"
      hash_key  = "order_id"
      range_key = "created_at"

      attributes = [
        { name = "order_id",   type = "S" },
        { name = "created_at", type = "S" },
        { name = "customer_id", type = "S" },
      ]

      global_secondary_indexes = [
        {
          name            = "CustomerIndex"
          hash_key        = "customer_id"
          projection_type = "ALL"
        },
      ]

      kms_key_arn            = module.kms.key_arn
      point_in_time_recovery = true
      stream_enabled         = true
      stream_view_type       = "NEW_AND_OLD_IMAGES"
    }

    sessions = {
      name          = "prod-sessions"
      billing_mode  = "PROVISIONED"
      read_capacity = 10
      write_capacity = 5
      hash_key      = "session_id"

      attributes = [
        { name = "session_id", type = "S" },
      ]

      ttl_attribute = "expires_at"
      kms_key_arn   = module.kms.key_arn

      autoscaling = {
        read_min_capacity  = 10
        read_max_capacity  = 100
        write_min_capacity = 5
        write_max_capacity = 50
        target_utilization = 70
      }
    }
  }

  tags = {
    Environment = "prod"
    Team        = "platform"
  }
}
```

<!-- BEGIN_TF_DOCS — DO NOT EDIT BELOW THIS LINE -->
## Requirements

| Name | Version |
| ---- | ------- |
| <a name="requirement_terraform"></a> [terraform](#requirement_terraform) | ~> 1.5 |
| <a name="requirement_aws"></a> [aws](#requirement_aws) | ~> 6.50 |

## Providers

| Name | Version |
| ---- | ------- |
| <a name="provider_aws"></a> [aws](#provider_aws) | 6.50.0 |

## Resources

| Name | Type |
| ---- | ---- |
| [aws_appautoscaling_policy.read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_target.read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_dynamodb_table.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |

## Inputs

| Name | Description | Type | Default | Required |
| ---- | ----------- | ---- | ------- | :------: |
| <a name="input_tables"></a> [tables](#input_tables) | Map of DynamoDB table definitions | <pre>map(object({<br/>    name                   = string<br/>    billing_mode           = optional(string, "PAY_PER_REQUEST")<br/>    read_capacity          = optional(number, null)<br/>    write_capacity         = optional(number, null)<br/>    hash_key               = string<br/>    range_key              = optional(string, null)<br/>    kms_key_arn            = optional(string, null)<br/>    point_in_time_recovery = optional(bool, true)<br/>    ttl_attribute          = optional(string, null)<br/>    stream_enabled         = optional(bool, false)<br/>    stream_view_type       = optional(string, "NEW_AND_OLD_IMAGES")<br/>    table_class            = optional(string, "STANDARD")<br/><br/>    attributes = list(object({<br/>      name = string<br/>      type = string<br/>    }))<br/><br/>    global_secondary_indexes = optional(list(object({<br/>      name               = string<br/>      hash_key           = string<br/>      range_key          = optional(string, null)<br/>      projection_type    = optional(string, "ALL")<br/>      non_key_attributes = optional(list(string), [])<br/>      read_capacity      = optional(number, null)<br/>      write_capacity     = optional(number, null)<br/>    })), [])<br/><br/>    local_secondary_indexes = optional(list(object({<br/>      name               = string<br/>      range_key          = string<br/>      projection_type    = optional(string, "ALL")<br/>      non_key_attributes = optional(list(string), [])<br/>    })), [])<br/><br/>    autoscaling = optional(object({<br/>      read_min_capacity  = optional(number, 5)<br/>      read_max_capacity  = optional(number, 20)<br/>      write_min_capacity = optional(number, 5)<br/>      write_max_capacity = optional(number, 20)<br/>      target_utilization = optional(number, 70)<br/>    }), null)<br/>  }))</pre> | `{}` | no |
| <a name="input_tags"></a> [tags](#input_tags) | Resource tags to apply to all resources | `map(string)` | `{}` | no |

## Outputs

| Name | Description |
| ---- | ----------- |
| <a name="output_stream_arns"></a> [stream_arns](#output_stream_arns) | Map of table keys to DynamoDB stream ARNs (null for tables without streams) |
| <a name="output_stream_labels"></a> [stream_labels](#output_stream_labels) | Map of table keys to DynamoDB stream labels (null for tables without streams) |
| <a name="output_table_arns"></a> [table_arns](#output_table_arns) | Map of table keys to DynamoDB table ARNs |
| <a name="output_table_ids"></a> [table_ids](#output_table_ids) | Map of table keys to DynamoDB table IDs |
| <a name="output_table_names"></a> [table_names](#output_table_names) | Map of table keys to DynamoDB table names |
<!-- END_TF_DOCS — DO NOT EDIT ABOVE THIS LINE -->
