2015-06-04 02:07:03 +02:00
---
layout: "aws"
page_title: "AWS: dynamodb_table"
sidebar_current: "docs-aws-resource-dynamodb-table"
description: |-
Provides a DynamoDB table resource
---
# aws\_dynamodb\_table
Provides a DynamoDB table resource
## Example Usage
The following dynamodb table description models the table and GSI shown
2016-01-14 21:55:39 +01:00
in the [AWS SDK example documentation ](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html )
2015-06-04 02:07:03 +02:00
2017-04-17 12:17:54 +02:00
```hcl
2015-06-04 02:07:03 +02:00
resource "aws_dynamodb_table" "basic-dynamodb-table" {
2017-02-18 23:48:50 +01:00
name = "GameScores"
read_capacity = 20
write_capacity = 20
hash_key = "UserId"
range_key = "GameTitle"
attribute {
name = "UserId"
type = "S"
}
attribute {
name = "GameTitle"
type = "S"
}
attribute {
name = "TopScore"
type = "N"
}
2017-05-01 00:20:38 +02:00
ttl {
attribute_name = "TimeToExist"
enabled = false
}
2017-02-18 23:48:50 +01:00
global_secondary_index {
name = "GameTitleIndex"
hash_key = "GameTitle"
range_key = "TopScore"
write_capacity = 10
read_capacity = 10
projection_type = "INCLUDE"
non_key_attributes = ["UserId"]
}
tags {
Name = "dynamodb-table-1"
Environment = "production"
}
2015-06-04 02:07:03 +02:00
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the table, this needs to be unique
within a region.
* `read_capacity` - (Required) The number of read units for this table
* `write_capacity` - (Required) The number of write units for this table
2016-05-23 12:07:03 +02:00
* `hash_key` - (Required, Forces new resource) The attribute to use as the hash key (the
2015-06-04 02:07:03 +02:00
attribute must also be defined as an attribute record
2016-05-23 12:07:03 +02:00
* `range_key` - (Optional, Forces new resource) The attribute to use as the range key (must
2015-06-04 02:07:03 +02:00
also be defined)
* `attribute` - Define an attribute, has two properties:
* `name` - The name of the attribute
* `type` - One of: S, N, or B for (S)tring, (N)umber or (B)inary data
2016-03-04 22:15:17 +01:00
* `stream_enabled` - (Optional) Indicates whether Streams are to be enabled (true) or disabled (false).
* `stream_view_type` - (Optional) 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.
2017-05-01 00:20:38 +02:00
* `ttl` - (Optional) Indicates whether time to live is enabled (true) or disabled (false) and the `attribute_name` to be used.
2016-05-23 12:07:03 +02:00
* `local_secondary_index` - (Optional, Forces new resource) Describe an LSI on the table;
2015-06-04 02:07:03 +02:00
these can only be allocated *at creation* so you cannot change this
2015-08-03 22:57:24 +02:00
definition after you have created the resource.
2015-06-04 02:07:03 +02:00
* `global_secondary_index` - (Optional) Describe a GSO for the table;
subject to the normal limits on the number of GSIs, projected
2016-03-22 19:08:57 +01:00
attributes, etc.
2017-02-01 21:44:30 +01:00
* `tags` - (Optional) A map of tags to populate on the created table.
2015-06-04 02:07:03 +02:00
For both `local_secondary_index` and `global_secondary_index` objects,
the following properties are supported:
* `name` - (Required) The name of the LSI or GSI
2015-12-02 23:05:45 +01:00
* `hash_key` - (Required for GSI) The name of the hash key in the index; must be
2016-03-22 19:08:57 +01:00
defined as an attribute in the resource. Only applies to
2015-12-02 23:05:45 +01:00
`global_secondary_index`
2015-06-04 02:07:03 +02:00
* `range_key` - (Required) The name of the range key; must be defined
* `projection_type` - (Required) One of "ALL", "INCLUDE" or "KEYS_ONLY"
where *ALL* projects every attribute into the index, *KEYS_ONLY*
projects just the hash and range key into the index, and *INCLUDE*
projects only the keys specified in the _non_key_attributes_
2015-08-03 22:57:24 +02:00
parameter.
2015-06-04 02:07:03 +02:00
* `non_key_attributes` - (Optional) Only required with *INCLUDE* as a
2015-06-30 20:38:16 +02:00
projection type; a list of attributes to project into the index. These
2015-08-03 22:57:24 +02:00
do not need to be defined as attributes on the table.
2015-06-04 02:07:03 +02:00
For `global_secondary_index` objects only, you need to specify
`write_capacity` and `read_capacity` in the same way you would for the
table as they have separate I/O capacity.
2015-06-30 20:38:16 +02:00
### A note about attributes
Only define attributes on the table object that are going to be used as:
2015-06-30 20:43:13 +02:00
* Table hash key or range key
* LSI or GSI hash key or range key
2015-06-30 20:38:16 +02:00
2015-08-03 22:57:24 +02:00
The DynamoDB API expects attribute structure (name and type) to be
passed along when creating or updating GSI/LSIs or creating the initial
table. In these cases it expects the Hash / Range keys to be provided;
because these get re-used in numerous places (i.e the table's range key
could be a part of one or more GSIs), they are stored on the table
object to prevent duplication and increase consistency. If you add
attributes here that are not used in these scenarios it can cause an
2016-03-22 19:08:57 +01:00
infinite loop in planning.
2015-06-30 20:38:16 +02:00
2015-06-04 02:07:03 +02:00
## Attributes Reference
The following attributes are exported:
2015-08-03 22:57:24 +02:00
* `arn` - The arn of the table
2015-06-04 02:07:03 +02:00
* `id` - The name of the table
2016-03-22 19:08:57 +01:00
* `stream_arn` - The ARN of the Table Stream. Only available when `stream_enabled = true`
2015-06-04 02:07:03 +02:00
2016-11-11 10:54:56 +01:00
## Import
2017-02-18 23:48:50 +01:00
DynamoDB tables can be imported using the `name` , e.g.
2016-11-11 10:54:56 +01:00
```
$ terraform import aws_dynamodb_table.basic-dynamodb-table GameScores
```