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

Top comments (0)