1
votes

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.

1
I haven't tested it but the example given in the linked documentation should be fine. Can you show the exact code you are using in your question and also post the exact error you get?ydaetskcoR

1 Answers

4
votes

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.