6

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.

4 Answers 4

11

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'

Sign up to request clarification or add additional context in comments.

Comments

7

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"}}'

Comments

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""}}"

Comments

0

Example with

  1. pk and sk
  2. array index accessing
  3. reserved words in attribute-name and values

aws dynamodb update-item \
    --table-name rulesets \
    --key '{"id":{"S":"cljr7n7tb0001pn6x30k869vf"}, "program":{"S":"cljr7n7tb0001pn6x30k869vf"}}' \
    --update-expression "SET steps[0].conditions.#a[1].#p = :ad" \
    --endpoint-url http://localhost:8000 \
    --expression-attribute-values file://values.json \
    --expression-attribute-names file://attrs.json \
    --return-values ALL_NEW

Here
id => partition_key_col_name
program => sort_key_col_name


values.json
{
   ":ad" : {"S": "$.patient.dateOfBirth"}
}

attrs.json
{
    "#a" : "all",
    "#p" : "path"
}

We could also update json collections https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.