BlogIaC

Importing existing infrastructure into Terraform state

Bring click-ops resources under Terraform with import blocks and generated config — without recreating them.

Sep 10, 2024·8 min readIntermediate

Every team has infrastructure that was clicked into existence before Terraform. You do not have to recreate it — import brings existing resources under management by writing them into state. Since Terraform 1.5, import blocks even generate the starting config for you.

Declare what to import

import.tf
import {
to = aws_s3_bucket.logs
id = "acme-app-logs" # the real resource identifier
}

Generate the config

bash — let Terraform write itlive
terraform plan -generate-config-out=generated.tf
aws_s3_bucket.logs: will be imported
wrote resource "aws_s3_bucket" "logs" to generated.tf
Review the generated HCL
Generated config is a literal dump of current state — it includes defaults and may miss lifecycle rules. Read it, tidy it, and confirm the next plan shows no changes.

The pre-1.5 way

bash
# write the resource block by hand first, then:
terraform import aws_s3_bucket.logs acme-app-logs

Whichever path you take, the goal is the same: a clean terraform plan with no changes afterwards. That is proof state matches reality.

Go deeper in a courseTerraformState, import, and bringing real infrastructure under IaC.View course

Related posts