Day 66: Terraform Unleashed: Building a Multi-Resource AWS Infrastructure❄
Day#66 Of 90 Days Of DevOps Challenge

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!
💥Introduction
Welcome back to Day 66 of our 90 Days of DevOps journey.
Today, we'll be diving into creating a Virtual Private Cloud (VPC) on Amazon Web Services (AWS) and deploying a web server within it.
This is a fundamental step in infrastructure as code (IaC) and cloud computing. So, let's get started!
📌Task 1: Create a VPC
Our first task is to create a VPC with a specific CIDR block. In Terraform, you can achieve this by defining a .tf file, in our case, vpc.tf, and adding the following code:
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "my_vpc"
}
}
Here's what this Terraform code does:
resource "aws_vpc" "my_vpc"defines the AWS VPC resource, named "my_vpc."cidr_block = "10.0.0.0/16"specifies the IP address range for the VPC, allowing instances with IP addresses in the range from 10.0.0.0 to 10.0.255.255.tagsset a tag for the VPC, making it easy to identify and organize within your AWS account.
To apply this configuration, run terraform init followed by terraform apply.
Create a provider.tf file in your Terraform project directory (if you haven't already) In the provider.tf file, add the AWS provider configuration
📌Task 2: Create a Private Subnet
Now that we have our VPC, let's create a private subnet within it. We'll define this in a separate .tf file, subnet.tf, with the following code:
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "private_subnet"
}
}
In this Terraform code:
resource "aws_subnet" "private_subnet"defines an AWS subnet resource, named "private_subnet," to be created within the previously defined VPC.vpc_id = aws_vpc.my_vpc.idspecifies the VPC in which this subnet will be created.cidr_block = "10.0.1.0/24"sets the CIDR block for the subnet's IP address range.
📌Task 3: Create a Public Subnet
Similarly, create a public subnet with the following CIDR block within the VPC. You can define it in the subnet.tf file created above.
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.2.0/24"
tags = {
Name = "public_subnet"
}
}
📌Task 4: Create an Internet Gateway
To allow internet access to our VPC, we'll need an Internet Gateway (IGW). Define this in a separate internetgateway.tf file with the following code:
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "internet_gateway_devops"
}
}
This code creates an Internet Gateway and attaches it to our VPC.
📌Task 5: Create a Route Table
Now, let's create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway. Define this in a routetable.tf file:
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
}
tags = {
Name = "public_route_table_devops"
}
}
resource "aws_route_table_association" "public_subnet_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
This code creates a route table with a default route pointing to the Internet Gateway and associates it with the public subnet.
To verify the route table, check the AWS console.
📌Task 6: Create a Security Group
We'll need a security group to control incoming and outgoing traffic. This security group allows SSH and HTTP access from anywhere. Define it in your securitygroup.tf file:
resource "aws_security_group" "web_server_sg" {
name_prefix = "web-server-sg"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
}
This code creates a security group named "web_server_sg" allowing SSH and HTTP access.
📌Task 7: Create an Elastic IP
An Elastic IP is essential for associating with your EC2 instance. Define it in your elasticip.tf file:
resource "aws_eip" "my_eip" {
instance = aws_instance.my_server.id
vpc = true
tags = {
Name = "elastic-ip"
}
}
This code associates an Elastic IP with your EC2 instance.
📌Task 8: Create an EC2 Instance
Finally, let's launch an EC2 instance in the public subnet. Define it in your ec2.tf file:
resource "aws_instance" "my_server" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
key_name = "aws-keys"
subnet_id = aws_subnet.public_subnet.id
security_groups = [
aws_security_group.web_server_sg.id
]
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<html><body><h1>Hello IaC Project</h1></body></html>" > /var/www/html/index.html
sudo systemctl restart apache2
EOF
tags = {
Name = "my_web_server"
}
}
This code launches an EC2 instance with an Apache web server installed.
After applying these configurations, open the public IP in your web browser to verify the website is hosted successfully.
🌈Conclusion
Today, we've achieved a significant milestone in our DevOps journey by creating a VPC, subnets, Internet Gateway, route table, security group, Elastic IP, and an EC2 instance with a web server.
This is a foundational setup for future deployments and infrastructure management.
Happy learning & may your DevOps journey be filled with exciting discoveries🎊
Thank you for joining us on this exciting Day 66 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashnode.dev
LinkedIn: linkedin.com/in/vishalphadnis




