πŸ—οΈ Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC) Techniques 🌟

πŸ—οΈ Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC) Techniques 🌟

Day 66 : #90DaysOfDevOps Challange

Welcome back to your Terraform journey! In this hands-on project, we will learn how to build your own AWS infrastructure using Terraform. We will leverage Infrastructure as Code (IaC) techniques to create a Virtual Private Cloud (VPC), subnets, an Internet Gateway, launch an EC2 instance, and host a simple website.

Prerequisites

Before we begin, make sure you have the following:

  • Basic understanding of AWS services

  • An AWS account with appropriate permissions

  • Terraform installed on your local machine

  • Basic knowledge of Terraform configuration files

Setting Up the Project

πŸ“ project
  πŸ“„ terraform.tf
  πŸ“„ provider.tf 
  πŸ“„ subnet.tf 
  πŸ“„ internategateway.tf 
  πŸ“„ route.tf
  πŸ“„ ec2.tf
  πŸ“„ userdata.sh
  πŸ“„ vpc.tf

Task 1: Create a VPC 🌐

The first step in building our AWS infrastructure is to create a Virtual Private Cloud (VPC). Let's define the configuration for our VPC in a vpc.tf file.Before that add these files πŸ“

Also add a provider.tf file:

provider "aws" {
  region = "us-east-1"
}

Add the following code to create a VPC with the CIDR block 10.0.0.0/16:

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "main"
  }
}

Save the file and execute terraform init and terraform apply in the project directory to create the VPC. Once the command execution completes, you can check the AWS Management Console for the newly created VPC named "main". 🏒

Task 2: Create a Private Subnet πŸ”’

Now, let's create a private subnet within our VPC. Open a new file named subnet.tf and add the following code:

resource "aws_subnet" "private" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.0.0/16"
  availability_zone = " us-east-1a"

  tags = {
    Name = "private"
  }
}

Save the file and execute terraform apply to create the private subnet. Verify the subnet creation in the AWS Management Console. 🚧

Task 3: Create a Public Subnet 🌐

Similarly, let's create a public subnet within our VPC. Open the subnet.tf file again and add the following code:

resource "aws_subnet" "public" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.16.0/20"
  availability_zone = "us-east-1a"

  tags = {
    Name = "public"
  }
}

Save the file and execute terraform apply to create the public subnet. Verify the subnet creation in the AWS Management Console. οΏ½

Task 4: Create an Internet Gateway 🌐

To provide internet access to our VPC, we need to create an Internet Gateway (IGW). Create a new file named internetgateway.tf and add the following code:

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "internet-gateway"
  }
}

Save the file and execute terraform apply to create the Internet Gateway. Verify the Internet Gateway creation in the AWS Management Console. 🌐

Task 5: Create a Route Table 🚦

Now, let's create a route table for our public subnet and associate it with the subnet. Open a new file named routetable.tf and add the following code:

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 = "public"
  }
}

resource "aws_route_table_association" "public_subnet_association" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public.id
}

Save the file and execute terraform apply to create the route table and associate it with the public subnet. Verify the route table and subnet association in the AWS Management Console. 🚧

Task 6: Create a Security Group πŸ”’

For our EC2 instance, we need to create a security group that allows SSH access and HTTP access from anywhere. Create a new file named securitygroup.tf and add the following code:

resource "aws_security_group" "web_server" {
  name        = "web-server-sg"
  description = "Allow SSH and HTTP access from anywhere"

  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"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Save the file and execute terraform apply to create the security group. Verify the security group and its rules in the AWS Management Console. πŸ”’

Task 7: Create an Elastic IP 🌐

To associate a static IP with our EC2 instance, we need to create an Elastic IP. Create a new file named elasticip.tf and add the following code:

resource "aws_eip" "ip" {
  instance = aws_instance.example.id
  vpc      = true

  tags = {
    Name = "elastic-ip"
  }
}

Save the file and execute terraform apply to create the Elastic IP. Verify the Elastic IP creation in the AWS Management Console. 🌐

Task 8: Create User Data to Install Apache πŸ–₯️

To host a simple website on our EC2 instance, we need to install Apache. We can use user data in Terraform to run a shell script during instance launch. Create a new file named userdata.sh and add the following code:

#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<!DOCTYPE html>
<html>
<head>
    <title>Introduction</title>
    <style>
        body {
            background-color: #d8e2dc;
            font-family: Arial, sans-serif;
            color: #3c415e;
            text-align: center;
            padding: 50px;
        }
        h1 {
            font-size: 3em;
            margin-bottom: 20px;
            text-shadow: 0 2px 2px rgba(0,0,0,0.1);
        }
        p {
            font-size: 1.5em;
            line-height: 1.5;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
    <h1>This is Dhananjay.</h1>
    <p>I am going to be a DevOps Pro</p>
</body>
</html>" > /var/www/html/index.html
sudo systemctl restart apache2

Save the file and make sure it is in the same directory as your Terraform configuration files.

Task 9: Create an EC2 Instance πŸ’»

Finally, let's create our EC2 instance in the public subnet with the required configurations. Open the ec2.tf file and add the following code :

resource "aws_instance" "example" {
  ami           = "ami-0557a15b87f6559cf"
  instance_type = "t2.micro"
  key_name      = "instance"
  subnet_id     = aws_subnet.public.id

  security_groups = [
    aws_security_group.web_server.id
  ]

  user_data = filebase64("userdata.sh")

  tags = {
    Name = "web-server"
  }
}

Later I updated few files in One:

#ec2.tf
#securitygroup
resource "aws_security_group" "web_server" {
  name        = "web-server-sg"
  description = "Allow SSH and HTTP access from anywhere"
  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"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

}

resource "aws_instance" "example" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  key_name      = "myself"
  subnet_id     = aws_subnet.public.id
  associate_public_ip_address = true
  security_groups = [
    aws_security_group.web_server.id
  ]

  user_data = filebase64("userdata.sh")

  tags = {
    Name = "web-server"
  }
}

Save the file and execute terraform apply to create the EC2 instance. Once the instance is up and running, open the website URL in a browser to verify that the website is successfully hosted.

Congratulations! You have successfully built your AWS infrastructure using Terraform. πŸŽ‰

Code Availability🌟

You can access the complete code for this project on GitHub. The code is available in the following GitHub repository: [link here]

Feel free to explore the code, make modifications, and contribute to the project.

Conclusion 🌟

In this blog post, we learned how to use Infrastructure as Code (IaC) techniques with Terraform to build our own AWS infrastructure. We created a VPC, subnets, an internet gateway, a route table, a security group, and launched an EC2 instance with a web server running on it. By automating infrastructure deployment, we can save time, ensure consistency, and easily manage our infrastructure.

Stay tuned for more exciting Terraform tutorials and happy coding! πŸš€πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

Β