Lab Information
The Nautilus DevOps Team has received a new request from the Development Team to set up a new EC2 instance. This instance will be used to host a new application that requires a stable IP address. To ensure that the instance has a consistent public IP, an Elastic IP address needs to be associated with it. This setup will help the Development Team to have a reliable and consistent access point for their application.
Create an EC2 instance named xfusion-ec2 using any Linux AMI like Ubuntu.
Instance type must be t2.micro and associate an Elastic IP address named xfusion-eipwith this instance.
Use the main.tf file (do not create a separate .tf file) to provision the EC2-Instance and Elastic IP.
Use the outputs.tf file and output the instance name using variable KKE_instance_name and the Elastic IP using variable KKE_eip.
Lab Solutions
1οΈβ£ main.tf
π Only resources here (as required)
resource "aws_instance" "xfusion_ec2" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
tags = {
Name = "xfusion-ec2"
}
}
resource "aws_eip" "xfusion_eip" {
instance = aws_instance.xfusion_ec2.id
tags = {
Name = "xfusion-eip"
}
}
π Notes:
Ubuntu/Linux AMI used
EC2 name = xfusion-ec2
Elastic IP name = xfusion-eip
EIP directly attached to EC2
2οΈβ£ outputs.tf
output "KKE_instance_name" {
value = aws_instance.xfusion_ec2.tags["Name"]
}
output "KKE_eip" {
value = aws_eip.xfusion_eip.public_ip
}
3οΈβ£ Terraform Commands (MANDATORY)
Run these in order:
terraform init
terraform validate
terraform apply
Type:
yes
Simple Step-by-Step Explanation (Why & What Happens)
Letβs break it down slow and simple π
πΉ Why do we need an Elastic IP?
Normal EC2 public IPs change when instance stops/restarts
Elastic IP (EIP) is static
Application always uses the same IP
Thatβs why the dev team asked for it
πΉ Why create EC2 first?
Terraform works like:
βI need a server before I can give it an IP.β
So:
EC2 is created
Then Elastic IP is attached to it
Terraform figures this order automatically because:
instance = aws_instance.xfusion_ec2.id
πΉ What happens during terraform apply?
Step 1: Terraform reads your code
Sees EC2
Sees Elastic IP
Step 2: Terraform talks to AWS
Requests a t2.micro EC2
Uses Linux AMI
Names it xfusion-ec2
Step 3: Elastic IP is created
AWS allocates a static public IP
Terraform attaches it to the EC2
Step 4: Terraform saves state
Stores EC2 ID
Stores EIP ID
Knows they are connected
Step 5: Outputs are printed
EC2 name shown
Elastic IP shown
πΉ Why outputs are important?
Outputs also help humans quickly see results
No need to open AWS Console
π§© Mental Model (Easy to Remember)
Think like this:
EC2 = π₯οΈ server
Elastic IP = π permanent address
Terraform = π€ automation robot
State file = π memory book
π¨ Common Mistakes
β Using normal public IP instead of EIP
β Forgetting to attach EIP
β Wrong output variable names
β Creating extra .tf files
β Forgetting terraform apply

Top comments (0)