AWSCli dynamodb update-item command syntax
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The awscli is a powerful command-line tool that allows users to interact with AWS services, including Amazon DynamoDB, Amazon's NoSQL database service. One of the key functionalities is the update-item command, which is used to modify the attributes of an item within a DynamoDB table.
AWS CLI DynamoDB Update-Item Command
The update-item command in AWS CLI is used to update an item or add new attributes to it in a DynamoDB table. The operation can be complex, as it includes updating, deleting, or adding new attributes and providing conditions under which the update would be applied.
Basic Syntax
The basic syntax for the update-item command is as follows:
Parameters
--table-name: The name of the table containing the item to update.--key: A map of attribute name to attribute values, representing the primary key of the item to update. For an in depth explanation on how to specify keys, refer to the AWS DynamoDB documentation.--update-expression: An expression that defines one or more attributes to be updated, the action to be performed on them, and new values for them.--expression-attribute-names: A placeholder-to-attribute-name mapping, used in theupdate-expression.--expression-attribute-values: A placeholder-to-attribute-value mapping, used in theupdate-expression.--condition-expression(Optional): Specifies a condition that must be satisfied in order for a conditional update to proceed.--return-values(Optional): UseALL_OLD,UPDATED_OLD,ALL_NEW,UPDATED_NEW, orNONE. This setting determines what gets returned in the response: the values before or after the update.
Example
Let's consider a simple example where we have a table named Employees with EmployeeID as the partition key, and we want to update an employee's department and increase their salary. Below is how this can be accomplished with update-item:
In this example, the update-expression specifies two operations:
- Setting a new Department value.
- Incrementing the existing Salary by an amount.
Conditional Update
Let’s say you want to update an employee’s information only if their salary is below a certain amount. This can be done using a condition expression:
Here, the condition-expression ensures that the update will only occur if the employee's current salary is less than 4500.
Returning Values
Through --return-values, you can control what data is returned from the update. The options can be:
ALL_OLD: Returns all attributes of the item, as they appeared before the UpdateItem operation.UPDATED_OLD: Returns only the updated attributes, as they appeared before the UpdateItem operation.ALL_NEW: Returns all attributes of the item, as they appear after the UpdateItem operation.UPDATED_NEW: Returns only the updated attributes, as they appear after the UpdateItem operation.NONE: No attributes are returned.
Table Summary
Here is a summary table to encapsulate the main aspects of the update-item command:
| Parameter | Description |
--table-name | The table containing the item to update. |
--key | The primary key of the item to update. |
--update-expression | Expression defining actions on attributes. |
--expression-attribute-names | Mapping for placeholder names in expressions. |
--expression-attribute-values | Mapping for placeholder values in expressions. |
--condition-expression | Conditional expression to restrict updates. |
--return-values | Specifies what values are returned post-update. |
The AWS CLI update-item command provides immense flexibility for modifying items in a DynamoDB table through a combination of expressions and conditionals, thereby allowing complex update logic tailored to specific use cases. Understanding these components is crucial for ensuring efficient and effective updates to your data.

