Home

Awesome

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

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Requirements

NameVersion
<a name="requirement_terraform"></a> terraform>= 1.0
<a name="requirement_aws"></a> aws>= 5.64

Providers

NameVersion
<a name="provider_aws"></a> aws>= 5.64

Modules

No modules.

Resources

NameType
aws_appautoscaling_policy.index_read_policyresource
aws_appautoscaling_policy.index_write_policyresource
aws_appautoscaling_policy.table_read_policyresource
aws_appautoscaling_policy.table_write_policyresource
aws_appautoscaling_target.index_readresource
aws_appautoscaling_target.index_writeresource
aws_appautoscaling_target.table_readresource
aws_appautoscaling_target.table_writeresource
aws_dynamodb_table.autoscaledresource
aws_dynamodb_table.autoscaled_gsi_ignoreresource
aws_dynamodb_table.thisresource

Inputs

NameDescriptionTypeDefaultRequired
<a name="input_attributes"></a> attributesList 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 datalist(map(string))[]no
<a name="input_autoscaling_defaults"></a> autoscaling_defaultsA map of default autoscaling settingsmap(string)<pre>{<br> "scale_in_cooldown": 0,<br> "scale_out_cooldown": 0,<br> "target_value": 70<br>}</pre>no
<a name="input_autoscaling_enabled"></a> autoscaling_enabledWhether or not to enable autoscaling. See note in README about this settingboolfalseno
<a name="input_autoscaling_indexes"></a> autoscaling_indexesA map of index autoscaling configurations. See example in examples/autoscalingmap(map(string)){}no
<a name="input_autoscaling_read"></a> autoscaling_readA map of read autoscaling settings. max_capacity is the only required key. See example in examples/autoscalingmap(string){}no
<a name="input_autoscaling_write"></a> autoscaling_writeA map of write autoscaling settings. max_capacity is the only required key. See example in examples/autoscalingmap(string){}no
<a name="input_billing_mode"></a> billing_modeControls how you are billed for read/write throughput and how you manage capacity. The valid values are PROVISIONED or PAY_PER_REQUESTstring"PAY_PER_REQUEST"no
<a name="input_create_table"></a> create_tableControls if DynamoDB table and associated resources are createdbooltrueno
<a name="input_deletion_protection_enabled"></a> deletion_protection_enabledEnables deletion protection for tableboolnullno
<a name="input_global_secondary_indexes"></a> global_secondary_indexesDescribe a GSI for the table; subject to the normal limits on the number of GSIs, projected attributes, etc.any[]no
<a name="input_hash_key"></a> hash_keyThe attribute to use as the hash (partition) key. Must also be defined as an attributestringnullno
<a name="input_ignore_changes_global_secondary_index"></a> ignore_changes_global_secondary_indexWhether to ignore changes lifecycle to global secondary indices, useful for provisioned tables with scalingboolfalseno
<a name="input_import_table"></a> import_tableConfigurations for importing s3 data into a new table.any{}no
<a name="input_local_secondary_indexes"></a> local_secondary_indexesDescribe 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
<a name="input_name"></a> nameName of the DynamoDB tablestringnullno
<a name="input_point_in_time_recovery_enabled"></a> point_in_time_recovery_enabledWhether to enable point-in-time recoveryboolfalseno
<a name="input_range_key"></a> range_keyThe attribute to use as the range (sort) key. Must also be defined as an attributestringnullno
<a name="input_read_capacity"></a> read_capacityThe number of read units for this table. If the billing_mode is PROVISIONED, this field should be greater than 0numbernullno
<a name="input_replica_regions"></a> replica_regionsRegion names for creating replicas for a global DynamoDB table.any[]no
<a name="input_restore_date_time"></a> restore_date_timeTime of the point-in-time recovery point to restore.stringnullno
<a name="input_restore_source_name"></a> restore_source_nameName of the table to restore. Must match the name of an existing table.stringnullno
<a name="input_restore_source_table_arn"></a> restore_source_table_arnARN of the source table to restore. Must be supplied for cross-region restores.stringnullno
<a name="input_restore_to_latest_time"></a> restore_to_latest_timeIf set, restores table to the most recent point-in-time recovery point.boolnullno
<a name="input_server_side_encryption_enabled"></a> server_side_encryption_enabledWhether or not to enable encryption at rest using an AWS managed KMS customer master key (CMK)boolfalseno
<a name="input_server_side_encryption_kms_key_arn"></a> server_side_encryption_kms_key_arnThe 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.stringnullno
<a name="input_stream_enabled"></a> stream_enabledIndicates whether Streams are to be enabled (true) or disabled (false).boolfalseno
<a name="input_stream_view_type"></a> stream_view_typeWhen 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.stringnullno
<a name="input_table_class"></a> table_classThe storage class of the table. Valid values are STANDARD and STANDARD_INFREQUENT_ACCESSstringnullno
<a name="input_tags"></a> tagsA map of tags to add to all resourcesmap(string){}no
<a name="input_timeouts"></a> timeoutsUpdated Terraform resource management timeoutsmap(string)<pre>{<br> "create": "10m",<br> "delete": "10m",<br> "update": "60m"<br>}</pre>no
<a name="input_ttl_attribute_name"></a> ttl_attribute_nameThe name of the table attribute to store the TTL timestamp instring""no
<a name="input_ttl_enabled"></a> ttl_enabledIndicates whether ttl is enabledboolfalseno
<a name="input_write_capacity"></a> write_capacityThe number of write units for this table. If the billing_mode is PROVISIONED, this field should be greater than 0numbernullno

Outputs

NameDescription
<a name="output_dynamodb_table_arn"></a> dynamodb_table_arnARN of the DynamoDB table
<a name="output_dynamodb_table_id"></a> dynamodb_table_idID of the DynamoDB table
<a name="output_dynamodb_table_stream_arn"></a> dynamodb_table_stream_arnThe ARN of the Table Stream. Only available when var.stream_enabled is true
<a name="output_dynamodb_table_stream_label"></a> dynamodb_table_stream_labelA timestamp, in ISO 8601 format of the Table Stream. Only available when var.stream_enabled is true
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Authors

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

License

Apache 2 Licensed. See LICENSE for full details.