🚀TERRAFORM Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)☁️
Table of contents
Welcome to Day 66 of the #90DaysOfDevOps Challenge. Today, we have an exciting hands-on project that will put your Terraform skills to the test. You will be creating your own AWS infrastructure using Infrastructure as Code (IaC) techniques. This project will demonstrate your ability to automate infrastructure deployment and showcase your proficiency in using Terraform. Let’s dive in and build your AWS infrastructure with ease!
Task: Build Your AWS Infrastructure with Terraform
Follow these step-by-step instructions to build your AWS infrastructure using Terraform:
1.Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16
Define a new Terraform resource block for
aws_vpc
.Set the
cidr_block
attribute to "10.0.0.0/16" to specify the IP address range for the VPC.
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main"
}
}
This will create a new VPC in your AWS account with the specified CIDR block and a name tag of "main".
Go to the VPC console and check new VPC with the name 'main' is successfully created.
2. Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.
Define a new Terraform resource block for
aws_subnet
.Set the
vpc_id
attribute to the ID of the VPC created in the previous step.Set the
cidr_block
attribute to "10.0.1.0/24" to specify the IP address range for the subnet.
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "Public Subnet"
}
}
Go to VPC console then go to Subnets.
Check that "Public Subnet" is created successfully.
3. Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.
Define another Terraform resource block for
aws_subnet
.Set the
vpc_id
attribute to the ID of the VPC.Set the
cidr_block
attribute to "10.0.2.0/24" for the subnet IP address range.
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
tags = {
Name = "Private Subnet"
}
}
Go to VPC console then go to Subnets.
Check that "Public Subnet" is created successfully.
4. Create an Internet Gateway (IGW) and attach it to the VPC.
Define a Terraform resource block for
aws_internet_gateway
.Attach the Internet Gateway to the VPC by setting the
vpc_id
attribute.
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "igw"
}
}
This Terraform code creates an internet gateway in the VPC with ID aws_vpc.main.id, and tags it with the name "igw".
terraform apply -
Go to VPC then go to Internet gateways
Check new internet gateway is created with the name 'igw'.
5. 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 a Terraform resource block for
aws_route_table
.Associate the route table with the public subnet by setting the
vpc_id
attribute.Add a route to the Internet Gateway using the
aws_route
resource block.
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "route-table"
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public.id
}
First create a route table for public subnet.
aws_route_table block creates a new route table in the VPC specified by vpc_id attribute. It also defines a route that sends all traffic with destination CIDR 0.0.0.0/0 to the internet gateway specified by gateway_id attribute. The tags attribute sets a name for the route table for easy identification.
Then associate the route table with the public subnet.
aws_route_table_association block associates the newly created route table with a public subnet specified by the subnet_id attribute. The route_table_id attribute refers to the ID of the route table created in the previous block.
In Route tables, new route table is successfully created.
Route table routes with internet gateway.
6. Launch an EC2 instance in the public subnet with the following details:
Define a Terraform resource block for
aws_instance
.Set the
ami
attribute to "ami-0eb260c4d5475b901" for the Amazon Machine Image (AMI).Set the
instance_type
attribute to "t2.micro" for the instance type.Specify a security group that allows SSH access from anywhere.
Use the
user_data
attribute to provide a shell script that installs Apache and hosts a simple website.
resource "aws_instance" "web_server" {
ami = "ami-0f8ca728008ff5af4"
instance_type = "t2.micro"
key_name = "terraform-key"
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [
aws_security_group.ssh_access.id
]
Security group: Allow SSH access from anywhere
resource "aws_security_group" "ssh_access" {
name_prefix = "ssh_access"
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
User data: Use a shell script to install Apache and host a simple website
The user_data attribute specifies the script to run when the instance is launched. This script updates the package manager, installs Apache web server, creates a basic HTML file, and restarts Apache.
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<html><body><h1>Welcome to my website!</h1></body></html>" > /var/www/html/index.html
sudo systemctl restart apache2
EOF
- Create an Elastic IP and associate it with the EC2 instance.
Define a Terraform resource block for
aws_eip
.Associate the Elastic IP with the EC2 instance by setting the
instance
attribute to the ID of the instance.
resource "aws_eip" "eip" {
instance = aws_instance.web_server.id
tags = {
Name = "test-eip"
}
}
Terraform main.tf file for creating EC2 instance in the public subnet.
Run terraform apply to create the EC2 instance in your AWS account
- Access the URL of your EC2 instance in a browser to ensure that the website is hosted successfully.
By following these steps, you will build your AWS infrastructure using Terraform. Remember to clean up your resources after completing the project to avoid unnecessary costs.
I hope you learned something from this blog. If you have, don’t forget to follow and click the clap 👏 button below to show your support 😄. Subscribe to my blogs so that you won’t miss any future posts.
If you have any questions or feedback, feel free to leave a comment below. Thanks for reading and have an amazing day ahead!
LinkedIn: https://www.linkedin.com/in/trushid-hatmode/
GitHub: https://github.com/Trushid
Happy Learning!