3 Tier Architecture on AWS Using Terraform

Manish Chaudhary
4 min readJun 30, 2021

1.What is Terraform?

Terraform is an open-source infrastructure as a code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language.

2.How Terraform works?

Terraform allows infrastructure to be expressed as code in a simple, human-readable language called HCL (HashiCorp Configuration Language). It reads configuration files and provides an execution plan of changes, which can be reviewed for safety and then applied and provisioned.

Extensible providers allow Terraform to manage a broad range of resources, including IaaS, PaaS, SaaS, and hardware services.

3. Prerequisites

Install Terraform CLI

Install AWS CLI & Configure Credentials (You know how to configure, right?)

Important Commands

terraform init: The terraform init command is used to initialize a working directory containing Terraform configuration files.

terraform fmt: to have terraform format your code in a clean way.

terraform plan: The terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

terraform apply: The terraform apply command performs a plan just like terraform plan does, but then actually carries out the planned changes to each resource.

Alright, Let’s get started…

I will be deploying all the resources in AP-SOUTH-1 region.

First, make a folder and a file main.tf (I will be creating variables and terraform.tfvars files as well to reference variables) inside that folder.

open that file/folder in VScode(or any editor of your choice)

Creating a VPC in AWS with the cidr block of 10.0.0.0/16

var.my-vpc-cidr(cidr block of vpc) is defined in terraform.tfvars file.

Creating 2 Public Subnets with cidr block 10.0.1.0/24, 10.0.2.0/24

Creating Private Subnets for Database with cidr 10.0.3.0/24, 10.0.4.0/24

Creating Internet Gateway And Route Table

Associating our public subnets to route table so that our resources inside our public subnet can communicate with the internet.

Security Group to allow Traffic on Port 80

Using Data Source to fetch AMI Info

Create EC2 instance in Public Subnets

Configure Security Group for RDS

configure a security group to all inbound traffic from our application load balancers.

Create ALB and Launch it in Public Subnets

ALB mapping to EC2 Instances

Adding Listener on Port 80

Creating RDS db Instances in Private Subnets

Now just run the terraform commands and most importantly dont forget to run terraform destroy to destroy everything once everything goes well.

still confused about code or anything? Alright I have got your back..Here is the code from my Git Repo

PS:- I am new to terraform, Just practicing what I learn. I will be using best practices and will be adding more articles in the future.

--

--

Manish Chaudhary

DevOps/SRE | Cloud Enthusiast | CKA | 2x AWS Certified