dynobase-icon
Dynobase

DynamoDB CLI Commands and Query Examples

Rafal Wilinski

Written by Rafal Wilinski

Last updated: October 23rd, 2026

Rewriting the same filters again and again? Dynobase builds filters visually, with operators for real-world data, and saves parameterized favorites that ask for values when you run them. Filters and favorites in Dynobase

Use aws dynamodb query with --key-condition-expression to read items by a partition key and optional sort key condition. AWS CLI v2 expects DynamoDB JSON values such as {"S":"customer-123"}, follows paginated reads automatically, and uses --query only to reshape output locally. The examples below use one consistent table schema and include filters, indexes, sorting, counts and reusable JSON request files.

Command index

Setup

Check that the installed CLI reports version 2, and confirm which identity will run the commands:

If necessary, use the AWS CLI installation instructions. For an IAM Identity Center profile, run aws sso login --profile dev before using it. The examples use POSIX shell quoting, suitable for bash or zsh; other shells can require different escaping.

The table is MyTableName, with String partition key id and String sort key createdAt. Items for a customer share id; timestamps use a consistent UTC format so lexicographic sorting matches time order. This example assumes each timestamp uniquely identifies an item within its customer. The GSI example uses an additional status-createdAt-index with String partition key status and String sort key createdAt.

Important environment variables

VariablePurpose
AWS_PROFILESelect a configured profile, such as dev
AWS_REGION / AWS_DEFAULT_REGIONSelect the Region; AWS_REGION takes precedence in CLI v2
AWS_DEFAULT_OUTPUTSet an output format such as json, yaml, text or table
AWS_CA_BUNDLEPath to a custom HTTPS certificate bundle
AWS_SHARED_CREDENTIALS_FILEOverride the shared credentials file location
AWS_PAGERSet the terminal pager; an empty value disables it

You can also append --profile dev --region us-east-1 to a command. Keep these settings consistent across table creation and later reads; a table in another Region will look missing. --no-cli-pager disables display paging without disabling API pagination. The CLI environment variable reference documents precedence.

Create a table

Create an on-demand table and wait until it can accept reads and writes:

HASH means partition key and RANGE means sort key. Define key attributes only in --attribute-definitions; ordinary fields do not need declarations. For provisioned capacity, replace the billing-mode line with --billing-mode PROVISIONED --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1, choosing values appropriate to the workload.

JSON syntax is also accepted. This is an alternative to the preceding creation command, not a second step:

Interactive table creation

CLI v2 auto-prompt helps complete a command without memorizing every parameter:

Use a different table name if you already created the example table. The table schema designer is another way to prepare a CreateTable payload.

CLI skeletons are not CloudFormation templates

--generate-cli-skeleton input emits a CLI input document, not an infrastructure template. Edit its fields and remove options you do not need before submitting it:

Skeleton contents can change between CLI versions. For CloudFormation, use an actual resource definition instead:

Do not deploy this definition over the manually created example table without an appropriate import plan. Dynobase infrastructure code generation can generate CloudFormation, CDK or Terraform for a table.

Delete a table

This deletes the table and its items. Run it only when finished with the example data:

Deletion is asynchronous and deletion protection, when enabled, blocks it. Deleting a table also disables its stream; already retained stream records remain subject to their retention period. The DeleteTable API describes allowed states and deletion behavior.

List tables

This lists tables for the current account and Region. The CLI automatically follows service pages. Here --query is a JMESPath output selector, not a DynamoDB Query expression. See the list-tables reference.

Describe a table

Remove --query to inspect the full response, including capacity settings and storage statistics. ItemCount is maintained asynchronously and is not an exact current count for an application query.

Create an on-demand backup

Save the returned ARN in a shell variable named BACKUP_ARN for the following examples. Backup creation is asynchronous and does not consume table read capacity. A backup is not a promise that all operational settings will be restored automatically.

List backups

Omit the time bounds for all dates, or omit --table-name to list backups across tables in the selected Region. Results appear in BackupSummaries.

Describe a backup

Confirm the source table and that the backup is available before restoring it. Use the ARN from actual output rather than constructing one from a table name.

Restore a table from backup

Restore creates a new table. Reconfigure settings such as auto scaling, IAM policies, alarms, tags, Streams, TTL and point-in-time recovery as needed. The restore documentation lists the settings that require attention.

Get all items with Scan

The command follows all pages. For a single bounded service request, add --limit 100 --no-paginate. To return only customers named Doe from each evaluated page:

