1

I am writing a shell script file for doing some operation on dynamoDB using AWS CLI. I am trying to update an attribute in an item in a dynamodb table if the attribute already exists.

However, I am not comfortable with the syntax of the update-item command. I want to update an attribute named 'conf' with some value. However, I am not able to figure out the syntax for SET in thie command. This is what I have got till now :

aws dynamodb update-item --table-name MY_TABLE_NAME --key '{"AccountId": {"S": accountId}}'

I know the above has to followed by the SET option.

Any help would be appreciated.

2 Answers 2

2

I think it would look something like this:

aws dynamodb update-item --table-name MY_TABLE_NAME --key file://update-key.json --update-expression "SET conf = :newconf" --expression-attribute-values file://update-attr-values.json --condition-expression "attribute_exists(conf)" --return-values ALL_NEW

update-key.json

{
    "AccountId": {
        "S": "account123"
    }
}

update-attr-values.json

{
    ":newconf": {
        "S": "new conf value"
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

My usecase if to do this update for various accounts by reading them from a file one at a time. So, I do not want to create separate json files for key and expression attribute values. Can you suggest the command that just updates an attribute value without using any json files ?
You could use shell script variables. How are you iterating through the file?
I want a similar command for update-item where I do not need to provide json files. I will iterate through the file line by line and for every line, I will update the table. Hence, I want to inject the value of shell script variables in the command and not use separate json files.
Here is the command to query a table for a given reference id : aws dynamodb query --table-name MY_TABLE --select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" --key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": '\"$referenceId\"' } ], "ComparisonOperator": "EQ"} }' --limit 1 | jq '.Items[0].AccountId.S' I want a similar command to update an attribute of a table without using json files.
2

It is possible to do this without files but the answer is hidden in the 700+ pages developer guide of dynamodb:

aws dynamodb update-item \
--region MY_REGION \
--table-name MY_TABLE_NAME \
--key='{"AccountId": {"S": accountId}}' \
--update-expression 'SET conf=:newconf' \
--expression-attribute-values '{":newconf":{"S":"new conf value"}}'

The developer guide of Dynamo DB can be found here: Dynamo DB Developer guide

On page 206 by Atomic Counters, there is an example of how to use --expression-attribute-values without a file

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.