There are more AWS SDK examples available in the AWS Doc SDK Examples
DynamoDB examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with DynamoDB.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use batch-get-item.
- AWS CLI
-
To retrieve multiple items from a table
The following
batch-get-itemsexample reads multiple items from theMusicCollectiontable using a batch of threeGetItemrequests, and requests the number of read capacity units consumed by the operation. The command returns only theAlbumTitleattribute.aws dynamodb batch-get-item \ --request-itemsfile://request-items.json\ --return-consumed-capacityTOTALContents of
request-items.json:{ "MusicCollection": { "Keys": [ { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} }, { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }, { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"} } ], "ProjectionExpression":"AlbumTitle" } }Output:
{ "Responses": { "MusicCollection": [ { "AlbumTitle": { "S": "Somewhat Famous" } }, { "AlbumTitle": { "S": "Blue Sky Blues" } }, { "AlbumTitle": { "S": "Louder Than Ever" } } ] }, "UnprocessedKeys": {}, "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 1.5 } ] }For more information, see Batch Operations in the Amazon DynamoDB Developer Guide.
-
For API details, see BatchGetItem
in AWS CLI Command Reference.
-
The following code example shows how to use batch-write-item.
- AWS CLI
-
To add multiple items to a table
The following
batch-write-itemexample adds three new items to theMusicCollectiontable using a batch of threePutItemrequests. It also requests information about the number of write capacity units consumed by the operation and any item collections modified by the operation.aws dynamodb batch-write-item \ --request-itemsfile://request-items.json\ --return-consumed-capacityINDEXES\ --return-item-collection-metricsSIZEContents of
request-items.json:{ "MusicCollection": [ { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}, "AlbumTitle": {"S": "Songs About Life"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"}, "AlbumTitle": {"S": "Blue Sky Blues"} } } } ] }Output:
{ "UnprocessedItems": {}, "ItemCollectionMetrics": { "MusicCollection": [ { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] }, { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } ] }, "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 6.0, "Table": { "CapacityUnits": 3.0 }, "LocalSecondaryIndexes": { "AlbumTitleIndex": { "CapacityUnits": 3.0 } } } ] }For more information, see Batch Operations in the Amazon DynamoDB Developer Guide.
-
For API details, see BatchWriteItem
in AWS CLI Command Reference.
-
The following code example shows how to use create-backup.
- AWS CLI
-
To create a backup for an existing DynamoDB table
The following
create-backupexample creates a backup of theMusicCollectiontable.aws dynamodb create-backup \ --table-nameMusicCollection\ --backup-nameMusicCollectionBackupOutput:
{ "BackupDetails": { "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "BackupName": "MusicCollectionBackup", "BackupSizeBytes": 0, "BackupStatus": "CREATING", "BackupType": "USER", "BackupCreationDateTime": 1576616366.715 } }For more information, see On-Demand Backup and Restore for DynamoDB in the Amazon DynamoDB Developer Guide.
-
For API details, see CreateBackup
in AWS CLI Command Reference.
-
The following code example shows how to use create-global-table.
- AWS CLI
-
To create a global table
The following
create-global-tableexample creates a global table from two identical tables in the specified, separate AWS Regions.aws dynamodb create-global-table \ --global-table-nameMusicCollection\ --replication-groupRegionName=us-east-2RegionName=us-east-1\ --regionus-east-2Output:
{ "GlobalTableDescription": { "ReplicationGroup": [ { "RegionName": "us-east-2" }, { "RegionName": "us-east-1" } ], "GlobalTableArn": "arn:aws:dynamodb::123456789012:global-table/MusicCollection", "CreationDateTime": 1576625818.532, "GlobalTableStatus": "CREATING", "GlobalTableName": "MusicCollection" } }For more information, see DynamoDB Global Tables in the Amazon DynamoDB Developer Guide.
-
For API details, see CreateGlobalTable
in AWS CLI Command Reference.
-
The following code example shows how to use create-table.
- AWS CLI
-
Example 1: To create a table with tags
The following
create-tableexample uses the specified attributes and key schema to create a table namedMusicCollection. This table uses provisioned throughput and is encrypted at rest using the default AWS owned CMK. The command also applies a tag to the table, with a key ofOwnerand a value ofblueTeam.aws dynamodb create-table \ --table-nameMusicCollection\ --attribute-definitionsAttributeName=Artist,AttributeType=SAttributeName=SongTitle,AttributeType=S\ --key-schemaAttributeName=Artist,KeyType=HASHAttributeName=SongTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=5,WriteCapacityUnits=5\ --tagsKey=Owner,Value=blueTeamOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 }, "TableSizeBytes": 0, "TableName": "MusicCollection", "TableStatus": "CREATING", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": "2020-05-26T16:04:41.627000-07:00", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 2: To create a table in On-Demand Mode
The following example creates a table called
MusicCollectionusing on-demand mode, rather than provisioned throughput mode. This is useful for tables with unpredictable workloads.aws dynamodb create-table \ --table-nameMusicCollection\ --attribute-definitionsAttributeName=Artist,AttributeType=SAttributeName=SongTitle,AttributeType=S\ --key-schemaAttributeName=Artist,KeyType=HASHAttributeName=SongTitle,KeyType=RANGE\ --billing-modePAY_PER_REQUESTOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T11:44:10.807000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 0, "WriteCapacityUnits": 0 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "BillingModeSummary": { "BillingMode": "PAY_PER_REQUEST" } } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 3: To create a table and encrypt it with a Customer Managed CMK
The following example creates a table named
MusicCollectionand encrypts it using a customer managed CMK.aws dynamodb create-table \ --table-nameMusicCollection\ --attribute-definitionsAttributeName=Artist,AttributeType=SAttributeName=SongTitle,AttributeType=S\ --key-schemaAttributeName=Artist,KeyType=HASHAttributeName=SongTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=5,WriteCapacityUnits=5\ --sse-specificationEnabled=true,SSEType=KMS,KMSMasterKeyId=abcd1234-abcd-1234-a123-ab1234a1b234Output:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T11:12:16.431000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "SSEDescription": { "Status": "ENABLED", "SSEType": "KMS", "KMSMasterKeyArn": "arn:aws:kms:us-west-2:123456789012:key/abcd1234-abcd-1234-a123-ab1234a1b234" } } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 4: To create a table with a Local Secondary Index
The following example uses the specified attributes and key schema to create a table named
MusicCollectionwith a Local Secondary Index namedAlbumTitleIndex.aws dynamodb create-table \ --table-nameMusicCollection\ --attribute-definitionsAttributeName=Artist,AttributeType=SAttributeName=SongTitle,AttributeType=SAttributeName=AlbumTitle,AttributeType=S\ --key-schemaAttributeName=Artist,KeyType=HASHAttributeName=SongTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --local-secondary-indexes \ "[ { \"IndexName\": \"AlbumTitleIndex\", \"KeySchema\": [ {\"AttributeName\": \"Artist\",\"KeyType\":\"HASH\"}, {\"AttributeName\": \"AlbumTitle\",\"KeyType\":\"RANGE\"} ], \"Projection\": { \"ProjectionType\": \"INCLUDE\", \"NonKeyAttributes\": [\"Genre\", \"Year\"] } } ]"Output:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Genre", "Year" ] }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ] } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 5: To create a table with a Global Secondary Index
The following example creates a table named
GameScoreswith a Global Secondary Index calledGameTitleIndex. The base table has a partition key ofUserIdand a sort key ofGameTitle, allowing you to find an individual user's best score for a specific game efficiently, whereas the GSI has a partition key ofGameTitleand a sort key ofTopScore, allowing you to quickly find the overall highest score for a particular game.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=SAttributeName=TopScore,AttributeType=N\ --key-schemaAttributeName=UserId,KeyType=HASH\AttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --global-secondary-indexes \ "[ { \"IndexName\": \"GameTitleIndex\", \"KeySchema\": [ {\"AttributeName\":\"GameTitle\",\"KeyType\":\"HASH\"}, {\"AttributeName\":\"TopScore\",\"KeyType\":\"RANGE\"} ], \"Projection\": { \"ProjectionType\":\"INCLUDE\", \"NonKeyAttributes\":[\"UserId\"] }, \"ProvisionedThroughput\": { \"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5 } } ]"Output:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "TopScore", "AttributeType": "N" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-26T17:28:15.602000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "GlobalSecondaryIndexes": [ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "UserId" ] }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameTitleIndex" } ] } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 6: To create a table with multiple Global Secondary Indexes at once
The following example creates a table named
GameScoreswith two Global Secondary Indexes. The GSI schemas are passed via a file, rather than on the command line.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=SAttributeName=TopScore,AttributeType=NAttributeName=Date,AttributeType=S\ --key-schemaAttributeName=UserId,KeyType=HASHAttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --global-secondary-indexesfile://gsi.jsonContents of
gsi.json:[ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 } }, { "IndexName": "GameDateIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "Date", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } ]Output:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Date", "AttributeType": "S" }, { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "TopScore", "AttributeType": "N" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-08-04T16:40:55.524000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "GlobalSecondaryIndexes": [ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameTitleIndex" }, { "IndexName": "GameDateIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "Date", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameDateIndex" } ] } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 7: To create a table with Streams enabled
The following example creates a table called
GameScoreswith DynamoDB Streams enabled. Both new and old images of each item will be written to the stream.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=S\ --key-schemaAttributeName=UserId,KeyType=HASHAttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --stream-specificationStreamEnabled=TRUE,StreamViewType=NEW_AND_OLD_IMAGESOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T10:49:34.056000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_AND_OLD_IMAGES" }, "LatestStreamLabel": "2020-05-27T17:49:34.056", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/stream/2020-05-27T17:49:34.056" } }For more information, see Basic Operations for Tables in the Amazon DynamoDB Developer Guide.
Example 8: To create a table with Keys-Only Stream enabled
The following example creates a table called
GameScoreswith DynamoDB Streams enabled. Only the key attributes of modified items are written to the stream.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=S\ --key-schemaAttributeName=UserId,KeyType=HASHAttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --stream-specificationStreamEnabled=TRUE,StreamViewType=KEYS_ONLYOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T18:45:34.140000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "KEYS_ONLY" }, "LatestStreamLabel": "2023-05-25T18:45:34.140", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/stream/2023-05-25T18:45:34.140", "DeletionProtectionEnabled": false } }For more information, see Change data capture for DynamoDB Streams in the Amazon DynamoDB Developer Guide.
Example 9: To create a table with the Standard Infrequent Access class
The following example creates a table called
GameScoresand assigns the Standard-Infrequent Access (DynamoDB Standard-IA) table class. This table class is optimized for storage being the dominant cost.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=S\ --key-schemaAttributeName=UserId,KeyType=HASHAttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --table-classSTANDARD_INFREQUENT_ACCESSOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T18:33:07.581000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableClassSummary": { "TableClass": "STANDARD_INFREQUENT_ACCESS" }, "DeletionProtectionEnabled": false } }For more information, see Table classes in the Amazon DynamoDB Developer Guide.
Example 10: To Create a table with Delete Protection enabled
The following example creates a table called
GameScoresand enables deletion protection.aws dynamodb create-table \ --table-nameGameScores\ --attribute-definitionsAttributeName=UserId,AttributeType=SAttributeName=GameTitle,AttributeType=S\ --key-schemaAttributeName=UserId,KeyType=HASHAttributeName=GameTitle,KeyType=RANGE\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5\ --deletion-protection-enabledOutput:
{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T23:02:17.093000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "DeletionProtectionEnabled": true } }For more information, see Using deletion protection in the Amazon DynamoDB Developer Guide.
-
For API details, see CreateTable
in AWS CLI Command Reference.
-
The following code example shows how to use delete-backup.
- AWS CLI
-
To delete an existing DynamoDB backup
The following
delete-backupexample deletes the specified existing backup.aws dynamodb delete-backup \ --backup-arnarn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3aOutput:
{ "BackupDescription": { "BackupDetails": { "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "BackupName": "MusicCollectionBackup", "BackupSizeBytes": 0, "BackupStatus": "DELETED", "BackupType": "USER", "BackupCreationDateTime": 1576616366.715 }, "SourceTableDetails": { "TableName": "MusicCollection", "TableId": "b0c04bcc-309b-4352-b2ae-9088af169fe2", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableSizeBytes": 0, "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableCreationDateTime": 1576615228.571, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "ItemCount": 0, "BillingMode": "PROVISIONED" }, "SourceTableFeatureDetails": {} } }For more information, see On-Demand Backup and Restore for DynamoDB in the Amazon DynamoDB Developer Guide.
-
For API details, see DeleteBackup
in AWS CLI Command Reference.
-
The following code example shows how to use delete-item.
- AWS CLI
-
Example 1: To delete an item
The following
delete-itemexample deletes an item from theMusicCollectiontable and requests details about the item that was deleted and the capacity used by the request.aws dynamodb delete-item \ --table-nameMusicCollection\ --keyfile://key.json\ --return-valuesALL_OLD\ --return-consumed-capacityTOTAL\ --return-item-collection-metricsSIZEContents of
key.json:{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"} }Output:
{ "Attributes": { "AlbumTitle": { "S": "Blue Sky Blues" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Scared of My Shadow" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 2.0 }, "ItemCollectionMetrics": { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } }For more information, see Writing an Item in the Amazon DynamoDB Developer Guide.
Example 2: To delete an item conditionally
The following example deletes an item from the
ProductCatalogtable only if itsProductCategoryis eitherSporting GoodsorGardening Suppliesand its price is between 500 and 600. It returns details about the item that was deleted.aws dynamodb delete-item \ --table-nameProductCatalog\ --key '{"Id":{"N":"456"}}' \ --condition-expression"(ProductCategory IN (:cat1, :cat2)) and (#P between :lo and :hi)"\ --expression-attribute-namesfile://names.json\ --expression-attribute-valuesfile://values.json\ --return-valuesALL_OLDContents of
names.json:{ "#P": "Price" }Contents of
values.json:{ ":cat1": {"S": "Sporting Goods"}, ":cat2": {"S": "Gardening Supplies"}, ":lo": {"N": "500"}, ":hi": {"N": "600"} }Output:
{ "Attributes": { "Id": { "N": "456" }, "Price": { "N": "550" }, "ProductCategory": { "S": "Sporting Goods" } } }For more information, see Writing an Item in the Amazon DynamoDB Developer Guide.
-
For API details, see DeleteItem
in AWS CLI Command Reference.
-
The following code example shows how to use delete-table.
- AWS CLI
-
To delete a table
The following
delete-tableexample deletes theMusicCollectiontable.aws dynamodb delete-table \ --table-nameMusicCollectionOutput:
{ "TableDescription": { "TableStatus": "DELETING", "TableSizeBytes": 0, "ItemCount": 0, "TableName": "MusicCollection", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 } } }For more information, see Deleting a Table in the Amazon DynamoDB Developer Guide.
-
For API details, see DeleteTable
in AWS CLI Command Reference.
-
The following code example shows how to use describe-backup.
- AWS CLI
-
To get information about an existing backup of a table