Tutorial: Creating a global table
This section describes how to create a global table using the Amazon DynamoDB console or the AWS Command Line Interface (AWS CLI).
Topics
Follow these steps to create a global table using the AWS Management Console. The following example creates a global table with replica tables in United States and Europe.
-
Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/home
. For this example, choose the US East (Ohio) Region. -
In the navigation pane on the left side of the console, choose Tables.
-
Choose Create Table.
-
On the Create table page, do the following:
-
For Table name, enter
Music
. -
For Partition key, enter
Artist
. -
For Sort key, enter
SongTitle
. -
Keep the default selection of String for both Partition key and Sort key.
-
Keep the other default selections on the page, and then choose Create table.
This new table serves as the first replica table in a new global table. It's the prototype for other replica tables that you add later.
-
-
On the Tables page, choose the newly created Music table, then do the following:
-
Choose the Global tables tab, and then choose Create replica.
-
In the Available replication Regions dropdown list, choose US West (Oregon) us-west-2.
The console checks to make sure that a table with the same name doesn't exist in the selected Region. If a table with the same name does exist, you must delete the existing table before you can create a new replica table in that Region.
-
Choose Create replica. This starts the table creation process in the US West (Oregon) us-west-2 Region.
The Global tables tab for the Music table (and for any other replica tables) shows that the table has been replicated in multiple Regions.
-
Add another Region so that your global table is replicated and synchronized across the United States and Europe. To do this, repeat step 5.b, but this time, specify Europe (Frankfurt) eu-central-1 instead of US West (Oregon) us-west-2.
-
-
Make sure that you're still using the AWS Management Console in the US East (Ohio) Region. Then, do the following:
-
Choose Explore table items.
-
Choose Create item.
-
For Artist, enter
item_1
. -
For SongTitle, enter
Song Value 1
. -
To save the item, choose Create item.
-
-
After a short time, the item is replicated across all three Regions of your global table. To verify this, in the console, on the Region selector in the upper-right corner, choose Europe (Frankfurt). The Music table in Europe (Frankfurt) should contain the new item.
-
Repeat step 7 and choose US West (Oregon) to verify replication in that Region.
Follow these steps to create a global table Music
using the AWS CLI.
The following example creates a global table, with replica tables in the United
States and in Europe.
-
Create a new table (
Music
) in US East (Ohio), with DynamoDB Streams enabled (NEW_AND_OLD_IMAGES
).aws dynamodb create-table \ --table-name Music \ --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 \ --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES \ --region us-east-2
-
Create an identical
Music
table in US East (N. Virginia).aws dynamodb update-table --table-name Music --cli-input-json \ '{ "ReplicaUpdates": [ { "Create": { "RegionName": "us-east-1" } } ] }' \ --region=us-east-2
-
Repeat step 2 to create a table in Europe (Ireland) (eu-west-1).
-
You can view the list of replicas created using
describe-table
.aws dynamodb describe-table --table-name Music --region us-east-2
-
To verify that replication is working, add a new item to the
Music
table in US East (Ohio).aws dynamodb put-item \ --table-name Music \ --item '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-2
-
Wait for a few seconds, and then check to see whether the item has been successfully replicated to US East (N. Virginia) and Europe (Ireland).
aws dynamodb get-item \ --table-name Music \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-1
aws dynamodb get-item \ --table-name Music \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region eu-west-1
-
Delete the replica table in Europe (Ireland) Region.
aws dynamodb update-table --table-name Music --cli-input-json \ '{ "ReplicaUpdates": [ { "Delete": { "RegionName": "eu-west-1" } } ] }'
The following java code sample, create a Music
table in
Europe (Ireland) region then creates a replica in Asia Pacific (Seoul)
region.
package com.amazonaws.codesamples.gtv2 import java.util.logging.Logger; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.BillingMode; import com.amazonaws.services.dynamodbv2.model.CreateReplicationGroupMemberAction; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest; import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.Projection; import com.amazonaws.services.dynamodbv2.model.ProjectionType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputOverride; import com.amazonaws.services.dynamodbv2.model.ReplicaGlobalSecondaryIndex; import com.amazonaws.services.dynamodbv2.model.ReplicationGroupUpdate; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.services.dynamodbv2.model.StreamSpecification; import com.amazonaws.services.dynamodbv2.model.StreamViewType; import com.amazonaws.services.dynamodbv2.model.UpdateTableRequest; import com.amazonaws.waiters.WaiterParameters; public class App { private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); public static void main( String[] args ) { String tableName = "Music"; String indexName = "index1"; Regions calledRegion = Regions.EU_WEST_1; Regions destRegion = Regions.AP_NORTHEAST_2; AmazonDynamoDB ddbClient = AmazonDynamoDBClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider("default")) .withRegion(calledRegion) .build(); LOGGER.info("Creating a regional table - TableName: " + tableName +", IndexName: " + indexName + " ....."); ddbClient.createTable(new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("Artist").withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName("SongTitle").withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement().withAttributeName("Artist").withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName("SongTitle").withKeyType(KeyType.RANGE)) .withBillingMode(BillingMode.PAY_PER_REQUEST) .withGlobalSecondaryIndexes(new GlobalSecondaryIndex() .withIndexName(indexName) .withKeySchema(new KeySchemaElement() .withAttributeName("SongTitle") .withKeyType(KeyType.HASH)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL))) .withStreamSpecification(new StreamSpecification() .withStreamEnabled(true) .withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES))); LOGGER.info("Waiting for ACTIVE table status ....."); ddbClient.waiters().tableExists().run(new WaiterParameters<>(new DescribeTableRequest(tableName))); LOGGER.info("Testing parameters for adding a new Replica in " + destRegion + " ....."); CreateReplicationGroupMemberAction createReplicaAction = new CreateReplicationGroupMemberAction() .withRegionName(destRegion.getName()) .withGlobalSecondaryIndexes(new ReplicaGlobalSecondaryIndex() .withIndexName(indexName) .withProvisionedThroughputOverride(new ProvisionedThroughputOverride() .withReadCapacityUnits(15L))); ddbClient.updateTable(new UpdateTableRequest() .withTableName(tableName) .withReplicaUpdates(new ReplicationGroupUpdate() .withCreate(createReplicaAction.withKMSMasterKeyId(null)))); } }