DynamoDB Query with the AWS CLI

aws dynamodb query reads every item that shares one partition key, optionally narrowing by the sort key. Values go in --expression-attribute-values as DynamoDB JSON ({":x": {"S": "..."}}).

Code

aws dynamodb query \
  --table-name Music \
  --key-condition-expression "Artist = :artist AND begins_with(SongTitle, :prefix)" \
  --expression-attribute-values '{
    ":artist": {"S": "Arturo Sandoval"},
    ":prefix": {"S": "C"}
  }' \
  --no-scan-index-forward \
  --region us-east-1

--no-scan-index-forward returns items in descending sort-key order; use --scan-index-forward (the default) for ascending.

Pagination

By default the CLI auto-paginates — it follows LastEvaluatedKey internally and prints the combined result. To page manually (e.g. for large result sets), control it with:

aws dynamodb query \
  --table-name Music \
  --key-condition-expression "Artist = :artist" \
  --expression-attribute-values '{":artist": {"S": "Arturo Sandoval"}}' \
  --page-size 100 \
  --max-items 50 \
  --region us-east-1
# The output includes a "NextToken"; pass it back with --starting-token to continue.

Explanation

  • --key-condition-expression — required partition-key equality, plus at most one sort-key condition (=, <, >, BETWEEN, begins_with). It cannot filter non-key attributes; use --filter-expression for that.
  • --expression-attribute-values — the :placeholder → typed-value map in DynamoDB JSON.
  • --page-size — items DynamoDB scans per underlying request (a performance/throttling knob), not the number returned.
  • --max-items — caps how many items the CLI returns, emitting a NextToken for the rest; resume with --starting-token.
  • Query a secondary index with --index-name ....

Do it visually

The DynamoDB Expression Builder generates the key-condition + attribute-values JSON and hands you a copy-paste CLI command.

To run queries against real tables in a GUI — key-condition form, paginated grid, copy-as-CLI — download DynoTable.

Travaille avec DynamoDB sans la Console

DynoTable est un client de bureau rapide pour DynamoDB — parcours les tables, exécute des requêtes de style SQL et édite les items en local.