AWS CloudFormation
User Guide (API Version 2010-05-15)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

AWS::DynamoDB::Table

Creates a DynamoDB table.

Note

AWS CloudFormation typically creates DynamoDB tables in parallel. However, if your template includes DynamoDB tables with indexes, you must declare dependencies so that the tables are created sequentially. For a sample snippet, see DynamoDB Table with a DependsOn Attribute.

Syntax

{
   "Type" : "AWS::DynamoDB::Table",
   "Properties" : {
      "AttributeDefinitions" : [ AttributeDefinitions, ... ],
      "GlobalSecondaryIndexes" : [ GlobalSecondaryIndexes, ... ],
      "KeySchema" : [ KeySchema, ... ],
      "LocalSecondaryIndexes" : [ LocalSecondaryIndexes, ... ],
      "ProvisionedThroughput" : { ProvisionedThroughput },
      "TableName" : String
   }
}

Properties

AttributeDefinitions

A list of AttributeName and AttributeType objects that describe the key schema for the table and indexes.

Required: Yes

Type: DynamoDB Attribute Definitions

Update requires: Replacement

GlobalSecondaryIndexes

Global secondary indexes to be created on the table. You can create up to 5 global secondary indexes.

Required: No

Type: DynamoDB Global Secondary Indexes

Update requires: Replacement

KeySchema

Specifies the attributes that make up the primary key for the table. The attributes in the KeySchema property must also be defined in the AttributeDefinitions property.

Required: Yes

Type: DynamoDB Key Schema

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: DynamoDB Local Secondary Indexes

Update requires: Replacement

ProvisionedThroughput

Throughput for the specified table, consisting of values for ReadCapacityUnits and WriteCapacityUnits. For more information about the contents of a Provisioned Throughput structure, see DynamoDB Provisioned Throughput.

Required: Yes

Type: DynamoDB Provisioned Throughput

Update requires: No interruption

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 do updates that require this resource to be replaced. You can still do updates to this resource if the update requires no or some interruption.

Required: No

Type: Name Type

Update requires: Replacement

Note

For detailed information about the limits in DynamoDB, see Limits in Amazon DynamoDB in the Amazon DynamoDB Developer Guide.

Return Value

When the logical ID of this resource is provided to the Ref intrinsic function, it 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.

DynamoDB Table with Local and Secondary Indexes

The following sample creates an DynamoDB table with Album, Artist, and Sales as attributes. The primary key includes the Album attribute as the hash key and Artist attribute as the range key. The table also includes a global and a 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 of an album, the local secondary index uses the same hash key as the table but uses the Sales attribute as the range key.

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Resources" : {
    "myDynamoDBTable" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions" : [
          {
            "AttributeName" : "Album",
            "AttributeType" : "S"   
          },
          {
            "AttributeName" : "Artist",
            "AttributeType" : "S"
          },
          {
            "AttributeName" : "Sales",
            "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"],
            "ProjectionType" : "INCLUDE"
          },
          "ProvisionedThroughput" : {
            "ReadCapacityUnits" : "5",
            "WriteCapacityUnits" : "5"
          }
        }],
        "LocalSecondaryIndexes" :[{
          "IndexName" : "myLSI",
          "KeySchema" : [
            {
              "AttributeName" : "Album",
              "KeyType" : "HASH"
            },
            {
              "AttributeName" : "Sales",
              "KeyType" : "RANGE"
            }
          ],                           
          "Projection" : {
            "NonKeyAttributes" : ["Artist"],
            "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. 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.

"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"}
      }
    }]
  }
}