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

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

·

5 min read

Task:

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

Establish a Virtual Private Cloud (VPC) using the CIDR block 10.0.0.0/16 with the following Terraform configuration:

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

  tags = {
    Name = "main"
  }
}

This configuration will generate a new VPC in your AWS account, featuring the designated CIDR block and labeled as "main."

The specified Terraform block outlines the required version of Terraform to execute this setup. In this instance, it mandates that the Terraform version should be >= 1.2.0. Additionally, include the region where you desire your instances to be located.

Execute the terraform init command to set up the working directory and download the necessary providers. Subsequently, utilize terraform apply to instantiate the VPC in your AWS account.

Verify the successful creation of the new VPC named 'main' by accessing the VPC console.

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

To generate a public subnet within the previously established VPC, employing the CIDR block 10.0.1.0/24, utilize the subsequent Terraform code:

resource "aws_subnet" "public_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Public Subnet"
  }
}

Execute the terraform apply command to instantiate the public subnet in your AWS account.

Navigate to the VPC console, proceed to the Subnets section, and verify the successful creation of the "Public Subnet."

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

This Terraform configuration generates a private subnet with the CIDR block 10.0.2.0/24 within the VPC identified by the ID aws_vpc.main.id. The subnet is tagged with the name "Private Subnet."

resource "aws_subnet" "private_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"

  tags = {
    Name = "Private Subnet"
  }
}

Execute the terraform apply command to execute the configuration and create the "Private Subnet."

Confirm the successful creation of the "Private Subnet" by checking the results in the VPC console after running Terraform apply.

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

This Terraform configuration generates an internet gateway within the VPC identified by the ID aws_vpc.main.id. The internet gateway is tagged with the name "igw."

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

  tags = {
    Name = "igw"
  }
}

Execute the terraform apply command to apply the configuration and create the internet gateway.

Afterwards, navigate to the VPC console, proceed to the Internet Gateways section, and verify the successful creation of the new internet gateway labeled 'igw.' Confirm that the VPC is attached to the internet gateway.

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.

Declare a route table for the public subnet using the following Terraform configuration:

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
}

The aws_route_table block establishes a new route table within the VPC specified by the vpc_id attribute. It defines a route directing all traffic with a destination CIDR of "0.0.0.0/0" to the internet gateway identified by the gateway_id attribute. The tags attribute assigns a name ("route-table") to facilitate easy identification.

Subsequently, the aws_route_table_association block associates the newly created route table with a public subnet, identified by the subnet_id attribute. The route_table_id attribute refers to the ID of the route table created in the preceding block.

Verify the successful creation of the new route table in the Route Tables section. Confirm that the routes direct traffic to the internet gateway. Additionally, acknowledge the association of the route table with the public subnet through Terraform.

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

  • AMI

  • Instance type: t2.micro

The aws_instance block initiates the creation of a new EC2 instance in the specified public subnet, denoted by the subnet_id attribute. The Amazon Machine Image (AMI) ID utilized is ami-0f8ca728008ff5af4, representing an Ubuntu 18.04 LTS image. The key_name attribute designates the key pair name for SSH access, and the vpc_security_group_ids attribute identifies the security group by its ID, created in the subsequent block.

resource "aws_instance" "web_server" {
  ami           = "ami-0c7217cdde317cfec"
  instance_type = "t2.micro"
  key_name      = "ansible_key"
  subnet_id     = aws_subnet.public_subnet.id

  vpc_security_group_ids = [
      aws_security_group.ssh_access.id
  ]
 tags = { 
    name = "terraform-instance"
}
}

Security group: Allow SSH access from anywhere

The aws_security_group block establishes a security group allowing inbound traffic on ports 22 (SSH) and 80 (HTTP) from any source (0.0.0.0/0). The name_prefix attribute sets a prefix for the security group name, and the vpc_id attribute specifies the VPC's ID where the security group is created.

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 contains a shell script that installs Apache, creates a basic HTML file, and restarts Apache upon instance launch.

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>Namaste! 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.

The aws_eip block creates an Elastic IP associated with the EC2 instance created earlier, specifying the instance ID in the instance attribute. The tags attribute names the Elastic IP for easy identification.

resource "aws_eip" "eip" {
  instance = aws_instance.web_server.id

  tags = {
    Name = "test-eip"
  }
}

After running terraform apply, the EC2 instance, named "terraform-instance," is successfully created in the AWS account.

Ultimately our main.tf file looks like this:

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


Thanks for reading until here. See you in the next one.