Search code examples
amazon-web-servicesterraformamazon-dynamodb

How do I import a DynamoDB table that has a replica in Terraform?


I created a DynamoDb table with the below script in terraform.

resource "aws_dynamodb_table" "GameScores" {
  name           = "GameScores"
  billing_mode   = "PAY_PER_REQUEST"
  hash_key       = "UserId"
  range_key      = "GameTitle"

  attribute {
    name = "UserId"
    type = "S"
  }

  attribute {
    name = "GameTitle"
    type = "S"
  }


  replica {
    region = us-east-2
    propagate_tags = true
  }

  tags {
    env = prod
  }
 
  stream_enabled = true
  stream_view_type = "NEW_AND_OLD_IMAGES" 
}

I don't have the Terraform state file and I want to import this resource.

I tried the below but it only imported the main table and not the replica:

terraform import aws_dynamodb_table.GameScores GameScores

When I do a terraform plan, I see that its going to delete the old replica and recreate a new replica.

How do I import this replica with the above terraform script? What steps and in what order do I need to execute to import the above resource?


Solution

  • Add the below to your current TF plan:

    lifecycle {
        ignore_changes = [replica]
    }
    

    and remove:

    replica {
        region = us-east-2
        propagate_tags = true
    }
    

    Create a new aws_dynamodb_table_replica resource and import it using the main table region e.g.

    terraform import aws_dynamodb_table_replica.GameScoresReplica GameScores:eu-west-1