DynamoDB UpdateItem with the AWS CLI
aws dynamodb update-item changes specific attributes of one item (and creates it if absent), leaving everything else untouched. The change is expressed with --update-expression and DynamoDB-JSON values.
Code
aws dynamodb update-item \
--table-name Music \
--key '{
"Artist": {"S": "Arturo Sandoval"},
"SongTitle": {"S": "Cubano Chant"}
}' \
--update-expression "SET Genre = :genre, #yr = :year ADD Awards :inc" \
--expression-attribute-names '{"#yr": "Year"}' \
--expression-attribute-values '{
":genre": {"S": "Latin Jazz"},
":year": {"N": "1994"},
":inc": {"N": "1"}
}' \
--return-values ALL_NEW \
--region us-east-1With --return-values ALL_NEW the command prints the item after the update:
{
"Attributes": {
"Artist": {"S": "Arturo Sandoval"},
"SongTitle": {"S": "Cubano Chant"},
"Genre": {"S": "Latin Jazz"},
"Year": {"N": "1994"},
"Awards": {"N": "1"}
}
}Explanation
--key— the full primary key of the item to update, in DynamoDB JSON.--update-expression— one or more clauses:SETassigns attributes (Genre = :genre).Yearis reserved, so it's aliased through--expression-attribute-names.ADDon a number is an atomic increment (ADD Awards :inc).REMOVEdeletes an attribute;DELETEremoves set elements.
--expression-attribute-values— the:placeholder→ typed-value map in DynamoDB JSON.--return-values—ALL_NEWprints the item after the update (alsoUPDATED_NEW,ALL_OLD,UPDATED_OLD,NONE).- Upsert semantics —
update-itemcreates the item if the key doesn't exist. Add--condition-expression "attribute_exists(Artist)"to update-only.
Do it visually
The DynamoDB Expression Builder builds SET / ADD / REMOVE clauses with the name/value JSON and hands you a copy-paste CLI command.
To edit items in a GUI — change a field, review the generated UpdateExpression, copy-as-CLI — download DynoTable.
Related guides
- DynamoDB update expressions —
SET,ADD,REMOVE,DELETE, and idioms. - Understanding ReturnValues — what each
--return-valuesoption gives you.