Terraform: Unable to import instances created using "count" within a module

Created on 7 Sep 2018  ยท  4Comments  ยท  Source: hashicorp/terraform

Terraform Version

$ terraform -v
Terraform v0.11.8
+ provider.aws v1.35.0
+ provider.random v2.0.0

Terraform Configuration Files

The resource was called from the module https://github.com/claranet/terraform-aws-aurora and I am trying to run terraform import on the created resource

module "services_storage_db" {
  source                          = "[email protected]:claranet/terraform-aws-aurora.git"
  engine                          = "aurora-postgresql"
  engine-version                  = "9.6.9"
  name                            = "services-storage-db"
  port                            = "5432"
  envname                         = "services-storage"
  envtype                         = "staging"
  subnets                         = ["subnet-0xxxxx", "subnet-0xxxxx"]
  azs                             = ["us-east-1a", "us-east-1b"]
  replica_count                   = "2"
  security_groups                 = ["sg-0xxxxx"]
  instance_type                   = "db.r4.large"
  username                        = "<username>"
  password                        = "<password>"
  backup_retention_period         = "14"
  final_snapshot_identifier       = "services-storage-final-db-snapshot"
  storage_encrypted               = "true"
  publicly_accessible             = "true"
  apply_immediately               = "true"
  monitoring_interval             = "10"
  cw_alarms                       = false
  db_parameter_group_name         = "${aws_db_parameter_group.services_storage_db_postgres96_parameter_group.id}"
  db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.services_storage_cluster_postgres96_parameter_group.id}"
}
// Create 'n' number of additional DB instance(s) in same cluster
resource "aws_rds_cluster_instance" "cluster_instance_n" {
  depends_on                   = ["aws_rds_cluster_instance.cluster_instance_0"]
  count                        = "${var.replica_scale_enabled ? var.replica_scale_min : var.replica_count}"
  engine                       = "${var.engine}"
  engine_version               = "${var.engine-version}"
  identifier                   = "${var.identifier_prefix != "" ? format("%s-node-%d", var.identifier_prefix, count.index + 1) : format("%s-aurora-node-%d", var.envname, count.index + 1)}"
  cluster_identifier           = "${aws_rds_cluster.default.id}"
  instance_class               = "${var.instance_type}"
  publicly_accessible          = "${var.publicly_accessible}"
  db_subnet_group_name         = "${aws_db_subnet_group.main.name}"
  db_parameter_group_name      = "${var.db_parameter_group_name}"
  preferred_maintenance_window = "${var.preferred_maintenance_window}"
  apply_immediately            = "${var.apply_immediately}"
  monitoring_role_arn          = "${join("", aws_iam_role.rds-enhanced-monitoring.*.arn)}"
  monitoring_interval          = "${var.monitoring_interval}"
  auto_minor_version_upgrade   = "${var.auto_minor_version_upgrade}"
  promotion_tier               = "${count.index + 1}"

  tags {
    envname = "${var.envname}"
    envtype = "${var.envtype}"
  }
}

Expected Behavior

When I run terraform import module.services_storage_db.aws_rds_cluster_instance.cluster_instance_n[0] services-storage-aurora-node-1 , I expect the resource to be imported into my state file.

Actual Behavior

I getv the response no matches found: module.services_storage_db.aws_rds_cluster_instance.cluster_instance_n[0]

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. Empty the state file
  4. terraform import module.services_storage_db.aws_rds_cluster_instance.cluster_instance_n[0] services-storage-aurora-node-1

References

This issue was addressed here: #8149, but it seems to still not work for modules.

bug core

Most helpful comment

@Ofiliojo

I found the solution. Just quote, single, the string after the import command, like this:

terraform import 'module.eip.aws_eip.eip[0]' eipalloc-01d74f8a3c52b8361
terraform import 'module.eip.aws_eip.eip[1]' eipalloc-03f1b063d22feb22f
terraform import 'module.eip.aws_eip.eip[2]' eipalloc-0bcb4e736c21ed5cd

Response:

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[0]' eipalloc-01d74f8a3c52b8361
module.eip.aws_eip.eip: Importing from ID "eipalloc-01d74f8a3c52b8361"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-01d74f8a3c52b8361)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-01d74f8a3c52b8361)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[1]' eipalloc-03f1b063d22feb22f
module.eip.aws_eip.eip: Importing from ID "eipalloc-03f1b063d22feb22f"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-03f1b063d22feb22f)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-03f1b063d22feb22f)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[2]' eipalloc-0bcb4e736c21ed5cd
module.eip.aws_eip.eip: Importing from ID "eipalloc-0bcb4e736c21ed5cd"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-0bcb4e736c21ed5cd)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-0bcb4e736c21ed5cd)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ—  

All 4 comments

I was able to do this without a glitch. ๐Ÿค” I also have an RDS cluster with 3 instances. I wonder if the count.index has to alight with the imported instance name

I am using latest terraform and:

  • provider.aws: version = "~> 1.36"

@Ofiliojo

I found the solution. Just quote, single, the string after the import command, like this:

terraform import 'module.eip.aws_eip.eip[0]' eipalloc-01d74f8a3c52b8361
terraform import 'module.eip.aws_eip.eip[1]' eipalloc-03f1b063d22feb22f
terraform import 'module.eip.aws_eip.eip[2]' eipalloc-0bcb4e736c21ed5cd

Response:

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[0]' eipalloc-01d74f8a3c52b8361
module.eip.aws_eip.eip: Importing from ID "eipalloc-01d74f8a3c52b8361"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-01d74f8a3c52b8361)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-01d74f8a3c52b8361)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[1]' eipalloc-03f1b063d22feb22f
module.eip.aws_eip.eip: Importing from ID "eipalloc-03f1b063d22feb22f"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-03f1b063d22feb22f)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-03f1b063d22feb22f)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ— terraform import 'module.eip.aws_eip.eip[2]' eipalloc-0bcb4e736c21ed5cd
module.eip.aws_eip.eip: Importing from ID "eipalloc-0bcb4e736c21ed5cd"...
module.eip.aws_eip.eip: Import complete!
  Imported aws_eip (ID: eipalloc-0bcb4e736c21ed5cd)
module.eip.aws_eip.eip: Refreshing state... (ID: eipalloc-0bcb4e736c21ed5cd)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

โžœ  management git:(master) โœ—  

That worked. Thank you! @aaomoware

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings