How can I add a local secondary index to an existing DynamoDB table using Terraform without replacing it?

212 views Asked by At

We are following the single table architecture for AWS DynamoDB and using a single table to store all the entities.

We have users with their vehicles registered with us and the table structure would be:

user_id entity_id entity_type
1 1 car
1 2 bike

and key attributes:

  • partition_key = user_id
  • sort_key = entity_id

Now, we are required to fetch only car, bike or any other vehicle using user_id for which I want to add a local secondary index with an existing partition key with entity_type as the sort key.

While applying this change, It's trying to recreate the table.

Is there any way to avoid recreation of the table as we already have data in our existing table?

1

There are 1 answers

0
Mark B On

Per the DynamoDB documentation:

Local secondary indexes on a table are created when the table is created.

So there isn't anything Terraform can do about this. It is AWS that is enforcing the rule that the table has to be recreated to add a local secondary index.

Instead of using a local secondary index, I suggest you use a Global Secondary Index, which can be created after the table exists.