Create DynamoDB table

In this step, we will add a DynamoDB table resource to the SAM template and deploy it using AWS SAM CLI.

Edit SAM Template

  1. Open the template.yaml file in the fcaj-book-shop folder.

  2. Add a new parameter booksTableName under the Parameters section:

    booksTableName:
      Type: String
      Default: Books
    

    Add booksTableName parameter

  3. Add the BooksTable resource under the Resources section:

    BooksTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: !Ref booksTableName
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: rv_id
            AttributeType: N
          - AttributeName: name
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: rv_id
            KeyType: RANGE
        LocalSecondaryIndexes:
          - IndexName: name-index
            KeySchema:
              - AttributeName: id
                KeyType: HASH
              - AttributeName: name
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
    

    Add BooksTable resource

The above script creates the Books table in DynamoDB with:

  • Partition key: id (String)
  • Sort key: rv_id (Number)
  • Local Secondary Index: name-index with sort key name (String), projecting all attributes
  • Billing mode: PAY_PER_REQUEST (On-demand)

Build and Deploy

  1. Run the following commands to build and validate the SAM template:

    sam build
    sam validate
    

    SAM Build and Validate

  2. Deploy the updated stack:

    sam deploy
    

    The changeset will show a new resource BooksTable of type AWS::DynamoDB::Table being added. Enter y to confirm the deployment.

    SAM Deploy - Changeset

  3. Wait for the deployment to complete. You should see Successfully created/updated stack.

    Deploy Complete

Verify on AWS Console

  1. Open the CloudFormation console to verify the stack status is UPDATE_COMPLETE.

    CloudFormation Stacks

  2. Click on the fcaj-book-shop stack, switch to the Resources tab. You should see the new BooksTable resource with status CREATE_COMPLETE.

    Stack Resources

  3. Open the DynamoDB console. At the Tables page, you should see the Books table with status Active.

    DynamoDB Tables

  4. Click on the Books table to view its details:

    • Check the General information section: Partition key is id (String), Sort key is rv_id (Number), and Capacity mode is On-demand.

    Books Table - General Information

  5. Click on the Indexes tab:

    • Verify the Local secondary indexes section shows name-index with Partition key id (String) and Sort key name (String).

    Books Table - Indexes

You have now successfully created the Books table with the Local Secondary Index name-index.

Add Sample Data

  1. To add data to the table, download the file below. Then, open the file and replace all AWS-REGION with the region where you created your S3 bucket (e.g., ap-southeast-1).

Make sure to replace all occurrences of AWS-REGION in the dynamoDB.json file with your actual AWS region before running the import command.

  1. Run the following command at the directory where you saved the dynamoDB.json file:

    aws dynamodb batch-write-item --request-items file://dynamoDB.json
    

    Batch Write Item

If the output shows "UnprocessedItems": {}, it means all items were successfully written to the table.

  1. To verify the data, go to the DynamoDB console, click Explore items in the left sidebar, select the Books table, and click Run to scan the table. You should see 9 items returned.

    Explore Items