0

It seems that modifying a dynamo table is not allowed with terraform.

How would I go about adding a GSI to an existing table with terraform? I was previously doing it in python (with boto3 update_table), but now trying to do it in Terraform. I don't want to lose data nor to have to do it manually.

This is the error I keep getting:

Error: error creating DynamoDB Table: ResourceInUseException: Table already exists

And my code:

# Create a DynamoDB table with GSIs.
resource "aws_dynamodb_table" "table" {
  name         = var.function_name
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "PK" # partition key
  range_key    = "SK" # sort key

  # Partition Key.
  attribute {
    name = "PK"
    type = "S"
  }

  # Sort Key (datetime in UTC).
  attribute {
    name = "SK"
    type = "S"
  }

  # Date (no time).
  attribute {
    name = "date"
    type = "S"
  }

  # Define a GSI.
  global_secondary_index {
    name            = "date-index"
    hash_key        = "date" # partition key
    range_key       = "SK"
    projection_type = "ALL"
  }
}
mincom
  • 670
  • 5
  • 21
  • Modifying a table is allowed for values that can be updated. That is not a terraform thing; you just can change keys without recreating the table. You can on a GSI thought, just not main table or LSI. – theherk Feb 20 '23 at 08:27
  • 1
    @theherk you can add a GSI without recreating the table, but the table has to be managed by terraform first. – Mark B Feb 20 '23 at 13:01
  • @MarkB: That is precisely what I intended; I was referencing the line that terraform doesn't allow updating tables. In summary, the issue was that it wasn't imported. Neither AWS nor terraform prevent addition of GSI's. – theherk Feb 20 '23 at 15:07

1 Answers1

2

You have to first import your table into terraform. Only then you will be able to edit it using TF.

Marcin
  • 215,873
  • 14
  • 235
  • 294