DynamoDB CloudFormation template examples

This post provides example AWS cft templates to deploy DynamoDB tables. AWS CloudFormation enables you to use a template file to create and configure a collection of AWS resources together as a single unit.

See Also

AWS cft template for DynamoDB table

Follow below steps to create and deploy the CloudFormation template, this article uses vscode to create yaml template, you can use text editor of your choice in case vscode is not available.

Step 1: Create directory with name cft-tutorials and open it in vscode.

Step 2: Create a file sample_cft.yaml inside cft-tutorials.

Provisioned Mode DynamoDB CFT Example

Below example AWS cft template deploys a DynamoDB table in provisioned mode.

   
AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  SampleTable: 
    Type: AWS::DynamoDB::Table
    Properties: 
      AttributeDefinitions: 
        - 
          AttributeName: "Brand"
          AttributeType: "S"
        - 
          AttributeName: "Category"
          AttributeType: "S"
      KeySchema: 
        - 
          AttributeName: "Brand"
          KeyType: "HASH"
        - 
          AttributeName: "Category"
          KeyType: "RANGE"
      BillingMode: PROVISIONED
      ProvisionedThroughput: 
        ReadCapacityUnits: "5"
        WriteCapacityUnits: "5"
      TableName: "SampleTable"
   

On-Demand Mode DynamoDB CFT Example

Below example AWS cft template deploys a DynamoDB table in on-demand mode.

   
AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  SampleTable: 
    Type: AWS::DynamoDB::Table
    Properties: 
      AttributeDefinitions: 
        - 
          AttributeName: "Brand"
          AttributeType: "S"
        - 
          AttributeName: "Category"
          AttributeType: "S"
      KeySchema: 
        - 
          AttributeName: "Brand"
          KeyType: "HASH"
        - 
          AttributeName: "Category"
          KeyType: "RANGE"
      BillingMode: PAY_PER_REQUEST
      TableName: "SampleTable"
     

DynamoDB table encrypted with AWS KMS Key CFT Example

Below example AWS cft template deploys a DynamoDB table that is encrypted with AWS KMS.

   
AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  SampleTable: 
    Type: AWS::DynamoDB::Table
    Properties: 
      AttributeDefinitions: 
        - 
          AttributeName: "Brand"
          AttributeType: "S"
        - 
          AttributeName: "Category"
          AttributeType: "S"
      KeySchema: 
        - 
          AttributeName: "Brand"
          KeyType: "HASH"
        - 
          AttributeName: "Category"
          KeyType: "RANGE"
      BillingMode: PAY_PER_REQUEST      
      SSESpecification:
        SSEEnabled: true
        SSEType: KMS 
      TableName: "SampleTable"
   

DynamoDB table with tags CFT Example

Below example AWS cft template deploys a DynamoDB table and applies tags on the table.

   
AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  SampleTable: 
    Type: AWS::DynamoDB::Table
    Properties: 
      AttributeDefinitions: 
        - 
          AttributeName: "Brand"
          AttributeType: "S"
        - 
          AttributeName: "Category"
          AttributeType: "S"
      KeySchema: 
        - 
          AttributeName: "Brand"
          KeyType: "HASH"
        - 
          AttributeName: "Category"
          KeyType: "RANGE"
      BillingMode: PAY_PER_REQUEST      
      SSESpecification:
        SSEEnabled: true
        SSEType: KMS 
      Tags:
        - Key: Name 
          Value: "SampleTable"
        - Key: Purpose
          Value: Testing
      TableName: "SampleTable"
   

Deploy stack to create resources

Step 3: Copy the above YAML template in sample_cft.yaml.

Step 4: Open the AWS CloudFormation directly with the URL https://console.aws.amazon.com/cloudformation/ .

Step 5: Navigate to Stacks, Click on Create stack and click on With new resources (standard).

Step 6: Select Template is ready . Select Upload a template file. Click on Choose file to select sample_cft.yaml from cft-tutorials directory and click on Next.

dynamodb-cft

Step 7: Enter Stack name and click on Next.

dynamodb-cft

Step 8: In "Configure stack options" page click on Next.

Step 9: Click on Create stack.

Step 10: Check Stack Events section, on completion you should see CREATE_COMPLETE for the stack.

dynamodb-cft


Category: AWS