Automating Infrastructure using AWS and Terraform

Janvi Ajudiya
5 min readSep 15, 2021

The Charotar University of Science and Technology did not stop worrying about their students development in midst of COVID-19 pandemic as they provide chance to their students to make project as a part of curriculum. As a part of it, I am able to complete my project in this pandemic situation to add it extra to resume.

You have questions like what’s the use of this project? Where it is helpful? What it does? What is the purpose behind this project? So basically, this project is all about automation of infrastructure hosted on AWS cloud platform. Infrastructure automation has became a daily need for company for easy deployment and maintenance of resources.

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.

What is CI/CD pipeline?

CI and CD are two acronyms frequently used in modern development practices and DevOps. CI stands for continuous integration, a fundamental DevOps best practice where developers frequently merge code changes into a central repository where automated builds and tests run. But CD can either mean continuous delivery or continuous deployment.

CI/CD Pipeline

Continuous Integration :

Continuous Integration is a process where developers work on code changes and commit the code very frequently to repository. CI triggers the build every time whenever new code changes detected in repository. Unit testing is performed against these code changes every time. CI enables to detect the errors at early stage or when code is being integrated. CI is a set of principles that helps developers to manage code integrations and detect early stage bugs and rectify them.

Continuous Delivery :

Continuous delivery is an extension of continuous integration since it automatically deploys all code changes to a testing and/or production environment after the build stage.

Continuous Delivery is a process where integrated code is pushed to specific environments. It ensures code delivery to specified infrastructure environment. It starts after continuous integration. CD ensures the automation of delivering new code with minimum efforts. Some extra checks are also performed during CD process such as performance test for production environment.

Continuous deployment :

Continuous deployment goes one step further than continuous delivery. It is most critical stage in pipeline. In this process code changes are automatically deployed to production environment where end customers or users are using the application. It is achieved by taking the benefit of continuous delivery by automating new stage (Production) in the pipeline. There is very less human interaction at this stage and it helps to reduce delay in making code changes live.

Continuous Delivery vs Continuous Deployment

What is .NET?

.NET is a developer platform made up of tools, programming languages, and libraries for building many different types of applications.

There are various implementations of .NET. Each implementation allows .NET code to execute in different places — Linux, macOS, Windows, iOS, Android, and many more.

  1. .NET Framework is the original implementation of .NET. It supports running websites, services, desktop apps, and more on Windows.
  2. .NET Core is a cross-platform implementation for running websites, services, and console apps on Windows, Linux, and macOS. .NET Core is open source on GitHub.
  3. Xamarin/Mono is a .NET implementation for running apps on all the major mobile operating systems, including iOS and Android.

.NET Standard is a formal specification of the APIs that are common across .NET implementations. This allows the same code and libraries to run on different implementations.

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.id
route {
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.

Overview of Project :

Website using .NET framework

--

--