AWSElastic Load Balancingaws_lb_target_group_attachmentPro

How to Import an AWS Target Group Attachment into Terraform

To import an existing AWS Target Group Attachment into Terraform, scan it with Terraback and run terraback aws import --method bulk. Terraback writes the matching aws_lb_target_group_attachment resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is not importable: aws_lb_target_group_attachment has no terraform import support; Terraback emits the resource block and you run terraform apply to register the target (re-registering an already-attached target is a safe no-op).

Import AWS Target Group Attachment with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the Elastic Load Balancing resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live AWS Target Group Attachment to managed Terraform.

1

Scan your AWS account

terraback scan all aws --region us-east-1
2

Generate import blocks and import into state

terraback aws import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

# aws_lb_target_group_attachment does not support "terraform import".
# Terraback generates the resource block below; run "terraform apply" to
# register it in state. Re-registering an already-attached target is a
# harmless no-op, so this is safe to run against live infrastructure.

Example aws_lb_target_group_attachment configuration

Here is a realistic AWS Target Group Attachment block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "aws_lb_target_group_attachment" "api" {
  target_group_arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/api/0a1b2c3d"
  target_id        = "i-1234567890abcdef0"
  port             = 8080
}

Gotchas when importing a AWS Target Group Attachment

  • This resource cannot be imported with terraform import; add the block and apply. Registering a target that is already registered is idempotent and safe.
  • target_id varies by target type: an instance ID for instance targets, a private IP for ip targets, or a Lambda function ARN for lambda targets.
  • port is required for instance and ip targets but must be omitted for lambda targets.
  • The attachment is separate from the aws_lb_target_group; the target group and its health checks are their own resource.

Doing it manually with terraform import

The native approach is to write the aws_lb_target_group_attachment block by hand, then run terraform import aws_lb_target_group_attachment.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole AWS account in minutes

Terraback scans 80+ AWS resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.