AWS::DynamoDB::Table
The AWS::DynamoDB::Table resource creates a DynamoDB table. For more information, see CreateTable in the Amazon DynamoDB API Reference.
You should be aware of the following behaviors when working with DynamoDB tables:
-
AWS CloudFormation typically creates DynamoDB tables in parallel. However, if your template includes multiple DynamoDB tables with indexes, you must declare dependencies so that the tables are created sequentially. Amazon DynamoDB limits the number of tables with secondary indexes that are in the creating state. If you create multiple tables with indexes at the same time, DynamoDB returns an error and the stack operation fails. For an example, see DynamoDB Table with a DependsOn Attribute.
-
Updates to
AWS::DynamoDB::Tableresources that are associated withAWS::ApplicationAutoScaling::ScalableTargetresources will always result in an update failure and then an update rollback failure. The followingScalableDimensionattributes cause this problem when associated with the table:-
dynamodb:table:ReadCapacityUnits
-
dynamodb:table:WriteCapacityUnits
-
dynamodb:index:ReadCapacityUnits
-
dynamodb:index:WriteCapacityUnits
As a workaround, please deregister scalable targets before performing updates to
AWS::DynamoDB::Tableresources. -
Syntax
To declare this entity in your AWS CloudFormation template, use the following syntax:
JSON
{ "Type" : "AWS::DynamoDB::Table", "Properties" : { "AttributeDefinitions" : [AttributeDefinition, ...], "BillingMode" :String, "GlobalSecondaryIndexes" : [GlobalSecondaryIndexes, ...], "KeySchema" : [KeySchema, ...], "LocalSecondaryIndexes" : [LocalSecondaryIndexes, ...], "PointInTimeRecoverySpecification" :PointInTimeRecoverySpecification, "ProvisionedThroughput" :ProvisionedThroughput, "SSESpecification" :SSESpecification, "StreamSpecification" :StreamSpecification, "TableName" :String, "Tags" : [Resource Tag, ...], "TimeToLiveSpecification" :TimeToLiveSpecification} }
YAML
Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: -AttributeDefinitionBillingMode:StringGlobalSecondaryIndexes: -GlobalSecondaryIndexesKeySchema: -KeySchemaLocalSecondaryIndexes: -LocalSecondaryIndexesPointInTimeRecoverySpecification:PointInTimeRecoverySpecificationProvisionedThroughput:ProvisionedThroughputSSESpecification:SSESpecificationStreamSpecification:StreamSpecificationTableName:StringTags: -Resource TagTimeToLiveSpecification:TimeToLiveSpecification
Properties
AttributeDefinitions-
A list of attributes that describe the key schema for the table and indexes. Duplicates are allowed.
Required: Yes
Type: List of AttributeDefinition
Update requires: Some interruptions. Replacement if you edit an existing
AttributeDefinition. BillingMode-
Specify how you are charged for read and write throughput and how you manage capacity.
Valid values include:
-
PROVISIONED: Sets the billing mode toPROVISIONED. We recommend usingPROVISIONEDfor predictable workloads. -
PAY_PER_REQUEST: Sets the billing mode toPAY_PER_REQUEST. We recommend usingPAY_PER_REQUESTfor unpredictable workloads.
If not specified, the default is
PROVISIONED.Required: No
Type: String
Update requires: No interruption
-
GlobalSecondaryIndexes-
Global secondary indexes to be created on the table. You can create up to 5 global secondary indexes.
Important
If you update a table to include a new global secondary index, AWS CloudFormation initiates the index creation and then proceeds with the stack update. AWS CloudFormation doesn't wait for the index to complete creation because the backfilling phase can take a long time, depending on the size of the table. You can't use the index or update the table until the index's status is
ACTIVE. You can track its status by using the DynamoDBDescribeTablecommand.If you add or delete an index during an update, we recommend that you don't update any other resources. If your stack fails to update and is rolled back while adding a new index, you must manually delete the index.
Required: No
Type: List of GlobalSecondaryIndex
Update requires: Updates are not supported. The following are exceptions:
-
If you update only the provisioned throughput values of global secondary indexes, you can update the table without interruption.
-
You can delete or add one global secondary index without interruption. If you do both in the same update (for example, by changing the index's logical ID), the update fails.
-
KeySchema-
Specifies the attributes that make up the primary key for the table. The attributes in the
KeySchemaproperty must also be defined in theAttributeDefinitionsproperty.Required: Yes
Type: List of KeySchema
Update requires: Replacement
LocalSecondaryIndexes-
Local secondary indexes to be created on the table. You can create up to 5 local secondary indexes. Each index is scoped to a given hash key value. The size of each hash key can be up to 10 gigabytes.
Required: No
Type: List of LocalSecondaryIndex
Update requires: Replacement
PointInTimeRecoverySpecification-
The settings used to enable point in time recovery.
Required: No
Type: PointInTimeRecoverySpecification
Update requires: No interruption
ProvisionedThroughput-
Throughput for the specified table, which consists of values for
ReadCapacityUnitsandWriteCapacityUnits. For more information about the contents of a provisioned throughput structure, see Amazon DynamoDB Table ProvisionedThroughput.Required: Conditional. If you set
BillingModeasPROVISIONED, you must specify this property. If you setBillingModeasPAY_PER_REQUEST, you cannot specify this property.Type: ProvisionedThroughput
Update requires: No interruption
SSESpecification-
Specifies the settings to enable server-side encryption.
Required: No
Type: DynamoDB Table SSESpecification
Update requires: Some interruptions
StreamSpecification-
The settings for the DynamoDB table stream, which capture changes to items stored in the table.
Required: No
Type: StreamSpecification
Update requires: No interruption to the table. However, the stream is replaced.
TableName-
A name for the table. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the table name. For more information, see Name Type.
Important
If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.
Required: No
Type: String
Update requires: Replacement
Tags-
Specifies an arbitrary set of tags (key–value pairs) to associate with this table. Use tags to manage your resources.
Required: No
Type: Resource Tag
Update requires: No interruption
TimeToLiveSpecification-
Specifies the Time to Live (TTL) settings for the table.
Required: No
Type: TimeToLiveSpecification
Update requires: No interruption
Note
For detailed information about the limits in DynamoDB, see Limits in Amazon DynamoDB in the Amazon DynamoDB Developer Guide.
Return Values
Ref
When the logical ID of this resource is provided to the Ref intrinsic
function, Ref returns the resource name. For example:
{ "Ref": "MyResource" }
For the resource with the logical ID myDynamoDBTable,
Ref will return the DynamoDB table name.
For more information about using the Ref function, see Ref.
Fn::GetAtt
Fn::GetAtt returns a value for a specified attribute of this type.
The following are the available attributes and sample return values.
Arn-
The Amazon Resource Name (ARN) of the DynamoDB table, such as
arn:aws:dynamodb:us-east-2:123456789012:table/myDynamoDBTable. StreamArn-
The ARN of the DynamoDB stream, such as
arn:aws:dynamodb:us-east-1:123456789012:table/testddbstack-myDynamoDBTable-012A1SL7SMP5Q/stream/2015-11-30T20:10:00.000.Note
You must specify the
StreamSpecificationproperty to use this attribute.
For more information about using Fn::GetAtt, see Fn::GetAtt.
Examples
DynamoDB Table with Local and Secondary Indexes
The following sample creates an DynamoDB table with Album, Artist,
Sales, NumberOfSongs as attributes. The primary key includes the
Album attribute as the hash key and Artist attribute as the range
key. The table also includes two global and one secondary index. For querying the
number of
sales for a given artist, the global secondary index uses the Sales attribute as
the hash key and the Artist attribute as the range key.
For querying the sales based on the number of songs, the global secondary index uses
the
NumberOfSongs attribute as the hash key and the Sales attribute as
the range key.
For querying the sales of an album, the local secondary index uses the same hash key
as
the table but uses the Sales attribute as the range key.
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "myDynamoDBTable" : { "Type" : "AWS::DynamoDB::Table", "Properties" : { "AttributeDefinitions" : [ { "AttributeName" : "Album", "AttributeType" : "S" }, { "AttributeName" : "Artist", "AttributeType" : "S" }, { "AttributeName" : "Sales", "AttributeType" : "N" }, { "AttributeName" : "NumberOfSongs", "AttributeType" : "N" } ], "KeySchema" : [ { "AttributeName" : "Album", "KeyType" : "HASH" }, { "AttributeName" : "Artist", "KeyType" : "RANGE" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" }, "TableName" : "myTableName", "GlobalSecondaryIndexes" : [{ "IndexName" : "myGSI", "KeySchema" : [ { "AttributeName" : "Sales", "KeyType" : "HASH" }, { "AttributeName" : "Artist", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Album","NumberOfSongs"], "ProjectionType" : "INCLUDE" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" } }, { "IndexName" : "myGSI2", "KeySchema" : [ { "AttributeName" : "NumberOfSongs", "KeyType" : "HASH" }, { "AttributeName" : "Sales", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Album","Artist"], "ProjectionType" : "INCLUDE" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" } }], "LocalSecondaryIndexes" :[{ "IndexName" : "myLSI", "KeySchema" : [ { "AttributeName" : "Album", "KeyType" : "HASH" }, { "AttributeName" : "Sales", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Artist","NumberOfSongs"], "ProjectionType" : "INCLUDE" } }] } } } }
YAML
AWSTemplateFormatVersion: "2010-09-09" Resources: myDynamoDBTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: "Album" AttributeType: "S" - AttributeName: "Artist" AttributeType: "S" - AttributeName: "Sales" AttributeType: "N" - AttributeName: "NumberOfSongs" AttributeType: "N" KeySchema: - AttributeName: "Album" KeyType: "HASH" - AttributeName: "Artist" KeyType: "RANGE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" TableName: "myTableName" GlobalSecondaryIndexes: - IndexName: "myGSI" KeySchema: - AttributeName: "Sales" KeyType: "HASH" - AttributeName: "Artist" KeyType: "RANGE" Projection: NonKeyAttributes: - "Album" - "NumberOfSongs" ProjectionType: "INCLUDE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" - IndexName: "myGSI2" KeySchema: - AttributeName: "NumberOfSongs" KeyType: "HASH" - AttributeName: "Sales" KeyType: "RANGE" Projection: NonKeyAttributes: - "Album" - "Artist" ProjectionType: "INCLUDE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" LocalSecondaryIndexes: - IndexName: "myLSI" KeySchema: - AttributeName: "Album" KeyType: "HASH" - AttributeName: "Sales" KeyType: "RANGE" Projection: NonKeyAttributes: - "Artist" - "NumberOfSongs" ProjectionType: "INCLUDE"
DynamoDB Table with a DependsOn Attribute
If you include multiple DynamoDB tables with indexes in a single template, you must include dependencies so that the tables are created sequentially. DynamoDB limits the number of tables with secondary indexes that are in the creating state. If you create multiple tables with indexes at the same time, DynamoDB returns an error and the stack operation fails.
The following sample assumes that the myFirstDDBTable table is declared in
the same template as the mySecondDDBTable table, and both tables include a
secondary index. The mySecondDDBTable table includes a dependency on the
myFirstDDBTable table so that AWS CloudFormation creates the tables one at a
time.
JSON
"mySecondDDBTable" : { "Type" : "AWS::DynamoDB::Table", "DependsOn" : "myFirstDDBTable" , "Properties" : { "AttributeDefinitions" : [ { "AttributeName" : "ArtistId", "AttributeType" : "S" }, { "AttributeName" : "Concert", "AttributeType" : "S" }, { "AttributeName" : "TicketSales", "AttributeType" : "S" } ], "KeySchema" : [ { "AttributeName" : "ArtistId", "KeyType" : "HASH" }, { "AttributeName" : "Concert", "KeyType" : "RANGE" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : {"Ref" : "ReadCapacityUnits"}, "WriteCapacityUnits" : {"Ref" : "WriteCapacityUnits"} }, "GlobalSecondaryIndexes" : [{ "IndexName" : "myGSI", "KeySchema" : [ { "AttributeName" : "TicketSales", "KeyType" : "HASH" } ], "Projection" : { "ProjectionType" : "KEYS_ONLY" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : {"Ref" : "ReadCapacityUnits"}, "WriteCapacityUnits" : {"Ref" : "WriteCapacityUnits"} } }], "Tags": [ { "Key": "foo", "Value": "bar" } ] } }
YAML
mySecondDDBTable: Type: AWS::DynamoDB::Table DependsOn: "myFirstDDBTable" Properties: AttributeDefinitions: - AttributeName: "ArtistId" AttributeType: "S" - AttributeName: "Concert" AttributeType: "S" - AttributeName: "TicketSales" AttributeType: "S" KeySchema: - AttributeName: "ArtistId" KeyType: "HASH" - AttributeName: "Concert" KeyType: "RANGE" ProvisionedThroughput: ReadCapacityUnits: Ref: "ReadCapacityUnits" WriteCapacityUnits: Ref: "WriteCapacityUnits" GlobalSecondaryIndexes: - IndexName: "myGSI" KeySchema: - AttributeName: "TicketSales" KeyType: "HASH" Projection: ProjectionType: "KEYS_ONLY" ProvisionedThroughput: ReadCapacityUnits: Ref: "ReadCapacityUnits" WriteCapacityUnits: Ref: "WriteCapacityUnits" Tags: - Key: foo Value: bar
DynamoDB Table with Application Auto Scaling
This example sets up Application Auto Scaling for a AWS::DynamoDB::Table resource.
The template defines a TargetTrackingScaling scaling policy that scales up the WriteCapacityUnits throughput for the table.
JSON
{ "Resources": { "DDBTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "AttributeDefinitions": [ { "AttributeName": "ArtistId", "AttributeType": "S" }, { "AttributeName": "Concert", "AttributeType": "S" }, { "AttributeName": "TicketSales", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "ArtistId", "KeyType": "HASH" }, { "AttributeName": "Concert", "KeyType": "RANGE" } ], "GlobalSecondaryIndexes": [ { "IndexName": "GSI", "KeySchema": [ { "AttributeName": "TicketSales", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "KEYS_ONLY" }, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } }, "WriteCapacityScalableTarget": { "Type": "AWS::ApplicationAutoScaling::ScalableTarget", "Properties": { "MaxCapacity": 15, "MinCapacity": 5, "ResourceId": { "Fn::Join": [ "/", [ "table", { "Ref": "DDBTable" } ] ] }, "RoleARN": { "Fn::GetAtt": ["ScalingRole", "Arn"] }, "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "ServiceNamespace": "dynamodb" } }, "ScalingRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "application-autoscaling.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:DescribeTable", "dynamodb:UpdateTable", "cloudwatch:PutMetricAlarm", "cloudwatch:DescribeAlarms", "cloudwatch:GetMetricStatistics", "cloudwatch:SetAlarmState", "cloudwatch:DeleteAlarms" ], "Resource": "*" } ] } } ] } }, "WriteScalingPolicy": { "Type": "AWS::ApplicationAutoScaling::ScalingPolicy", "Properties": { "PolicyName": "WriteAutoScalingPolicy", "PolicyType": "TargetTrackingScaling", "ScalingTargetId": { "Ref": "WriteCapacityScalableTarget" }, "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 50.0, "ScaleInCooldown": 60, "ScaleOutCooldown": 60, "PredefinedMetricSpecification": { "PredefinedMetricType": "DynamoDBWriteCapacityUtilization" } } } } } }
YAML
Resources: DDBTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: "ArtistId" AttributeType: "S" - AttributeName: "Concert" AttributeType: "S" - AttributeName: "TicketSales" AttributeType: "S" KeySchema: - AttributeName: "ArtistId" KeyType: "HASH" - AttributeName: "Concert" KeyType: "RANGE" GlobalSecondaryIndexes: - IndexName: "GSI" KeySchema: - AttributeName: "TicketSales" KeyType: "HASH" Projection: ProjectionType: "KEYS_ONLY" ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 WriteCapacityScalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: MaxCapacity: 15 MinCapacity: 5 ResourceId: !Join - / - - table - !Ref DDBTable RoleARN: !GetAtt ScalingRole.Arn ScalableDimension: dynamodb:table:WriteCapacityUnits ServiceNamespace: dynamodb ScalingRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - application-autoscaling.amazonaws.com Action: - "sts:AssumeRole" Path: "/" Policies: - PolicyName: "root" PolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "dynamodb:DescribeTable" - "dynamodb:UpdateTable" - "cloudwatch:PutMetricAlarm" - "cloudwatch:DescribeAlarms" - "cloudwatch:GetMetricStatistics" - "cloudwatch:SetAlarmState" - "cloudwatch:DeleteAlarms" Resource: "*" WriteScalingPolicy: Type: AWS::ApplicationAutoScaling::ScalingPolicy Properties: PolicyName: WriteAutoScalingPolicy PolicyType: TargetTrackingScaling ScalingTargetId: !Ref WriteCapacityScalableTarget TargetTrackingScalingPolicyConfiguration: TargetValue: 50.0 ScaleInCooldown: 60 ScaleOutCooldown: 60 PredefinedMetricSpecification: PredefinedMetricType: DynamoDBWriteCapacityUtilization
