Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Find the import ID. Every resource type has its own ID format — read the Import section at the end of the resource's page in the provider docs:
| Resource | Import ID |
|---|---|
aws_s3_bucket | Bucket name: acme-shop-logs |
aws_instance | Instance ID: i-0abc1234def567890 |
aws_security_group | sg-0123456789abcdef0 |
azurerm_resource_group | Full ID: /subscriptions/…/resourceGroups/rg-shop |
azurerm_storage_account | Full Azure resource ID |
- Write the import block (quick answer). Keep it in its own file (
import.tf) — you delete it after the import. - Let Terraform write the config (when you have none yet):
terraform plan -generate-config-out=generated.tfOpen generated.tf, clean it up: remove default values and read-only attributes, replace hard-coded IDs with references and variables, then move it to the right file.
- Plan until it says "0 to change":
terraform plan
aws_s3_bucket.logs: Preparing import... [id=acme-shop-logs]
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.If the plan wants to change or replace the object, your code does not match reality yet — adjust the code, not the object.
- Apply — this changes only the state, not the cloud object:
terraform apply
Apply complete! Resources: 1 imported, 0 added, 0 changed, 0 destroyed.Then delete import.tf (it is not needed any more) and commit the code.
- Many objects —
for_eachin import blocks (Terraform 1.7+):
locals {
buckets = ["acme-shop-logs", "acme-shop-media"]
}
import {
for_each = toset(local.buckets)
to = aws_s3_bucket.this[each.key]
id = each.key
}- The other direction — stop managing an object without deleting it (Terraform 1.7+):
removed {
from = aws_s3_bucket.old_reports
lifecycle {
destroy = false
}
}Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Cannot import non-existent remote object | Wrong ID, wrong region/account, or wrong provider alias. | Check the ID format and provider = / region. |
Resource already managed by Terraform | It is already in the state (maybe under another name). | terraform state list; use a moved block to rename instead. |
| The plan wants to replace the object after import | A "force new" attribute differs (name, AMI, location). | Make the code match the real value exactly. |
generated.tf has errors (conflicting arguments) | The generator writes all attributes, also ones that conflict. | Remove the conflicting or computed attributes by hand. |
| Imported, but sub-settings (bucket policy, versioning) show as new | In newer AWS providers these are separate resources. | Import aws_s3_bucket_versioning, _policy, etc. too. |
Old tutorials use terraform import ADDR ID | The old CLI command (still works, one at a time, no plan). | Prefer import blocks: reviewable in a pull request, with a plan. |
Go further
- Command pages: terraform, aws, az.
- Next how-tos: Plan and apply Terraform safely, Store Terraform state in S3 with locking.
- Learn it: Terraform — Import, Terraform — moved blocks, Terraform — State CLI, Terraform — Drift.
- Full guides: Terraform, OpenTofu.
- Practise: Lab: work with state — inspect, move, remove, import and find drift.
1What should terraform plan show before you apply an import?
1 to import, 0 to add, 0 to change, 0 to destroy — no changes to the real object.
2How do you stop managing a resource without deleting it?
A removed block with lifecycle { destroy = false } (Terraform 1.7+).