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

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

Day 66 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start with Terraform Hands-on Project.

In this blog, we will use Terraform to set up a basic AWS infrastructure, including a VPC, public and private subnets, an Internet Gateway, a route table, a security group, and an EC2 instance hosting a simple website. The tutorial will guide you through each step, providing explanations for each Terraform block.

Task:

First create provider as provider.tf

# provider.tf
terraform {
    required_providers {
      aws = {
        source = "hashicorp/aws"
        version = "~> 5.0"
      }
    }
}

provider "aws" {
    region = "ap-south-1"
}

Now we are ready to create resources in main.tf

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

# created vpc
resource "aws_vpc" "mainvpc" {
  cidr_block = "10.0.0.0/16"
}

Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.

Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.

resource "aws_subnet" "public_subnet" {
  vpc_id                  = aws_vpc.mainvpc.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "ap-south-1a"
  map_public_ip_on_launch = true
}

resource "aws_subnet" "private_subnet" {
  vpc_id            = aws_vpc.mainvpc.id
  cidr_block        = "10.0.2.0/24"
  availability_zone = "ap-south-1b"
}

Replace "availability_zone" with your desired availability zone.

Create an Internet Gateway (IGW) and attach it to the VPC.

# created internet_gateway
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.mainvpc.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.

# created route table
resource "aws_route_table" "routetab" {
  vpc_id = aws_vpc.mainvpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
}

Also you have to create a Security Group.

# created route table association
resource "aws_route_table_association" "rta1" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.routetab.id
}

# created security group
resource "aws_security_group" "websg" {
  name        = "web"
  description = "Allow TLS inbound traffic"
  vpc_id      = aws_vpc.mainvpc.id

# inbound rule
  ingress {
    description = "HTTP from VPC"
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    description = "SSH from VPC"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

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

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

Launch an EC2 instance in the public subnet with the following details:

AMI: ami-0557a15b87f6559cf

Instance type: t2.micro

Security group: Allow SSH access from anywhere

User data: Use a shell script to install Apache and host a simple website

Create an Elastic IP and associate it with the EC2 instance.

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

resource "aws_instance" "instance" {
  ami                    = "ami-03f4878755434977f" # Replace with Ubuntu AMI
  instance_type          = "t2.micro"
  subnet_id              = aws_subnet.public_subnet.id
  vpc_security_group_ids = [aws_security_group.websg.id]

  user_data = <<-EOF
    #!/bin/bash
    apt-get update
    apt-get install -y apache2
    systemctl start apache2
    systemctl enable apache2
    echo "<h1>Hello from Terraform</h1>" > /var/www/html/index.html
  EOF
}

Replace "ami-xxxxxxxxxxxxxxxxx" with the correct Ubuntu AMI for your region.

Apply Terraform Configuration

Run the following command to apply the Terraform configuration:

terraform init
terraform apply -auto-approve

Verify Website Hosting

After Terraform applies the configuration, open the EC2 instance's public IP or DNS in a web browser to verify that the website is hosted successfully.

You should see the Hello message on the website hosted on your EC2 instance.

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: LinkedIn