DEV Community

Cover image for βœ… TASK 4: Terraform Import β€” Importing an Existing EC2 Instance Into Terraform
Latchu@DevOps
Latchu@DevOps

Posted on

βœ… 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:

  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)
Enter fullscreen mode Exit fullscreen mode

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
  }
}
Enter fullscreen mode Exit fullscreen mode

🧱 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
}
Enter fullscreen mode Exit fullscreen mode

🧱 Step 3 β€” terraform init

terraform init -reconfigure
Enter fullscreen mode Exit fullscreen mode

This loads S3 backend + DynamoDB locking.


🧱 Step 4 β€” Get your EC2 Instance ID

Pick any existing EC2 instance.

Example ID:

i-0f7231d1dbe6f446b
Enter fullscreen mode Exit fullscreen mode

🧱 Step 5 β€” Import the EC2 instance

Run:

terraform import aws_instance.imported i-0f7231d1dbe6f446b
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

🧱 Step 6 β€” Show the imported state

terraform state show aws_instance.imported
Enter fullscreen mode Exit fullscreen mode

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)