AWSCli dynamodb update-item command syntax

12,157

Solution 1

My AwsCli version did not support --update-expression option. I used the attribute-updates option instead.

Here is my command :

updatedVersion=aws dynamodb update-item --table-name MY_TABLE_NAME --key '{"AccountId": {"S": '$accountId'}}' --attribute-updates '{"Version": {"Value": {"S": '$desiredVersion'},"Action": "PUT"}}' --return-values UPDATED_NEW | jq '.Attributes.RuleSetVersion.S'

Solution 2

Below is the update command with --update-expression

 aws --region "us-east-1" dynamodb update-item \
 --table-name "MY_TABLE_NAME" --key \
 '{"Primary_Column_name":{"S":"Primary_Column_value"}}' \
 --update-expression 'SET #H = :h' \
 --expression-attribute-names '{"#H":"Column_name_to_change"}' \
 --expression-attribute-values '{":h":{"S":"Changed_Column_value"}}'

Solution 3

The other answers will work very good on MAC and Linux. If you want to run it on Windows, you need to use " quotes instead of ' and double quotes "" instead of a single quote "`"

Example:

aws dynamodb update-item --table-name MY_TABLE_NAME --key "{""PRIMARY_KEY_NAME"":{""S"":""PRIMARY_KEY_VALUE""}}" --update-expression "SET #G = :g"  --expression-attribute-names "{""#G"":""COLUMN_NAME_TO_UPDATE_VALUE""}" --expression-attribute-values "{"":g"":{""N"":""DESIRED_VALUE""}}"
Share:
12,157
roger
Author by

roger

Updated on June 08, 2022

Comments

  • roger
    roger almost 2 years

    am using AmazonAwsCli to write a shell script to update an attribute in an item in a dynamodb table. I want to update an attribute in a table for multiple items. I am reading the attribute value from a file and am trying to update the table by injecting the value of the shell script variable in the command. The documentation available at http://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-item.html suggests using separate json files for expression-attribute-names and expression-attribute-values. However, I do not want to create separate json files. Rather, I want to write one command to update an item for a given attribute value.

    My table name = MY_TABLE_NAME
    
    hashkey = AccountId
    
    shell script variable holding the value of AccountId = accountId
    
    attribute name that needs to be updated = Version
    
    shell script variable holding the value of Version = ver
    

    I have got something like :

    aws dynamodb update-item --table-name MY_TABLE_NAME --key '{"AccountId": {"S": '$accountId'}}' --update-expression "SET Version = '{"Version": {"S": '$ver'}}'" --condition-expression "attribute_exists(Version)" --return-values UPDATED_NEW

    But, the above command does not work. Can someone point me to the correct syntax.