how to return items in a dynamodb on aws-cli
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reading data from DynamoDB with the AWS CLI is straightforward once you choose the right command for the access pattern. Use get-item for one exact primary key, query for multiple items that share a partition key, and scan only when you truly need to inspect the whole table.
Get One Item by Primary Key
get-item is the most direct way to fetch a single record. You must provide the full primary key in DynamoDB's typed JSON format.
If the table uses a composite primary key, include both key attributes:
The response contains an Item field when a match exists. If the item is missing, the command still succeeds, but Item is absent.
Query Multiple Items Efficiently
When you need several items from the same partition, use query. This is fast because it uses the table key design rather than reading everything.
You can add sort-key conditions as well:
If you only need a few attributes, add a projection expression to reduce payload size:
Projection expressions reduce response size, but they do not change which items DynamoDB reads to satisfy the key condition. They are still useful for CLI workflows because the output becomes easier to inspect, pipe to jq, or hand off to another command.
Use scan Only as a Last Resort
scan reads the table broadly and is usually the most expensive option. It is useful for ad hoc inspection, migration work, or tables without a queryable access pattern for the data you need.
A filter expression on scan does not make the read efficient. DynamoDB still examines the scanned items first and filters afterward.
Control Consistency and Output
By default, DynamoDB reads are eventually consistent. If your workflow needs the most recent committed value and the table supports it, enable strong consistency on get-item or query:
For shell pipelines, combine the CLI output with --output json and a jq filter so you can inspect only the fields you care about instead of scrolling through the full response.
Common Pitfalls
- Using
scanwhenget-itemorquerywould work. That increases latency and read cost. - Forgetting DynamoDB attribute types such as
"S","N", and"BOOL"in the CLI JSON input. - Expecting
get-itemto fail when the item is absent. It returns noIteminstead. - Omitting part of a composite primary key.
get-itemneeds the full key. - Confusing filter expressions with key conditions. Filters narrow returned data, but they do not change how much data DynamoDB reads.
Summary
- Use
get-itemfor one exact key lookup. - Use
queryfor multiple items that share a partition key, optionally with sort-key conditions. - Use
scansparingly because it is the least efficient read pattern. - Provide keys and expression values in DynamoDB's typed JSON format.
- Add projection expressions when you want smaller, cheaper responses.

