Search code examples
amazon-web-servicesterraformterraform-provider-awsterraform-modules

Unable to import Dynamo table using terraform-aws-dynamodb-table module


I am new to Terraform and trying to get my way through it.

I am using the terraform-aws-modules/dynamodb-table/aws module to create DynamoDB tables. I am trying to import an existing table.

Here is my import code:

import {
  to = module.test_table.aws_dynamodb_table.this
  id = "test-table"
}

module "test_table" {
  source       = "terraform-aws-modules/dynamodb-table/aws"
  version      = "4.0.1"
  billing_mode = "PAY_PER_REQUEST"
  name         = "test-table"
  hash_key     = "id"
  attributes = [
    { "name" = "id", "type" = "S" }
  ]
}

The table exists in my AWS account and can be imported as an aws_dynamodb_table resource. However, I get the following error when I try to import it using the terraform-aws-modules/dynamodb-table/aws module:

The configuration for the given import module.test_table.aws_dynamodb_table.this does not exist. All target instances must have an associated configuration to be imported.

Any recommendations or help would be highly appreciated. Thanks!

Import existing Dynamo Table to terraform state file using terraform-aws-modules/dynamodb-table/aws module


Solution

  • This module uses count on the resource level (over here) so you need to specify the index of a resource in the target argument in the import block:

    import {
      to = module.test_table.aws_dynamodb_table.this[0]
      id = "test-table"
    }