Skip to content

sweetgreen/terraform-aws-dynamodb-table

 
 

Repository files navigation

AWS DynamoDB Table Terraform module

Terraform module to create a DynamoDB table.

Usage

module "dynamodb_table" {
  source   = "terraform-aws-modules/dynamodb-table/aws"

  name     = "my-table"
  hash_key = "id"

  attributes = [
    {
      name = "id"
      type = "N"
    }
  ]

  tags = {
    Terraform   = "true"
    Environment = "staging"
  }
}

Notes

Warning: enabling or disabling autoscaling can cause your table to be recreated

There are two separate Terraform resources used for the DynamoDB table: one is for when any autoscaling is enabled the other when disabled. If your table is already created and then you change the variable autoscaling_enabled then your table will be recreated by Terraform. In this case you will need to move the old aws_dynamodb_table resource that is being destroyed to the new resource that is being created. For example:

terraform state mv module.dynamodb_table.aws_dynamodb_table.this module.dynamodb_table.aws_dynamodb_table.autoscaled

Warning: autoscaling with global secondary indexes

When using an autoscaled provisioned table with GSIs you may find that applying TF changes whilst a GSI is scaled up will reset the capacity, there is an open issue for this on the AWS Provider. To get around this issue you can enable the ignore_changes_global_secondary_index setting however, using this setting means that any changes to GSIs will be ignored by Terraform and will hence have to be applied manually (or via some other automation).

NOTE: Setting ignore_changes_global_secondary_index after the table is already created causes your table to be recreated. In this case, you will need to move the old aws_dynamodb_table resource that is being destroyed to the new resource that is being created. For example:

terraform state mv module.dynamodb_table.aws_dynamodb_table.autoscaled module.dynamodb_table.aws_dynamodb_table.autoscaled_ignore_gsi

Module wrappers

Users of this Terraform module can create multiple similar resources by using for_each meta-argument within module block which became available in Terraform 0.13.

Users of Terragrunt can achieve similar results by using modules provided in the wrappers directory, if they prefer to reduce amount of configuration files.

Examples

Requirements

Name Version
terraform >= 1.0
aws >= 5.21

Providers

Name Version
aws >= 5.21

Modules

No modules.

Resources

Name Type
aws_appautoscaling_policy.index_read_policy resource
aws_appautoscaling_policy.index_write_policy resource
aws_appautoscaling_policy.table_read_policy resource
aws_appautoscaling_policy.table_write_policy resource
aws_appautoscaling_target.index_read resource
aws_appautoscaling_target.index_write resource
aws_appautoscaling_target.table_read resource
aws_appautoscaling_target.table_write resource
aws_dynamodb_table.autoscaled resource
aws_dynamodb_table.autoscaled_gsi_ignore resource
aws_dynamodb_table.this resource

Inputs

Name Description Type Default Required
attributes List of nested attribute definitions. Only required for hash_key and range_key attributes. Each attribute has two properties: name - (Required) The name of the attribute, type - (Required) Attribute type, which must be a scalar type: S, N, or B for (S)tring, (N)umber or (B)inary data list(map(string)) [] no
autoscaling_defaults A map of default autoscaling settings map(string)
{
"scale_in_cooldown": 0,
"scale_out_cooldown": 0,
"target_value": 70
}
no
autoscaling_enabled Whether or not to enable autoscaling. See note in README about this setting bool false no
autoscaling_indexes A map of index autoscaling configurations. See example in examples/autoscaling map(map(string)) {} no
autoscaling_read A map of read autoscaling settings. max_capacity is the only required key. See example in examples/autoscaling map(string) {} no
autoscaling_write A map of write autoscaling settings. max_capacity is the only required key. See example in examples/autoscaling map(string) {} no
billing_mode Controls how you are billed for read/write throughput and how you manage capacity. The valid values are PROVISIONED or PAY_PER_REQUEST string "PAY_PER_REQUEST" no
create_table Controls if DynamoDB table and associated resources are created bool true no
deletion_protection_enabled Enables deletion protection for table bool null no
global_secondary_indexes Describe a GSI for the table; subject to the normal limits on the number of GSIs, projected attributes, etc. any [] no
hash_key The attribute to use as the hash (partition) key. Must also be defined as an attribute string null no
ignore_changes_global_secondary_index Whether to ignore changes lifecycle to global secondary indices, useful for provisioned tables with scaling bool false no
import_table Configurations for importing s3 data into a new table. any {} no
local_secondary_indexes Describe an LSI on the table; these can only be allocated at creation so you cannot change this definition after you have created the resource. any [] no
name Name of the DynamoDB table string null no
point_in_time_recovery_enabled Whether to enable point-in-time recovery bool false no
range_key The attribute to use as the range (sort) key. Must also be defined as an attribute string null no
read_capacity The number of read units for this table. If the billing_mode is PROVISIONED, this field should be greater than 0 number null no
replica_regions Region names for creating replicas for a global DynamoDB table. any [] no
server_side_encryption_enabled Whether or not to enable encryption at rest using an AWS managed KMS customer master key (CMK) bool false no
server_side_encryption_kms_key_arn The ARN of the CMK that should be used for the AWS KMS encryption. This attribute should only be specified if the key is different from the default DynamoDB CMK, alias/aws/dynamodb. string null no
stream_enabled Indicates whether Streams are to be enabled (true) or disabled (false). bool false no
stream_view_type When an item in the table is modified, StreamViewType determines what information is written to the table's stream. Valid values are KEYS_ONLY, NEW_IMAGE, OLD_IMAGE, NEW_AND_OLD_IMAGES. string null no
table_class The storage class of the table. Valid values are STANDARD and STANDARD_INFREQUENT_ACCESS string null no
tags A map of tags to add to all resources map(string) {} no
timeouts Updated Terraform resource management timeouts map(string)
{
"create": "10m",
"delete": "10m",
"update": "60m"
}
no
ttl_attribute_name The name of the table attribute to store the TTL timestamp in string "" no
ttl_enabled Indicates whether ttl is enabled bool false no
write_capacity The number of write units for this table. If the billing_mode is PROVISIONED, this field should be greater than 0 number null no

Outputs

Name Description
dynamodb_table_arn ARN of the DynamoDB table
dynamodb_table_id ID of the DynamoDB table
dynamodb_table_stream_arn The ARN of the Table Stream. Only available when var.stream_enabled is true
dynamodb_table_stream_label A timestamp, in ISO 8601 format of the Table Stream. Only available when var.stream_enabled is true

Authors

Module is maintained by Anton Babenko with help from these awesome contributors.

License

Apache 2 Licensed. See LICENSE for full details.