Zum Hauptinhalt springen
•
Linux Tipps & HardeningFind your fighting spirit with the Humble Indie Fighters Bundle(08.10.2026 um 09:53 Uhr)
••
Linux Tipps & HardeningSo is this actually legit?(08.10.2026 um 09:46 Uhr)
•
KI & AI VideosJulian Goldie SEO: Herald OS: FREE Hermes Agent OS!(08.10.2026 um 05:01 Uhr)
•
KI & AI VideosJulian Goldie SEO: Starnet Just Changed AI Agents Forever(08.10.2026 um 06:00 Uhr)
•
AI & KI NachrichtenJulian Goldie SEO: GPT-6 Intelligent UI is INSANE!(08.10.2026 um 07:00 Uhr)
•••••
Linux Tipps & HardeningFind your fighting spirit with the Humble Indie Fighters Bundle(08.10.2026 um 09:53 Uhr)
••
Linux Tipps & HardeningSo is this actually legit?(08.10.2026 um 09:46 Uhr)
•
KI & AI VideosJulian Goldie SEO: Herald OS: FREE Hermes Agent OS!(08.10.2026 um 05:01 Uhr)
•
KI & AI VideosJulian Goldie SEO: Starnet Just Changed AI Agents Forever(08.10.2026 um 06:00 Uhr)
•
AI & KI NachrichtenJulian Goldie SEO: GPT-6 Intelligent UI is INSANE!(08.10.2026 um 07:00 Uhr)
••••
Intelligence View
⚡ tsecurity.de Intelligence

✅ TASK 4: Terraform Import — Importing an Existing EC2 Instance Into Terraform

📌 TASK REQUIREMENTS We already have: S3 backend → tf-backend-lab-123 DynamoDB lock table → tf-state-lock You will now: Choose ANY existing EC2 instance in your …

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!




📌 TASK REQUIREMENTS



We already have:




  • S3 backend → tf-backend-lab-123

  • DynamoDB lock table → tf-state-lock



You will now:




  1. Choose ANY existing EC2 instance in your AWS account

  2. Create import-tutorial/ folder

  3. Configure backend

  4. Write a minimal EC2 resource (empty shell)

  5. Run terraform import

  6. Run terraform plan → fix drift

  7. Generate the correct Terraform code for the imported EC2









🏗 Directory Structure






import-tutorial/
main.tf
backend.tf
outputs.tf
variables.tf (optional)






1






🧱 Step 1 — backend.tf




terraform {
backend "s3" {
bucket = "tf-backend-lab-123"
key = "import/ec2/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "tf-state-lock"
encrypt = true
}
}









🧱 Step 2 — main.tf (EMPTY resource block)



Before import, Terraform requires only the resource address, not full config.




provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "imported" {
# no arguments yet
}









🧱 Step 3 — terraform init




terraform init -reconfigure






This loads S3 backend + DynamoDB locking.






🧱 Step 4 — Get your EC2 Instance ID



Pick any existing EC2 instance.



Example ID:




i-0f7231d1dbe6f446b









🧱 Step 5 — Import the EC2 instance



Run:




terraform import aws_instance.imported i-0f7231d1dbe6f446b






Expected output:




 terraform import aws_instance.imported i-0f7231d1dbe6f446b
aws_instance.imported: Importing from ID "i-0f7231d1dbe6f446b"...
aws_instance.imported: Import prepared!
Prepared aws_instance for import
aws_instance.imported: Refreshing state... [id=i-0f7231d1dbe6f446b]

Import successful!









🧱 Step 6 — Show the imported state




terraform state show aws_instance.imported






2



This prints the actual properties AWS returned.






🧱 Step 7 — Build the final configuration



Now copy the relevant attributes from state show into main.tf.



⚠ Important:



You only include arguments you want Terraform to manage — not all attributes.



Example minimal config (after import):




provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "imported" {
ami = "ami-0ecb62995f68bb549"
instance_type = "t3.micro"
subnet_id = "subnet-042e22fd675a9bc09"
vpc_security_group_ids = ["sg-04c2b586b63bfc79c"]
key_name = "terraform-key"

tags = {
Name = "import-instance"
}
}






Remove read-only attributes:




  • arn

  • host_id

  • password_data

  • primary_network_interface_id

  • private_dns

  • etc






🧱 Step 8 — Run terraform plan (Very Important)




terraform plan






Terraform will:




  • detect missing arguments

  • detect drift

  • show changes required to adopt the resource



Your job:



✔ update main.tf until terraform plan shows NO CHANGES

This proves Terraform fully manages the resource.



3







🎯 Final Goal



Your output after all corrections:




No changes. Infrastructure is up-to-date.






This means your EC2 instance is successfully imported and managed by Terraform.









🧠 Interview Notes: What You Should Explain



You should be able to answer:



What is the process to import a resource?



Explain 3 steps:




  • Write a placeholder resource block

  • Run terraform import

  • Copy attributes into the final Terraform code



Does terraform import write the configuration?



No. Only state.

You must manually write the .tf file.



What happens if config and imported state differ?



→ terraform plan will propose changes.



Should you import everything?



Only when:




  • Migrating existing infra to IaC

  • Auditing a legacy environment

  • Adopting manually created resources






🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.






— Latchu | Senior DevOps & Cloud Engineer



☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation

📌 Sharing hands-on guides, best practices & real-world cloud solutions

Intelligence Digest — kostenlos Täglich die wichtigsten Security-News · jederzeit abbestellbar
Community Rating & Social Proof
⭐ Leser-Wertung
–
Ø / 5.0
Noch keine Leser-Bewertungen
War diese Seite hilfreich?
0
Jetzt Bewertung abgeben
🟢 Niedrig (1-3.9) 🟡 Mittel (4.0-6.9) 🔴 Hoch (7.0-8.9) Kritisch (9.0-10.0)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten ✅ TASK 4: Terraform Import — Importing an Existing EC2 Instance Into Terraform

Thematisch verwandte Begriffe: TASK, Terraform, Import, Importing · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
Nächster Beitrag