Deploy 2 Tier Architecture using Terraform

Huy Le
Nerd For Tech
Published in
5 min readAug 15, 2022

Welcome back to my Medium.
This week, we will be focusing on Terraform.

Before we begin, I think we all should know a basic knowledge about Terraform.

So what is Terraform?

Terraform is a Infrastructure as Code(IaC) that developed by Hashicorp. It is entirely open source provisioning tool written in JSON-like language called Hashicorp Configuration Language(HCL).

If you have knowledge in AWS, you probably know CloudFormation, a service offered by AWS. Cloudformation is similar to Terraform but the Terraform is Multi-cloud, It can be used in any Cloud Platform you could think of.

What problems do Terraform solve?

Terraform is a declarative language for infrastructure management. It means that you don’t have to worry about specifics of what calls to use to make things, all you need to do is write a description file of what you want it do to.

For example: I start a new project and I will have to deploy it in AWS. Without Terraform I will have to go onto AWS console and click on EC2 to deploy instances, S3 for storage and manually configure VPC, Subnet and Security groups to my own desire and that’s a lot of clicking for me to do.

Terraform solve that infrastructure management problem, all I have to do is write down a descriptive terraform file, specifying what resources I want and deploy it. Terraform will keep track of changes and deploy the whole infrastructure for me.

Here is what we will be deploying for our project:

1. Deploy a VPC with CIDR 10.0.0.0/16 with 2 public subnets with CIDR 10.0.1.0/24 and 10.0.2.0/24. Each public subnet should be in a different AZ for high availability.
2. Create 2 private subnet with CIDR ‘10.0.3.0/24’ and ‘10.0.4.0/24’ with an RDS MySQL instance (micro) in one of the subnets. Each private subnet should be in a different AZ.
3. A load balancer that will direct traffic to the public subnets.
4. Deploy 1 EC2 t2.micro instance in each public subnet.

Prerequisites:

  • AWS account
  • IDE, I will be using VSCode

On a side of note, Instead of coding out HCL like regularly, I will be using Terraform modules which promote reusable and prevent errors.

To find AWS module, you can find it on official Terraform registry website.

The best recommend practice in Terraform is using Terraform Module but this is my very first Terraform project so I will be writing all my codes in one file.

Let’s get it started!.

We will start with providing terraform what cloud we want to use, specifying the version and the region we want to use.

Next, we will start with the below codes to create VPC and our 2 public subnets

Private subnets.

We will need:

  • Internet Gateway to direct traffic.
  • A route table for the gateway to determine where the network traffic from the gateway is directed.
  • Route tables for our Public subnets.

So next thing we need is Security Groups for our Public and Private subnets. This will allow incoming traffic from certain ports.

Load balancer will be the next thing we create. It will help distributes evenly the amount of connections, prevent overloading. Think of it like a host at a restaurant, every time you walked in, the host will seat you into the appropriate table.

The Load Balancer will need a Target Group which will divert the traffic and the listener will process the request

So we have everything that we needed. It’s time to create EC2. I included a a little script so that we can ensure the webpage is working to our desire.

Lastly, our RDS with a Subnet Group attached, It will help designate a VPC to our RDS database instance.

I also included IP address as outputs, this will show the IP address when we do terraform command.

Now that we have everything we needed to deploy our 2-tier architecture.

Let’s run it!.

terraform init

This command will be the very first command that you will do. It help initialize a directory with terraform configuration files.

Once success, it will show the image below

terraform plan

The command will execute a plan and show you a preview of what is going to be built.

After those 2 commands, you can finally run

terraform apply

The CLI will ask if you want to proceed, type yes

The process will take 5 minutes as the RDS database take a while to build

A success notification and the output IP address that we put in our code will pops up when it’s done.

We can go onto our AWS and see the resources that created.

You can check the webserver by going onto our load balancer and copy the DNS name => paste it on a new tab and It will show our webpage.

The last part of our tutorial will be deleting all our resources that we created.

terraform destroy

You will be prompted to type yes again and it will proceed.

Thank you so much for following my tutorial!.

--

--

Huy Le
Nerd For Tech

DevOps/Cloud engineer passionate about technologies. Looking to collaborate on anything Cloud related. Connect with me on https://www.linkedin.com/in/huy-le-110