Amazon DynamoDB - Global Tables

Objective

This exercise will show you the steps for using the Global Tables engine for Amazon DynamoDB tables. If you want more information click here.

By the end of this exercise, you will be able to:

  • Replicate Amazon DynamoDB tables in other AWS Regions

Estimated Duration: 15 minutes

Approximate Cost: 1 USD

Execution

Go to AWS CloudShell

  1. Go to AWS CloudShell, in the top bar of the AWS Console, click the button on the right side of the search bar.

Create an Amazon DynamoDB table

  1. Create a new table Music in the region US East (N. Virginia).
    aws dynamodb create-table \
        --table-name Music \
        --attribute-definitions \
            AttributeName=Artist,AttributeType=S \
            AttributeName=Song,AttributeType=S \
        --key-schema \
            AttributeName=Artist,KeyType=HASH \
            AttributeName=Song,KeyType=RANGE \
        --billing-mode PAY_PER_REQUEST \
        --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES \
        --region us-east-1
    

Create a replica of the table in another AWS Region

  1. Create a table Music identical in US West (N. California).

    aws dynamodb update-table --table-name Music --cli-input-json --region us-east-1 \
        '{
            "ReplicaUpdates":
            [
                {
                "Create": {
                    "RegionName": "us-west-1"
                    }
                }
            ]
        }' 
    
  2. (Optional) You can view the list of replicas created using describe-table.

    aws dynamodb describe-table --table-name Music --region us-east-1
    

    If any of the tables are not in the list of replicates, wait a few minutes for them to be created.

Enter sample data

  1. To verify that replication is working, add a new item to the Music table in the region US East (N. Virginia).

    aws dynamodb put-item \
        --table-name Music \
        --item '{"Artist": {"S":"The Beatles"},"Song": {"S":"Hey Jude"}}' \
        --region us-east-1
    
  2. To verify that replication is bidirectional, add a new item to the Music table in the region US East (N. California).

    aws dynamodb put-item \
        --table-name Music \
        --item '{"Artist": {"S":"The Clash"},"Song": {"S":"London Calling"}}' \
        --region us-west-1
    
  3. Make sure the items have been successfully replicated to the regions:

    aws dynamodb scan --table-name Music --region us-east-1
    aws dynamodb scan --table-name Music --region us-west-1
    

Cleaning up

  1. Delete the replica table in the region US West (N. California).

    aws dynamodb update-table --table-name Music --cli-input-json --region us-east-1 \
    '{
        "ReplicaUpdates":
        [
            {
            "Delete": {
                "RegionName": "us-west-1"
                }
            }
        ]
    }'
    
  2. Wait a few minutes until the secondary table be deleted. The primary table status will change from UPDATING to ACTIVE. You can check the deletion already finished using the command below:

    aws dynamodb describe-table --table-name Music --region us-east-1 | jq -r '.Table.TableStatus'
    
  3. After the deletion of secondary table be completed, delete the primary table Music in the region US East (N. Virginia).

    aws dynamodb delete-table --table-name Music --region us-east-1
    

Conclusion

With this exercise, it was possible to verify how replicated Amazon DynamoDB tables can be used in a strategy of the type. Active-Active, both for reading and for data persist.

Source: Amazon DynamoDB - Developer Guide