Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding non-key attribute to DynamoDB using Terraform.

I am trying to add non-key attribute in Dynamo Table. Just trying to execute this https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html first example. I receive the error "All attribute must be indexed. Unused attribute"

Can someone explain this? Or how to add non-key attribute to dynamodb with Terraform.

like image 628
Ashish Bhatia Avatar asked Jun 11 '26 23:06

Ashish Bhatia


1 Answers

DynamoDB is a schemaless data store, so in general it is not necessary to declare attributes ahead of time.

The exception is any attribute that acts as a key either for the table itself (primary key, sort key) or for a secondary index. These must be defined as part of the definition of the table because DynamoDB needs to know how to structure the necessary indices to efficiently query by these attributes.

This is mentioned in more detail in the documentation:

Only define attributes on the table object that are going to be used as:

  • Table hash key or range key
  • LSI or GSI hash key or range key

The three attributes UserId, GameTitle, and TopScore from the example are used across the hash_key, range_key, and the GameTitleIndex index respectively, and so they should be accepted.

To set an attribute that isn't a key, you just need to include it when writing the item to the table. DynamoDB will store that result and return it when the item is retrieved, but you will not be able to use it as a filter or sort key when querying the table.

like image 55
Martin Atkins Avatar answered Jun 14 '26 17:06

Martin Atkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!