DynamoDB GetItem with the AWS CLI

aws dynamodb get-item reads a single item by its full primary key. On the CLI the key is passed as DynamoDB JSON — each value wrapped with its type ({"S": "..."}, {"N": "..."}).

Code

aws dynamodb get-item \
  --table-name Music \
  --key '{
    "Artist":    {"S": "Arturo Sandoval"},
    "SongTitle": {"S": "Cubano Chant"}
  }' \
  --projection-expression "Artist, SongTitle, AlbumTitle, #yr" \
  --expression-attribute-names '{"#yr": "Year"}' \
  --consistent-read \
  --region us-east-1

The response prints the item in DynamoDB JSON:

{
  "Item": {
    "Artist": {"S": "Arturo Sandoval"},
    "SongTitle": {"S": "Cubano Chant"},
    "Year": {"N": "1981"}
  }
}

When no item matches, the command prints nothing (empty output) and exits 0 — there is no Item key.

Explanation

  • --table-name — the table to read from.
  • --key — DynamoDB JSON containing every primary-key attribute (partition key, plus sort key for a composite table). A partial key errors out.
  • --projection-expression — optional; limits which attributes return. Year is reserved, so it's aliased through --expression-attribute-names.
  • --consistent-read — opt into a strongly-consistent read (2× RCU); omit it for the default eventually-consistent read.
  • Tip: on Windows cmd, JSON quoting differs — put the --key JSON in a file and pass --key file://key.json to avoid escaping headaches.

Do it visually

Hand-writing DynamoDB JSON is error-prone. Use the free DynamoDB Expression Builder to generate the projection and attribute-name maps, then copy a ready-to-run CLI command.

To browse tables and read items in a GUI instead of the terminal — key form, results grid, copy-as-CLI — download DynoTable.

Work with DynamoDB without the Console

DynoTable is a fast desktop client for DynamoDB — browse tables, run SQL-style queries, and edit items locally.