متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 49IaC · Terraform

How to import an existing resource into Terraform

Someone created a bucket, a VM or a DNS zone by hand in the console — now it should be managed by Terraform, without deleting and creating it again. Import connects the existing object to a resource in your code and writes it into the state. Since Terraform 1.5 you do it with an import block, and Terraform can even write the code for you. This how-to imports a real-world example step by step and checks that nothing will change.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

49.1

The quick answer

import.tf
import {
  to = aws_s3_bucket.logs
  id = "acme-shop-logs"
}
terraform plan -generate-config-out=generated.tf
terraform plan
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
terraform apply

الأوامر في هذا الدرس

49.2

Step by step

  1. 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:
ResourceImport ID
aws_s3_bucketBucket name: acme-shop-logs
aws_instanceInstance ID: i-0abc1234def567890
aws_security_groupsg-0123456789abcdef0
azurerm_resource_groupFull ID: /subscriptions/…/resourceGroups/rg-shop
azurerm_storage_accountFull Azure resource ID
  1. Write the import block (quick answer). Keep it in its own file (import.tf) — you delete it after the import.
  2. Let Terraform write the config (when you have none yet):
terraform plan -generate-config-out=generated.tf

Open 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.

  1. 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.

  1. 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.

  1. Many objects — for_each in import blocks (Terraform 1.7+):
import.tf
locals {
  buckets = ["acme-shop-logs", "acme-shop-media"]
}
 
import {
  for_each = toset(local.buckets)
  to       = aws_s3_bucket.this[each.key]
  id       = each.key
}
  1. The other direction — stop managing an object without deleting it (Terraform 1.7+):
removed.tf
removed {
  from = aws_s3_bucket.old_reports
  lifecycle {
    destroy = false
  }
}

الأوامر في هذا الدرس

49.3

Check that it worked

terraform state list | grep aws_s3_bucket.logs
terraform plan
No changes. Your infrastructure matches the configuration.

49.4

Common problems

ProblemCauseFix
Cannot import non-existent remote objectWrong ID, wrong region/account, or wrong provider alias.Check the ID format and provider = / region.
Resource already managed by TerraformIt 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 importA "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 newIn newer AWS providers these are separate resources.Import aws_s3_bucket_versioning, _policy, etc. too.
Old tutorials use terraform import ADDR IDThe old CLI command (still works, one at a time, no plan).Prefer import blocks: reviewable in a pull request, with a plan.

الأوامر في هذا الدرس

49.5

Go further

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+).

كل الأوامر في هذا الفصل