This is still a scan. Choose the access operation using Query vs Scan, and use FilterExpression syntax for more predicates.

Use JSON files for expressions

Save this as expression-attribute-names.json:

Save this as expression-attribute-values.json:

Then run:

CLI expression values require the S, N, BOOL and other type wrappers even when read from a file. Numbers use strings inside N; Booleans use JSON Booleans inside BOOL.

Get one item

Supply both key attributes for this table:

--consistent-read is optional. An absent item produces no Item field. If the key schema differs from the example, update every key consistently rather than changing only one command.

Put an item

The condition makes this an insert-only example. Without it, put-item replaces the whole existing item with that primary key. You can place the same typed item JSON in item.json and use --item file://item.json instead.

Query a set of items

Partition key equality

The partition key requires equality. Query works on a partition-key-only table too; a sort key is needed only for sort-key conditions and ordering within an item collection.

Sort key range

BETWEEN includes both endpoints. This example assumes timestamps stored at whole-second precision. If you store fractional seconds, choose bounds that match that representation. For a month prefix, use begins_with(#created, :prefix) with :prefix set to {"S":"2026-10"} instead of the range condition and range placeholders.

Filter and project non-key attributes

Filters run after reads and do not reduce consumed capacity. Projection reduces response attributes, not the read charge. To select items without updatedAt, replace the filter with attribute_not_exists(#updated), add the corresponding alias, and remove the now-unused :status value.

Query a secondary index

This requires status-createdAt-index to exist with status as its partition key and createdAt as its sort key:

Use the index's key schema, not the base table's. GSIs support eventual consistency only and can return only projected attributes. A low-cardinality status-only key is an illustration, not a universal production index design.

Count matching items

COUNT reads the same candidates and consumes read capacity. With automatic pagination the CLI aggregates page counts. Do not combine COUNT with a projection expression. With --no-paginate, the count covers just one service page.

Save a complete query input

Save this as query-input.json:

The CLI Query reference documents all request fields. Keep request JSON separate from output-selection expressions supplied through --query.

Query with sorting

Reverse sort-key order with --no-scan-index-forward:

Ascending is the default. DynamoDB sorts by the selected table or index's sort key; this flag does not sort arbitrary attributes. To read only the most recent item without a filter, add --limit 1 --no-paginate.

Query pagination

If the output contains NextToken, set TOKEN to that exact string and resume:

For manual pagination, use --no-paginate and pass the service's LastEvaluatedKey map to --exclusive-start-key. Never put LastEvaluatedKey in --starting-token. --page-size controls each underlying call, whereas --max-items controls CLI output. The pagination guide covers empty filtered pages and SDK equivalents.

Update an item

Aliases handle reserved words. The existence condition prevents this example from creating a new item if its key is absent. Without a condition, UpdateItem can create an item. See the update-item reference.

Increment an attribute

This atomically adds one to a Number attribute, using zero as the starting value if the attribute is missing. Repeating the successful command increments again, so it is not an idempotent retry mechanism.

Delete an item

Delete only if the item's status is still CLOSED:

Remove the condition and its expression maps for an unconditional delete. ALL_OLD returns the removed item, when present. A conditional failure means the write was not applied.

Run commands against DynamoDB Local

Use the same endpoint option on every command intended for the local database. The DynamoDB Local setup guide covers local credentials and starting the service. Local behavior does not prove managed-service capacity or throttling behavior.

Frequently Asked Questions

How do I query DynamoDB with AWS CLI?

Use aws dynamodb query with a partition-key equality in --key-condition-expression and typed values in --expression-attribute-values. Add a sort-key condition when needed.

What is the difference between --query and --key-condition-expression?

--key-condition-expression tells DynamoDB which key range to read. --query is a local JMESPath expression that reshapes the CLI response after the service call.

Does AWS CLI return all DynamoDB query pages?

It follows pages automatically by default. --no-paginate limits it to one service call, while --max-items caps the paginator output and can return a NextToken.

How do I list DynamoDB tables?

Run aws dynamodb list-tables in the intended profile and Region. Add --query 'TableNames' --output text for a compact list.

Does --generate-cli-skeleton create CloudFormation?

No. It produces CLI request or sample response JSON. CloudFormation requires a template containing resources such as AWS::DynamoDB::Table.

Dynobase is a Professional GUI Client for DynamoDB

Start your 7-day free trial today

Product Features

Download
/
Changelog
/
Pricing
/
Member Portal
/
Privacy
/
EULA
/
Twitter
© 2026 Dynobase