1
votes

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

2 Answers

2
votes

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

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