terraform import: Usage, Options & Common CI Errors
Bring resources that already exist under Terraform management.
terraform import associates an existing real-world object with a Terraform resource address in state, so Terraform starts managing it instead of trying to create a duplicate.
What it does
terraform import <address> <id> reads the existing object identified by <id> and records it in state under <address>. You must still write matching configuration for the resource. As of Terraform 1.5+ you can also declare import {} blocks and generate config with terraform plan -generate-config-out.
Common usage
# Import an existing EC2 instance into a resource you have already written
terraform import aws_instance.web i-0abc123def456
# Import into a module / for_each instance
terraform import 'module.db.aws_db_instance.this' mydb-prod
# Declarative import (Terraform 1.5+): add a block, then plan
# import {
# to = aws_s3_bucket.logs
# id = "my-logs-bucket"
# }
terraform plan -generate-config-out=generated.tfCommon error in CI: resource address requires config / already managed
import fails with "Error: resource address ... does not exist in the configuration" when you import before writing the resource block, or "Resource already managed" when it is already in state. Fix: write the resource block first (even a stub), then import; check terraform state list to confirm it is not already tracked. After import, run plan and reconcile any drift between the real object and your config until the plan is clean.
Key options
| Option | Purpose |
|---|---|
| <address> <id> | Target address and provider-specific ID |
| -var / -var-file | Variables needed to evaluate the config |
| -input=false | Never prompt (required in CI) |