Infrastructure Automation Using AWS and Terraform

Krupa Bhimani
4 min readFeb 11, 2022

--

As a part of curriculum of software Group Project (SGP) I , Krupa Bhimani and my classmate Janvi Ajudiya made one DEVOPS based project of automating infrastructure using very famous and open source tool Terraform and we used AWS as a cloud platform. you can read detailed blog given below.

What is DevOps?

DevOps stands for Development and Operations. It is a software engineering practice that focuses on bringing together the development team and the operations team for the purpose of automating the project at every stage. This approach helps in easily automating the project service management in order to aid the objectives at the operational level and improve the understanding of the technological stack used in the production environment.

What is AWS?

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

What is Terraform?

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. This includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. Terraform can manage both existing service providers and custom in-house solutions.

Terraform Script :

Connection with cloud providers :

provider “aws” {
region = “us-east-1”
access_key = “***”
secret_key = “***”
}

Create VPC:

resource “aws_vpc” “sgp” {
cidr_block = “10.0.0.0/16”
tags = {
Name = “SGP”
}
}

Create IGW:

resource “aws_internet_gateway” “gw” {
vpc_id = aws_vpc.sgp.id
}

Create public subnet:

resource “aws_subnet” “publicsubnet” {
vpc_id = aws_vpc.sgp.id
cidr_block = “10.0.1.0/24”
availability_zone = “us-east-1a”
tags = {
Name = “Public-subnet”
}
}

Create Private subnet:

resource “aws_subnet” “privatesubnet” {
vpc_id = aws_vpc.sgp.id
cidr_block = “10.0.2.0/24”
availability_zone = “us-east-1a”
tags = {
Name = “Private-subnet”
}
}
resource “aws_route_table” “public-route-table” {
vpc_id = aws_vpc.sgp.idroute {
cidr_block = “0.0.0.0/0”
gateway_id = aws_internet_gateway.gw.id
}route {
ipv6_cidr_block = “::/0”
gateway_id = aws_internet_gateway.gw.id
}tags = {
Name = “Public”
}
}

Create network interface:

resource “aws_network_interface” “sgp-nic” {
subnet_id = aws_subnet.privatesubnet.id
private_ips = [“10.0.2.50”]
security_groups = [aws_security_group.allow_web.id]
}

Create private route table:

resource “aws_route_table” “private-route-table” {
vpc_id = aws_vpc.sgp.id
route {
cidr_block = “10.0.2.0/24”
network_interface_id = aws_network_interface.sgp-nic.id

}tags = {
Name = “Private”
}
}tags = {
Name = “Private”
}
}

Associate subnet with route table:

resource “aws_route_table_association” “private” {
subnet_id = aws_subnet.privatesubnet.id
route_table_id = aws_route_table.private-route-table.id
}

Create Security group:

resource “aws_security_group” “allow_web” {
name = “allow_web_traffic”
description = “Allow Web inbound traffic”
vpc_id = aws_vpc.sgp.id
ingress {
description = “HTTPS”
from_port = 443
to_port = 443
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress {
description = “HTTP”
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress {
description = “SSH”
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”]
}tags = {
Name = “allow_web”
}
}

Create EC2 instance:

resource “aws_instance” “web-server-instance” {
ami = “ami-087c17d1fe0178315”
instance_type = “t2.micro”
availability_zone = “us-east-1a”
key_name = “sgp”
tags = {
Name = “web-server”
}
}

This is script for creating EC2 instance inside specific VPC with custom rules, routes and everything using Terraform.

The whole work has been done two phase.

  1. Web app development
  2. Infrastructure automation on terraform

Let’s check first part. We made one .NET(C#) based web application in which user can buy car batteries of different category as well as different companies. In this first page there are three rotating images and quotes will be changed automatically for user attraction purposes.

Home page

Here there will be list of companies of batteries and when you press “explore” button you can see detailed information about particular company.

companies

Next page is Service page. here there will company wise colorful pricing table will be shown with different types of batteries and starting price.

service page

Next is about Contact us page. This includes google map and contact form for any kind of queries.

contact us

there will be login and register facilities for user.

login — register

You can see the gif of project given below.

GIF

you can find the code of project from my github account.

--

--

Krupa Bhimani

2X AWS Certified, DevOps Engineer, Microsoft Learn Student Ambassadors (Alpha)