6.Launch EC2 Instance from Custom AMI Using Terraform

Published: (February 7, 2026 at 12:53 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

Lab Information

The Nautilus DevOps team needs to create an AMI from an existing EC2 instance for backup and scaling purposes.

  • Existing EC2 instance name: devops-ec2
  • Desired AMI name: devops-ec2-ami
  • New EC2 instance name: devops-ec2-new (must be launched from the AMI)

Update the main.tf file (do not create a separate .tf file) to:

  1. Provision the AMI from the existing instance.
  2. Launch a new EC2 instance using that AMI.

Create an outputs.tf file that outputs:

  • KKE_ami_id – the ID of the AMI created.
  • KKE_new_instance_id – the ID of the new EC2 instance.

Lab Solutions

main.tf

# 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"
  }
}

outputs.tf

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

output "KKE_new_instance_id" {
  value = aws_instance.devops_ec2_new.id
}

Terraform Commands (MANDATORY)

Run the following commands in order:

terraform init
terraform validate
terraform apply
# Type "yes" when prompted

Terraform apply output

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

What is an AMI?

An AMI (Amazon Machine Image) is a snapshot + blueprint of an EC2 instance. It includes:

  • Operating system
  • Installed software
  • Configuration
  • Disk data

Why create an AMI?

  • Backup an EC2 instance
  • Create identical servers quickly
  • Scale out efficiently
  • Enable disaster recovery

What the Terraform code does

  1. Find the existing EC2

    data "aws_instance" "existing_ec2" { … }

    Terraform reads the instance whose Name tag is devops-ec2. No new resources are created here.

  2. Create an AMI from that EC2

    resource "aws_ami_from_instance" "devops_ami" { … }

    Terraform tells AWS to freeze the running instance’s disk state and generate an AMI. This step may take a few minutes.

  3. Launch a new EC2 from the AMI

    resource "aws_instance" "devops_ec2_new" { … }

    Terraform creates a brand‑new instance using the AMI, resulting in the same OS, software, and configuration as the original.

How Terraform determines the correct order

Terraform builds a dependency graph:

existing EC2 → AMI → new EC2

Because the AMI resource depends on the existing instance and the new EC2 depends on the AMI, Terraform automatically executes the steps in the right sequence.

What happens during terraform apply

  1. Terraform reads the existing EC2.
  2. AWS creates the AMI.
  3. Terraform waits until the AMI is available.
  4. AWS launches the new EC2 from the AMI.
  5. Terraform records everything in the state file.
  6. The defined outputs display the AMI ID and the new instance ID.

Why the outputs matter

  • Confirm that the AMI was successfully created.
  • Confirm that the new EC2 instance was launched.
  • KodeKloud (and other graders) use these outputs for validation, saving you from manually checking the AWS console.

Common Mistakes to Avoid

  • Hard‑coding the instance ID.
  • Creating extra .tf files (the lab requires a single main.tf).
  • Launching the new EC2 before the AMI is ready.
  • Forgetting to define the required outputs.
  • Using incorrect output variable names (KKE_ami_id, KKE_new_instance_id).

Resources & Next Steps

Credits
All labs are from: KodeKloud

0 views
Back to Blog

Related posts

Read more »

14.Provision IAM User with Terraform

Lab Information The Nautilus DevOps team is experimenting with Terraform provisioners. Your task is to create an IAM user and use a local-exec provisioner to l...

8.Sync Data to S3 Bucket with Terraform

!Cover image for 8.Sync Data to S3 Bucket with Terraformhttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F...