Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC) Techniques(Interview Questions)
Today, we embark on an exciting hands-on project that will deepen your understanding of Terraform and Infrastructure as Code (IaC) techniques. In previous tasks, you learned the fundamentals of Terraform and created an EC2 instance. Today, we’re taking it up a notch by building a complete AWS infrastructure from scratch.
Task Overview
In this project, we’ll use Terraform to create the following AWS resources:
VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16
Public subnet with CIDR block 10.0.1.0/24 in the VPC.
Private subnet with CIDR block 10.0.2.0/24 in the VPC.
Internet Gateway (IGW) and attach it to the VPC.
Route table for the public subnet and associate it with the public subnet, including a route to the Internet Gateway.
EC2 instance in the public subnet with specific details.
Elastic IP and associate it with the EC2 instance.
Verify that the website is successfully hosted on the EC2 instance.
Let’s Dive into the Details
Creating a VPC
We begin by defining our VPC with a CIDR block of 10.0.0.0/16. This command in your Terraform configuration file will create the VPC:
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
Creating Subnets
Next, we’ll create both a public and a private subnet within our VPC. The public subnet will have the CIDR block 10.0.1.0/24, and the private subnet will use 10.0.2.0/24.
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.2.0/24"
}
Internet Gateway and Route Table
To enable internet access for resources in our public subnet, we need to create an Internet Gateway and associate it with the VPC. Additionally, we must define a route table that directs traffic to the Internet Gateway.
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
}
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.my_vpc.id route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
}
Launching an EC2 Instance
Now, let’s create an EC2 instance in our public subnet with the specified configuration, including installing Apache and hosting a simple website using user data.
resource "aws_instance" "my_instance" {
ami = "ami-0557a15b87f6559cf"
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet.id
security_groups = [aws_security_group.my_security_group.name] user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
service httpd start
echo "<html><h1>Hello from your Terraform EC2 instance!</h1></html>" > /var/www/html/index.html
EOF
}
Elastic IP
To ensure our EC2 instance has a static IP address, we’ll create an Elastic IP and associate it with the instance.
resource "aws_eip" "my_eip" {
instance = aws_instance.my_instance.id
}
Verifying the Websit
Once you’ve applied these configurations using terraform apply
, Terraform will create the specified infrastructure in your AWS account. You can retrieve the public IP address of your EC2 instance using aws_
instance.my
_instance.public_ip
in your Terraform scripts or check the AWS Management Console.
Open a web browser and navigate to the Elastic IP address associated with your EC2 instance. You should see a simple webpage displaying “Hello from your Terraform EC2 instance!”
Conclusion
Congratulations! You’ve successfully built a multi-component AWS infrastructure using Terraform and practiced essential IaC techniques. This project not only strengthens your Terraform skills but also provides a practical demonstration of how IaC can simplify and automate infrastructure management. Continue exploring and experimenting with Terraform to deepen your understanding of this powerful tool in the world of cloud infrastructure. Happy coding! ☁️🚀