Lesson 22 / 25
Importing Existing Resources
Bring hand-made infrastructure under management.
import blocks and generated config
Resources created by hand or by other tools can be adopted with an import block (to the address, id the provider's identifier). terraform plan -generate-config-out=generated.tf can draft the matching configuration, which you then clean up. The plan shows imports before apply; after applying, Terraform manages the resource. Import one resource or group at a time, and check the plan shows no unintended changes after the import.
Importing an existing bucket (sketch)
Not run here; it needs a cloud account or a remote backend.
import {
to = aws_s3_bucket.assets
id = "acme-shop-assets"
}
# then:
# terraform plan -generate-config-out=generated.tf
# review generated.tf, move it into your code, plan until "no changes"Aim for "no changes" after import
Adjust the configuration until the plan after import shows no changes, proving the code matches reality.
Quick check: What does an import block do?
- Downloads a module
- Creates a new copy of the resource
- Deletes the resource
- Adopts an existing real resource into Terraform state at a given address
Answer
Adopts an existing real resource into Terraform state at a given address — Bring existing infrastructure under management.