Skip to content

[Bug] aws_dynamodb_table GSI shows unexpected diffs when using DynamoDB local emulator #41110

Open
@Shion1305

Description

@Shion1305

Terraform Core Version

1.9.6

AWS Provider Version

5.84.0

Affected Resource(s)

aws_dynamodb_table

Expected Behavior

AWS Terraform module has the ability to apply configurations against the official DynamoDB local emulator through Custom Service Endpoint.
If the Terraform code is applied, it should show no diffs in the DynamoDB schema as long as no changes are made.

Actual Behavior

If a table has a global_secondary_index, diffs are shown as follows, even though there should be no changes.

  # module.dynamo.aws_dynamodb_table.positions will be updated in-place
  ~ resource "aws_dynamodb_table" "some_table" {
        id                          = "sample_table.dev.Positions"
        name                        = "sample_table.dev.Positions"
        tags                        = {}
        # (13 unchanged attributes hidden)

      - global_secondary_index {
          - hash_key           = "HashID" -> null
            name               = null
          - non_key_attributes = [] -> null
          - projection_type    = "ALL" -> null
          - range_key          = "Date" -> null
          - read_capacity      = 0 -> null
          - write_capacity     = 0 -> null
        }
      + global_secondary_index {
          + hash_key           = "HashID"
          + name               = "HashID-Date-index"
          + non_key_attributes = []
          + projection_type    = "ALL"
          + range_key          = "Date"
          + read_capacity      = (known after apply)
          + write_capacity     = (known after apply)
        }

        # (6 unchanged blocks hidden)
    }

Relevant Error/Panic Output Snippet

Terraform Configuration Files

main.tf

resource "aws_dynamodb_table" "some_table" {
  name         = "sample_table.${var.env}.Positions"
  billing_mode = "PAY_PER_REQUEST"

  hash_key  = "Code"
  range_key = "Date#HashId"

  attribute {
    name = "Code"
    type = "S"
  }
  attribute {
    name = "Date#HashId"
    type = "S"
  }
  # GSI attributes
  attribute {
    name = "HashID"
    type = "N"
  }
  attribute {
    name = "Date"
    type = "N"
  }

  global_secondary_index {
    name            = "HashID-Date-index"
    hash_key        = "HashID"
    range_key       = "Date"
    projection_type = "ALL"
  }
}

provider.tf

provider "aws" {
  region = "ap-northeast-1"

  endpoints {
    dynamodb = "http://localhost:8000"
  }
}

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Steps to Reproduce

  1. Run [Official DynamoDB Emulator Docker container locally.
    docker run -d \
      --name dynamodb-local \
      -p 8000:8000 \
      -v ./dynamo-data:/home/dynamodblocal/data \
      -w /home/dynamodblocal \
      amazon/dynamodb-local:latest \
      -jar DynamoDBLocal.jar -sharedDb -dbPath ./data
  2. Apply the Terraform code against localhost:8000 (local DynamoDB instance)
  3. (Check that the Terraform code is applied successfully)
  4. Run Plan without any changes
  5. (The Plan should show changes on global_secondary_index as described before)

Debug Output

(too long... omitting)

Panic Output

(no panic occurred)

Important Factoids

I found the root cause for this issue. I will submit a PR after filing this issue.

References

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html

Would you like to implement a fix?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAddresses a defect in current functionality.service/dynamodbIssues and PRs that pertain to the dynamodb service.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions