3

I'm trying to add an attribute to a whole table, without specifying an index.

In this examples it's always being used an index:

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

Plus, that's an update for an attribute that is already in the table.

How can add a new attribute to each record of a table?

1
  • --update-expression SET will add new attribute automatically if it doesn't exist Commented Dec 11, 2020 at 16:19

1 Answer 1

2

There is no API that will automatically add an attribute to all items in a table. DynamoDB just doesn't work that way.

The only way to add an attribute to all items in a table is to scan the table and for each item, make an UpdateItem request to add the attribute you want. This can be done for attributes that are missing (ie. adding new), or attributes that already exist and just being updated.

Some caveats:

  • If the table is small, and not being updated too often, this may work as intended in a single pass
  • If the table is larger and being updated relatively fast (ie. every second) then you will need to make sure the code updating the table is also adding the attribute to new items, or items being updated and that the updates don't clobber
  • Lastly, if the table is large, this can consume a LOT of capacity because of the scan and update for each item so plan on it taking a long time (also mind the consumed capacity vs. provisioned capacity) -- better have some rate-limiting on the update script
Sign up to request clarification or add additional context in comments.

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.