0
votes

I want to write a terraform module that will create dynamoDb tables. The attributes are expected to be read from .tfvars or default variable instead of being already named in .tf as in the resource guide here

To explain further, say a list of attributes is being used to achieve this pseudo-code:

resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = "GameScores"
... #Other required feilds
...
... 
#  attributes is a list of names
  for(attribute_name:${length(var.attributes)}){
    attribute {
      name = "${var.attributes[i]}"
      type = "N"
    }
  }
}

How can I iterate over the attribute list and create the attribute{ } during terraform plan/apply ? The number of attribute blocks cannot be static like shown in the terraform docs, and their names must be read from variables.

1
What exactly is your question?ThomasVdBerge
@ThomasVdBerge edited nowBaghel

1 Answers

1
votes

When you create a DynamoDB table, the only attributes you need to specify are the partition key and, optionally, the sort key. All other attributes are stored as part of each document (or item) you store in the table.

The same applies to Global Secondary Indexes as well. You only need to specify the partition key and sort key for each index.

If you don't have static attributes, then you can't create a table. The names of the partition and sort keys must be the same for the lifetime of a table/index.

Finally, it's not clear from the question, but please don't use Teraform to load data into your table. It's not the right tool for that!