Build a three-tier web application using Terraform.

Emmanuelibok
6 min readMay 30, 2023

--

From the beginning, we’ll look at how to use Terraform to automate the provisioning of a 3-tier architecture for a web app on AWS.

What is Terraform?

Through the use of configuration files rather than a graphical user interface, Terraform is an infrastructure-as-code (IaC) solution that enables the management of infrastructure. By specifying resource configurations that you can version, reuse, and share, Terraform enables you to build, modify, and manage your infrastructure in a safe, dependable, and repeatable manner.

Terraform is an infrastructure-as-code solution by HashiCorp. It allows you to describe resources and infrastructure in declarative configuration files that are understandable by humans and maintain the lifecycle of your infrastructure.

Benefits

There are certain advantages to using Terraform rather than managing your infrastructure by hand:

Terraform is cloud-agnostic and capable of managing infrastructure across many cloud services.
You can quickly build infrastructure code thanks to the human-readable configuration language.
You may manage resources by tracking changes to them throughout deployments using Terraform’s state.
When working on infrastructure (which enables collaboration), you can safely cooperate by committing your configurations to version control.
Terraform provides consistency and reconciles the desired configuration with the actual state to minimize configuration drift because all of your infrastructure is code.

Comparing this to the manual effort we put into this project is a step up. Go there to get a feel for how to manually do everything we are about to do next. With Terraform, you would be better equipped to recognize the strength and value of automation.

Prerequisites

You can quickly navigate to any of the following topics.

Let’s start now…

AWS Authentication

Run the command in the VS Code terminal to configure programmatic access.

aws configure

Grab your AWS credentials for programmatic access and enter them one line at a time.

AWS Access Key ID: AKIA2ZFMGVYZABCDE

Secret Access Key: fYi+iKNoW+tHisIsAfAkEkz123+abcde

Default region name: us-east-1

Default output format: JSON

Output like this ought to be the result.

Let’s put our programmatic access to the test.

  • Run the command below
aws s3 ls

Depending on how you installed it, your output should either display a list of S3 buckets or be blank if none are present.

Output showing no bucket

Clone Repo

Open your chosen terminal or code editor. I’ll be using Visual Studio Code.

git clone https://github.com/Emmylong1/terraform-3-tier-webapp.git
  • Change to the 3-tier-webapp directory
from Emmylong

To set up the working directory, use terraform init.

The Terraform Plan shows the execution strategy and the modifications that Terraform intends to make.

The plan presented in Terraform Plan is put into action via Terraform Apply.

There will be a prompt; enter “yes.”

Let’s wait now…

You can get some popcorn and watch Terraform at work as soon as you press the Enter key. To provision the resources you specified, Terraform is really using AWS’s API to communicate with Terraform.

Time: I timed the entire procedure, and from the moment I pressed Enter, the provisioning and setup of the web app took a little over 20 minutes.

In the meantime, while we wait for Terraform to provision all the resources, let’s take a look at what is in the configuration file.

Networking

For the EC2 instances and the RDS databases, we construct a unique VPC with 2 public subnets and 4 private subnets. We will require an internet gateway in our public subnet to enable access to the internet. A NAT gateway will gladly serve that function in conjunction with the elastic IPs so that resources in the private subnets can commence communication with the internet.Traffic to and from the load balancer, EC2, and RDS instances is managed by security groups, which adds an additional layer of security to the system.

Database

Let’s move on to the database portion next. In this section, we define our database cluster and instance. Multi-AZs will be used for high availability.

Compute

The operation’s brains must now take over. an EC2 instance

Result

Here, we record the application load balancer’s DNS name.

One of the resources we deployed in advance of

Clear up

Terraform has the wonderful feature of allowing you to delete resources without having to go through them one by one. It is sophisticated enough to understand what was made and how to carefully erase each resource while taking into account their interdependencies.

Terraform depletes all produced resources.

emmylong

Where it prompts a response, enter yes

We successfully set the blueprint for our infrastructure. In the event we need to rebuild the entire environment from scratch, we don’t have to do it manually like we did here. We can simply run the terraform apply command, and in about half an hour, everything should be up and running. Awesome! I hope you liked it. Let me know your thoughts!

I hope you enjoyed it. Let me know what you think!

Once more, you can locate and clone the repository ‘[Here](https://github.com/Emmylong1/terraform-3-tier-webapp.git)’. See you in the following.

--

--

Emmanuelibok

As a DevOps pro, I automate workflows, boost reliability, and foster teamwork. My goal: agile, secure, and efficient software delivery.