This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.
Using Amazon DynamoDB in the Amazon CLI
An introduction to Amazon DynamoDB |
---|
|
The Amazon Command Line Interface (Amazon CLI) provides support for all of the Amazon database services, including Amazon DynamoDB. You can use the Amazon CLI for impromptu operations, such as creating a table. You can also use it to embed DynamoDB operations within utility scripts.
For more information about using the Amazon CLI with DynamoDB, see
dynamodb
in the Amazon CLI Command Reference.
To list the Amazon CLI commands for DynamoDB, use the following command.
$
aws dynamodb help
Prerequisites
To run the dynamodb
commands, you need to:
Install and configure the Amazon CLI. For more information, see Installing, updating, and uninstalling the Amazon CLI and Authentication and access credentials for the Amazon CLI.
Creating and using DynamoDB tables
The command line format consists of an DynamoDB command name, followed by the parameters for that command. The Amazon CLI supports the CLI shorthand syntax for the parameter values, and full JSON.
The following example creates a table named MusicCollection
.
$
aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
You can add new lines to the table with commands similar to those shown in the following example. These examples use a combination of shorthand syntax and JSON.
$
aws dynamodb put-item \ --table-name MusicCollection \ --item '{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} , "AlbumTitle": {"S": "Somewhat Famous"} }' \ --return-consumed-capacity TOTAL
{ "ConsumedCapacity": { "CapacityUnits": 1.0, "TableName": "MusicCollection" } }
$
aws dynamodb put-item \ --table-name MusicCollection \ --item '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} , "AlbumTitle": {"S": "Songs About Life"} }' \ --return-consumed-capacity TOTAL
{ "ConsumedCapacity": { "CapacityUnits": 1.0, "TableName": "MusicCollection" } }
It can be difficult to compose valid JSON in a single-line command. To make this
easier, the Amazon CLI can read JSON files. For example, consider the following JSON snippet,
which is stored in a file named expression-attributes.json
.
{ ":v1": {"S": "No One You Know"}, ":v2": {"S": "Call Me Today"} }
You can use that file to issue a query
request using the Amazon CLI. In the
following example, the content of the expression-attributes.json
file is used as the value for the --expression-attribute-values
parameter.
$
aws dynamodb query --table-name MusicCollection \ --key-condition-expression "Artist = :v1 AND SongTitle = :v2" \ --expression-attribute-values
file://expression-attributes.json
{ "Count": 1, "Items": [ { "AlbumTitle": { "S": "Somewhat Famous" }, "SongTitle": { "S": "Call Me Today" }, "Artist": { "S": "No One You Know" } } ], "ScannedCount": 1, "ConsumedCapacity": null }
Using DynamoDB Local
In addition to DynamoDB, you can use the Amazon CLI with DynamoDB Local. DynamoDB Local is a small client-side database and server that mimics the DynamoDB service. DynamoDB Local enables you to write applications that use the DynamoDB API, without manipulating any tables or data in the DynamoDB web service. Instead, all of the API actions are rerouted to a local database. This lets you save on provisioned throughput, data storage, and data transfer fees.
For more information about DynamoDB Local and how to use it with the Amazon CLI, see the following sections of the Amazon DynamoDB Developer Guide:
Resources
Amazon CLI reference:
Service reference:
-
DynamoDB Local in the Amazon DynamoDB Developer Guide
-
Using the Amazon CLI with DynamoDB Local in the Amazon DynamoDB Developer Guide