Work with DynamoDB global tables and multi-region replication using Amazon Command Line Interface v2 - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Work with DynamoDB global tables and multi-region replication using Amazon Command Line Interface v2

The following code example shows how to manage DynamoDB global tables with multi-region replication.

  • Create a table with multi-region replication.

  • Put and get items from replica tables.

  • Remove replicas.

Bash
Amazon CLI with Bash script

Create a table with multi-region replication.

# Step 1: Create a new table in us-west-2 aws dynamodb create-table \ --table-name MusicTable \ --attribute-definitions \ AttributeName=Artist,AttributeType=S \ AttributeName=SongTitle,AttributeType=S \ --key-schema \ AttributeName=Artist,KeyType=HASH \ AttributeName=SongTitle,KeyType=RANGE \ --billing-mode PAY_PER_REQUEST \ --region us-west-2 # Step 2: Create replicas in us-east-1 and us-east-2 aws dynamodb update-table \ --table-name MusicTable \ --replica-updates '[{"Create": {"RegionName": "us-east-1"}}, {"Create": {"RegionName": "us-east-2"}}]' \ --multi-region-consistency STRONG \ --region us-west-2

Describe the multi-region table.

# Describe the base table aws dynamodb describe-table --table-name MusicTable --region us-west-2

Put items in a replica table.

# Write a single item to one of the replica tables. aws dynamodb put-item \ --table-name MusicTable \ --item '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-2

Get items from replica tables.

# Get item from the other two replicas aws dynamodb get-item \ --table-name MusicTable \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --consistent-read \ --region us-east-1 aws dynamodb get-item \ --table-name MusicTable \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --consistent-read \ --region us-west-2

Remove replicas.

# Remove the replica tables. aws dynamodb update-table \ --table-name MusicTable \ --replica-updates '[{"Delete": {"RegionName": "us-east-2"}}, {"Delete": {"RegionName": "us-east-1"}}]' \ --region us-west-2

For a complete list of Amazon SDK developer guides and code examples, see Using DynamoDB with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.