Day 66 Task: Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

Day 66 Task: Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

Welcome back to your Terraform journey.

In the previous tasks, you have learned about the basics of Terraform, its configuration file, and creating an EC2 instance using Terraform. Today, we will explore more about Terraform and create multiple resources.

🔶 Task:

  • Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16

        resource "aws_vpc" "tf_vpc" {
          cidr_block = "10.0.0.0/16"
    
          tags = {
            Name = "tf_vpc"
          }
        }
    
  • Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.

        resource "aws_subnet" "tf_public_subnet" {
          vpc_id     = aws_vpc.tf_vpc.id
          cidr_block = "10.0.1.0/24"
    
          tags = {
            Name = "tf_public_subnet"
          }
        }
    
  • Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.

        resource "aws_subnet" "tf_private_subnet" {
          vpc_id     = aws_vpc.tf_vpc.id
          cidr_block = "10.0.2.0/24"
    
          tags = {
            Name = "tf_private_subnet"
          }
        }
    
  • Create an Internet Gateway (IGW) and attach it to the VPC.

        resource "aws_internet_gateway" "tf_igw" {
          vpc_id = aws_vpc.tf_vpc.id
    
          tags = {
            Name = "tf_igw"
          }
        }
    
        resource "aws_route" "igw_route" {
          route_table_id         = aws_route_table.tf_routetable.id
          destination_cidr_block = "0.0.0.0/0"
          gateway_id             = aws_internet_gateway.tf_igw.id
        }
    
  • 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.

        resource "aws_route_table" "tf_routetable" {
          vpc_id = aws_vpc.tf_vpc.id
    
          route {
            cidr_block = "0.0.0.0/0"
            gateway_id = aws_internet_gateway.tf_igw.id
          }
    
          tags = {
            Name = "tf_routetable"
          }
        }
    
        resource "aws_route_table_association" "public_subnet_association" {
          subnet_id      = aws_subnet.tf_public_subnet.id
          route_table_id = aws_route_table.tf_routetable.id
        }
    
        resource "aws_route_table_association" "private_subnet_association" {
          subnet_id      = aws_subnet.tf_private_subnet.id
          route_table_id = aws_route_table.tf_routetable.id
        }
    
  • Launch an EC2 instance in the public subnet with the following details:

  • AMI: ami-053b0d53c279acc90

  • Instance type: t2.micro

  • Security group: Allow SSH access from anywhere

        resource "aws_security_group" "tf_sg" {
          name_prefix = "tf_sg"
          vpc_id      = aws_vpc.tf_vpc.id
    
          ingress {
            from_port   = 80
            to_port     = 80
            protocol    = "tcp"
            cidr_blocks = ["0.0.0.0/0"]
          }
    
          ingress {
            from_port   = 443
            to_port     = 443
            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"]
            ipv6_cidr_blocks = ["::/0"]
          }
    
        }
    
        resource "aws_instance" "tf_ec2instance" {
          ami           = "ami-053b0d53c279acc90"
          instance_type = "t2.micro"
          key_name      = "CP-keys"
          subnet_id     = aws_subnet.tf_public_subnet.id
          security_groups = [
            aws_security_group.tf_sg.id
          ]
    
          user_data = "${file("index.sh")}"
          tags = {
            Name = "tf_ec2instance"
          }
        }
    
  • User data: Use a shell script to install Apache and host a simple website.

      #!/bin/bash
      sudo apt-get update -y
      sudo apt install apache2 -y
      sudo systemctl start apache2
      sudo systemctl enable apache2
      echo "<!DOCTYPE html>
      <html>
      <head>
          <style>
              /* Add some CSS styling to make it look cool */
              body {
                  font-family: Arial, Helvetica, sans-serif;
                  background-color: #f2f2f2;
                  text-align: center;
              }
              h1 {
                  color: #007BFF;
                  font-size: 36px;
                  text-shadow: 2px 2px #333;
              }
          </style>
      </head>
      <body>
          <h1>Welcome to my website</h1>
          <h2>My name is Chandresh Patle!</h2>
      </body>
      </html>
       > /var/www/html/index.html
    
  • Create an Elastic IP and associate it with the EC2 instance.

      resource "aws_eip" "tf_eip" {
          instance = aws_instance.tf_ec2instance.id
        }
    
  • Now Run the terraform plan and terraform apply.

  • Open the website URL in a browser to verify that the website is hosted successfully.

In conclusion, this Terraform hands-on task serves as a practical demonstration of your proficiency in leveraging Infrastructure as Code (IaC) principles to manage AWS infrastructure.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!