How to Import Existing Resources Into Terraform in CI
A config-driven import block lets Terraform adopt an existing resource during a normal plan, which you can review on a PR before applying.
Add an import block with the target address and real resource id, then run terraform plan -generate-config-out=generated.tf to scaffold the config. Review the generated resource on the PR, then apply to record it in state.
import block
main.tf
import {
to = aws_s3_bucket.logs
id = "acme-prod-logs"
}Generate config in CI
Terminal
terraform plan -generate-config-out=generated.tf -input=falseGotchas
- Import blocks are planned like any change, so the import shows up in the PR plan for review.
- Generated config is a starting point; refine it before apply so future plans show no diff.
Related guides
How to Run terraform plan on Pull Requests in CIProduce a Terraform plan for every pull request in GitHub Actions with terraform plan -out, using read-only c…
How to Destroy Ephemeral Terraform Environments in CISpin up a per-pull-request Terraform environment and tear it down on PR close in GitHub Actions with a dedica…