DEV Community

Cover image for 5.Associate Elastic IP with EC2 Instance Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

5.Associate Elastic IP with EC2 Instance Using Terraform

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

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

πŸ“Œ 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
}
Enter fullscreen mode Exit fullscreen mode

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


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)