[AWS Lab] CloudFormation – DynamoDB

In this lab, we will learn how to create a DynamoDB table using CloudFormation.


    1. Create a Template File

    • Please review the following YAML template file and save it “myDynamoDB.yaml“.
    AWSTemplateFormatVersion: "2010-09-09"
    Description: "AWS CloudFormation Sample Template DynamoDB."
    
    Parameters:
      ReadCapacityUnits:
        Description: "Provisioned read throughput"
        Type: "Number"
        Default: "1"
        MinValue: "1"
        MaxValue: "1000"
        ConstraintDescription: "must be between 1 and 1000"
      WriteCapacityUnits:
        Description: "Provisioned write throughput"
        Type: "Number"
        Default: "1"
        MinValue: "1"
        MaxValue: "10000"
        ConstraintDescription: "must be between 1 and 1000"
    
    Resources:
      OrdersTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: "Orders"
          AttributeDefinitions:
            - AttributeName: "OrderId"
              AttributeType: "N"
            - AttributeName: "Item"
              AttributeType: "S"
            - AttributeName: "Price"
              AttributeType: "N"
            - AttributeName: "OrderDate"
              AttributeType: "S"  
          KeySchema:
            - AttributeName: "OrderId"
              KeyType: "HASH"
            - AttributeName: "Item"
              KeyType: "RANGE"
          ProvisionedThroughput:
            ReadCapacityUnits: !Ref ReadCapacityUnits
            WriteCapacityUnits: !Ref WriteCapacityUnits
          LocalSecondaryIndexes:
            - IndexName: "PriceIndex"
              KeySchema:
                - AttributeName: "OrderId"
                  KeyType: "HASH"
                - AttributeName: "Price"
                  KeyType: "RANGE"
              Projection:
                ProjectionType: "KEYS_ONLY"
          GlobalSecondaryIndexes:
            - IndexName: "OrderDateIndex"
              KeySchema:            
                - AttributeName: "OrderDate"
                  KeyType: "HASH"
                - AttributeName: "OrderId"
                  KeyType: "RANGE"
              Projection:
                ProjectionType: "ALL"
              ProvisionedThroughput:
                ReadCapacityUnits: !Ref ReadCapacityUnits
                WriteCapacityUnits: !Ref WriteCapacityUnits
     
    Outputs:
      TableName:
          Value: !Ref OrdersTable
          Description: "Name of the newly created DynamoDB table"
    

    2. CloudFormation – Create a Stack

    • Click “Create stack
      • Click “with new resources (standard)
    • Create Stack
      • Check “Template is ready
      • Check “Upload a template file
        • Choose “myDynamoDB.yaml
      • Click “View In Designer
        • Click the “Validate Template” button
        • Click the “Create Stack” button – cloud shape
      • Click “Next
    • Stack Details
      • Stack name: “DynamoDBStack
      • parameters
        • ReadCapacityUnits“: 2
        • WriteCapacityUnits“: 2
      • Click “Next
    • Stack Options
      • Accept all defaults
      • Click “Next
    • Review
      • Click “Submit

    3. CloudFormation – Create Resources

    • The stack is creating a DynamoDB table with indexes.
      • Wait until the process completes.
    • Check the “Events” tab
    • Check the “Outputs” tab
      • You can see the table name.
    • Check the “Resources” tab
      • Navigate to the DynamoDB table and check the properties

    Leave a Comment