AWS Command Line Interface v2 を使用して DynamoDB グローバルテーブルとマルチリージョンレプリケーションを操作する - Amazon DynamoDB

AWS Command Line Interface v2 を使用して DynamoDB グローバルテーブルとマルチリージョンレプリケーションを操作する

次のコード例は、マルチリージョンレプリケーションを備えた DynamoDB グローバルテーブルを管理する方法を示しています。

  • マルチリージョンレプリケーションを備えたテーブルを作成します。

  • レプリカテーブルの項目を配置および取得します。

  • レプリカを削除します。

Bash
Bash スクリプトを使用した AWS CLI

マルチリージョンレプリケーションを備えたテーブルを作成します。

# 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 base table aws dynamodb describe-table --table-name MusicTable --region us-west-2

レプリカテーブルに項目を配置します。

# 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 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 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

AWS SDK デベロッパーガイドとコード例の詳細なリストについては、「AWS SDK で DynamoDB を使用する」を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。