DEV Community

Cover image for 6.Launch EC2 Instance from Custom AMI Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

6.Launch EC2 Instance from Custom AMI Using Terraform

Lab Information

The Nautilus DevOps team needs to create an AMI from an existing EC2 instance for backup and scaling purposes. The following steps are required:

They have an existing EC2 instance named devops-ec2.

They need to create an AMI named devops-ec2-ami from this instance.

Additionally, they need to launch a new EC2 instance named devops-ec2-new using this AMI.

Update the main.tf file (do not create a different or separate.tf file) to provision an AMI and then launch an EC2 Instance from that AMI.

Create an outputs.tf file to output the following values:
    KKE_ami_id for the AMI ID you created.
    KKE_new_instance_id for the EC2 instance ID you created.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

1️⃣ main.tf

πŸ‘‰ Only one file for resources (as required)

# Step 1: Read the existing EC2 instance
data "aws_instance" "existing_ec2" {
  filter {
    name   = "tag:Name"
    values = ["devops-ec2"]
  }
}

# Step 2: Create AMI from existing EC2
resource "aws_ami_from_instance" "devops_ami" {
  name               = "devops-ec2-ami"
  source_instance_id = data.aws_instance.existing_ec2.id
}

# Step 3: Launch a new EC2 from the AMI
resource "aws_instance" "devops_ec2_new" {
  ami           = aws_ami_from_instance.devops_ami.id
  instance_type = "t2.micro"

  tags = {
    Name = "devops-ec2-new"
  }
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ outputs.tf

πŸ‘‰ Exact output variable names (grader-sensitive)

output "KKE_ami_id" {
  value = aws_ami_from_instance.devops_ami.id
}

output "KKE_new_instance_id" {
  value = aws_instance.devops_ec2_new.id
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ Terraform Commands (MANDATORY)

Run in this order:

terraform init

terraform validate

terraform apply

Type:

yes


Simple Step-by-Step Explanation (Why & What Happens)

Now let’s understand this slowly and clearly, no jargon.

πŸ”Ή What is an AMI (simple words)?

Think of an AMI as:

πŸ“¦ A snapshot + blueprint of an EC2

It contains:

OS

Installed software

Configuration

Disk data

πŸ”Ή Why create an AMI?

Backup an EC2

Create identical servers

Scale quickly

Disaster recovery

That’s why DevOps teams love AMIs.

πŸ”Ή Step-by-step: What this Terraform code does
🟒 Step 1: Find the existing EC2
data "aws_instance" "existing_ec2"

πŸ‘‰ Terraform is saying:

β€œAWS, show me the EC2 whose Name tag is devops-ec2.”

πŸ“Œ No EC2 is created here
It only reads an existing one.

🟒 Step 2: Create an AMI from that EC2
aws_ami_from_instance

πŸ‘‰ Terraform tells AWS:

β€œTake this running EC2 and make an image from it.”

AWS then:

Freezes disk state

Creates an AMI

Gives it an AMI ID

⏳ This step takes some time (normal).

🟒 Step 3: Launch a new EC2 from the AMI
resource "aws_instance" "devops_ec2_new"

πŸ‘‰ Terraform now says:

β€œCreate a brand-new EC2 using that AMI.”

Result:

Same OS

Same software

Same configuration

Brand new instance ID

πŸ”Ή How Terraform knows the correct order?

Because of this chain πŸ‘‡

existing EC2 β†’ AMI β†’ new EC2

Terraform sees:

AMI depends on EC2

New EC2 depends on AMI

So it automatically executes in the right order.

πŸ”Ή What happens during terraform apply?

1️⃣ Terraform finds existing EC2
2️⃣ AWS creates AMI
3️⃣ Terraform waits until AMI is ready
4️⃣ AWS launches new EC2 from AMI
5️⃣ Terraform saves everything in state
6️⃣ Outputs show IDs

πŸ”Ή Why outputs matter here?

Confirms AMI was created

Confirms new EC2 was launched

KodeKloud uses outputs for validation

Saves you from opening AWS Console

🧠 Easy Memory Trick

data β†’ read existing things

resource β†’ create new things

AMI β†’ EC2 template

state β†’ Terraform memory

🚨 Common Mistakes

❌ Hardcoding instance ID
❌ Creating extra .tf files
❌ Launching EC2 before AMI
❌ Forgetting outputs
❌ Wrong output variable names


Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)