Building a Multi-Tier Architecture on AWS with Terraform: A Comprehensive Guide

Yasholo
12 min readJun 12, 2024

--

3-tier architecture on AWS using terraform

Introduction

As cloud computing continues to revolutionize the way we deploy and manage applications, infrastructure as code (IaC) tools like Terraform have become essential. Terraform enables the creation, management, and automation of infrastructure across various cloud providers. This article will guide you through setting up a multi-tier architecture on AWS (Amazon Web Services) using Terraform, focusing on modularity, scalability, and maintainability.

πŸ“‹Index

  • Overview
  • Prerequisites
  • Objective
  • Key Features and Components
  • Project Directory Structure
  • Terraform Configuration Files
  • Deployment Steps
  • Future Enhancements
  • Conclusion

πŸ”° Overview

This project aims to deploy a multi-tier architecture on AWS, consisting of three tiers: the Presentation Layer (Web Tier), the Application Layer, and the Data Layer (Database Tier). Each tier will be deployed using Terraform to automate infrastructure provisioning, configuration, and management. Terraform involves structuring infrastructure into modular layers, each responsible for specific tasks such as networking, security, load balancing, auto-scaling, database management, and monitoring. This approach ensures scalability, security, and maintainability. This article provides a comprehensive guide on setting up a multi-tier architecture using Terraform, covering all aspects from directory structure to deployment and future enhancements.

1. Web Tier:

Serves as the entry point for user requests.

2. Application Tier:

It contains the application’s core logic and processing capabilities and handles business logic, processing user requests, and generating responses. Interacts with the Web Tier and Database Tier.

3. Database Tier:

Stores and manages application data persistently. Ensures data integrity, consistency, and durability through backups and replication.

(We will be using a high standard terraform module approach in this project. Resulting in a Multi-Tier-Project helping us to eficciently run our applications. (you won’t find actual web, application or database module but a variety of other modules which will be creating all these instances for us)

Infrastructure Architecture

Below is a diagram of the architecture we will be deploying:

Architechture Diagram

🌟 Objective

  • Modularity: Utilize Terraform modules to encapsulate each tier’s components, enabling reusability and maintainability.
  • Scalability: Implement Auto Scaling Groups to dynamically scale resources based on demand, ensuring optimal performance and cost efficiency.
  • High Availability: Deploy resources across multiple Availability Zones (AZs) to enhance fault tolerance and maintain continuous availability.
  • Security: Utilize AWS Identity and Access Management (IAM), security groups, and encryption mechanisms to enforce data security and compliance.
  • Monitoring and Logging: Integrate AWS CloudWatch for monitoring resource utilization, application performance, and operational metrics.
  • Infrastructure as Code (IaC): Implement infrastructure deployment and management using Terraform, enabling version-controlled, repeatable, and consistent infrastructure configurations.

🌠 Key Features and Components:

Networking

  • VPC: Isolated network for your infrastructure.
  • Subnets: Public and private subnets for better segregation.
  • Internet gateway: It provide two-way public connectivity to applications running in AWS Regions and/or in Local Zones.
  • Route tables: It contains a set of rules, called routes, that determine where network traffic from your subnet or gateway is directed.

Securityroute tables

  • Security Groups: Control inbound and outbound traffic, ensuring only authorized traffic can reach your resources.

Load Balancing

  • Application Load Balancer (ALB): Distributes incoming application traffic across multiple targets (e.g., EC2 instances), improving availability and fault tolerance.

Auto Scaling

  • Auto Scaling Groups: Automatically adjusts the number of EC2 instances based on demand, ensuring your application can handle varying loads.

Database

  • RDS: Managed relational database service for storing and managing data, with automated backups and updates.

Monitoring

  • CloudWatch: Monitors resources and applications, providing insights and alerting capabilities.

πŸ›’ Prerequisites

  • AWS Account: You need an active AWS account to provision resources and services on the AWS cloud platform.
  • AWS Access Credentials: Obtain AWS access credentials with appropriate permissions to create and manage resources. This involves creating an IAM user with programmatic access and attaching policies that grant necessary permissions.
  • Terraform Installed: Install Terraform on your local machine or development environment. You can follow this article on terraform installation.
  • AWS CLI Installed and Configured: Install the AWS Command Line Interface (CLI) on your machine and configure it with your AWS access credentials. This allows Terraform to interact with AWS services on your behalf. You can follow this article on AWS CLI Installation..
  • And lastly some basic knowledge of AWS services (VPC, EC2, RDS, ALB, CloudWatch) and Terraform.

No more BS… Let’s get over with our multi-tier AWS terraform project!

Step-by-Step Guide:

🧬 Step 1: Set Up Your Project Directory Structure

Organize your Terraform files and modules into a structured directory for better maintainability:

terraform-project/
β”‚
β”œβ”€β”€ main.tf # Main Terraform configuration file
β”œβ”€β”€ variables.tf # Input variables
β”œβ”€β”€ outputs.tf # Output variables
β”œβ”€β”€ terraform.tfvars # Terraform variable values (optional)
└── modules/ # Directory for reusable modules (optional)
β”œβ”€β”€ network/
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ variables.tf
β”‚ └── outputs.tf
β”œβ”€β”€ security_group/
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ variables.tf
β”‚ └── outputs.tf
β”œβ”€β”€ load_balancer/
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ variables.tf
β”‚ └── outputs.tf
β”œβ”€β”€ auto_scaling_group/
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ variables.tf
β”‚ └── outputs.tf
β”œβ”€β”€ database/
β”‚ β”œβ”€β”€ main.tf
β”‚ β”œβ”€β”€ variables.tf
β”‚ └── outputs.tf
└── monitoring/
β”œβ”€β”€ main.tf
β”œβ”€β”€ variables.tf
└── outputs.tf

βš™οΈ Terraform Configuration Files

Tip: You can follow the official terraform aws documentation if you ever get stuck while setting up your resources. (use the search bar to get information about a particular object.)

Step 2: Define the Root Module Files

The root module serves as the entry point for your Terraform configuration and includes the primary files that define the overall structure and components of your infrastructure. Here’s an in-depth look at each of the root module files:

main.tf:

  • Purpose: This file is the core of your Terraform configuration. It specifies the provider details and calls various modules to set up different parts of the infrastructure.
  • Components:

Provider Configuration: Defines the cloud provider (e.g., AWS) to be used and sets up credentials, regions, and other settings.

Module Calls: Invokes various modules to create resources such as network, security groups, load balancers, auto-scaling groups, databases, and monitoring.

⭐⭐⭐ [ Main.tf terraform script for this project. ] ⭐⭐⭐

variables.tf:

  • Purpose: Contains all the variable definitions used throughout the Terraform configuration. These variables allow for customizable input values without hardcoding them in the configuration files.
  • Components:

Variable Declarations: Defines variables with descriptions, types, and default values (if any). This helps in making the Terraform configuration flexible and reusable.

⭐⭐⭐ [ Variable.tf terraform script for this project. ] ⭐⭐⭐

outputs.tf:

  • Purpose: Defines the output values from the root module. These outputs can be used to display important information after the infrastructure is provisioned or to pass values between modules.
  • Components:

Output Declarations: Specifies the values to be output after the deployment, such as IDs and ARNs of created resources.

⭐⭐⭐ [ Output.tf terraform script for this project. ] ⭐⭐⭐

terraform.tfvars:

  • Purpose: Provides the actual values for the variables defined in variables.tf. This file is used to set specific values for different environments or configurations without altering the main configuration files.
  • Components:

Variable Assignments: Assigns values to the variables declared in β€˜variables.tf’, allowing for environment-specific configurations.

⭐⭐⭐ [ Terraform.tfvars terraform script for this project. ] ⭐⭐⭐

Modules

Modules are self-contained packages of Terraform configurations that are managed as a group. Each module has its own set of Terraform files that define resources, variables, and outputs.

⭐⭐⭐ [ Modules for this project. ] ⭐⭐⭐

Here’s a detailed look at each module and its purpose:

Step 3: Configure the Network Module πŸ”Œ

Purpose: Sets up the networking components, including VPC, subnets, internet gateway, and route tables.

  • main.tf: Defines resources for creating VPC, subnets (both public and private), internet gateway, and route tables.
  • variables.tf: Declares variables for the network settings, such as VPC CIDR block and subnet CIDRs.
  • outputs.tf: Outputs relevant information like VPC ID, subnet IDs, and route table IDs.

⭐⭐⭐ [ Network Module scripts for this project. ] ⭐⭐⭐

VPC created for secure and isolated private cloud network with full control.
Nat gateway for private subnet to connect outside of our vpc but not vice-versa.
Internet gateway for VPC and public internet interaction.
Route Table created to determine where network traffic from your subnet or gateway is directed
Public and Private Subnets created.
Elastic-IP

Step 4: Configure the Security Group Module πŸ”’

Purpose: Manages security groups to control inbound and outbound traffic.

  • main.tf: Defines resources for creating security groups with specific rules for inbound and outbound traffic.
  • variables.tf: Declares variables for security group settings, such as allowed IP ranges and ports.
  • outputs.tf: Outputs the security group IDs.

⭐⭐⭐ [ Security Group Module scripts for this project. ] ⭐⭐⭐

Neccessary Security Groups created to manage the network traffic of instances.

Step 5: Configure the Load Balancer Module βš–οΈ

Purpose: Sets up an Application Load Balancer (ALB) to distribute incoming traffic across multiple targets.

  • main.tf: Defines resources for creating an ALB, target groups, and listeners.
  • variables.tf: Declares variables for load balancer settings, such as VPC ID and subnet IDs.
  • outputs.tf: Outputs the ALB DNS name and target group ARNs.

⭐⭐⭐ [ Load Balancer Module scripts for this project. ] ⭐⭐⭐

Load Balancers Deployed!
Web-load-balancer for web instances
App-load-balancer for app instances
Target Groups Created
App-target-group
Web-target-group

Step 6: Configure the Auto Scaling Group Module πŸ–₯️

Purpose: Manages auto-scaling groups to ensure the appropriate number of EC2 instances are running to handle the load.

  • main.tf: Defines resources for creating auto-scaling groups, launch configurations, and scaling policies.

(You can also configure your ec2 instance here, to have all the required codes for you application and make it available on the web. Refer to my github repository for an example.)

  • variables.tf: Declares variables for auto-scaling settings, such as VPC ID, subnet IDs, and desired capacity.
  • outputs.tf: Outputs the auto-scaling group name and instance details.

⭐⭐⭐ [ Auto Scaling Group Module scripts for this project. ] ⭐⭐⭐

ASG (auto-scaling groups) created.
All 4 EC2 instances UP and RUNNING!
Web instance 1 can also be accessed using SSH
Web instance 2 can also be accessed using SSH

Step 7: Configure the Database Module πŸ’Ύ

Purpose: Sets up a managed relational database service (RDS) for storing and managing data.

  • main.tf: Defines resources for creating an RDS instance, including subnet groups and security groups.
  • variables.tf: Declares variables for database settings, such as database name, username, password, and instance type.
  • outputs.tf: Outputs the database endpoint and instance details.

⭐⭐⭐ [ Database Module scripts for this project. ] ⭐⭐⭐

Database Instance Created!

Step 8: Configure the Monitoring Module πŸ“ˆ

Purpose: Configures monitoring and logging services to track the performance and health of the infrastructure.

  • main.tf: Defines resources for setting up CloudWatch log groups and metrics.
  • variables.tf: Declares variables for monitoring settings.
  • outputs.tf: Outputs relevant monitoring details such as log group names.

⭐⭐⭐ [ Monitoring Module scripts for this project. ] ⭐⭐⭐

Monitoring alarm deployed!

πŸš€ Step-by-Step Guide for Deployment

To deploy the project, follow these steps:

  • Install Terraform on your local machine and set up AWS credentials with appropriate permissions.
  • Navigate to the project directory.

Step 1: Initialize Terraform

Initialize the Terraform project to set up the backend and install required providers.

terraform init

Step 2: Validate Configuration

Ensure that your configuration files are free of syntax errors and are correctly set up.Ensure that your configuration files are free of syntax errors and are correctly set up.

terraform validate

Step 3: Plan the Infrastructure Changes

Generate an execution plan to see what changes Terraform will make.

terraform plan

Step 4: Apply the Changes

Apply the planned changes to create the infrastructure.

terraform apply

Step 5: Verify the Deployment

Check the outputs to verify that the infrastructure has been set up correctly.

terraform output

You can also run this script if you don’t want to do it one by one manually… start_terraform.sh

start_terraform bash script executed successfully!

start_terraform.sh bash file also generates a terraform.log file if you open this file, you can see all process that occur.

terraform.log file tail output
  • After deployment, access the web application using the Web Load Balancer’s DNS name.
Hurray! our applicattion is up and running!

[Note: The web application deployed above is done using a deployment script β€œdeploy_web_app.sh” in our EC2 instance. you can take a look at the script as well as ec2 EOF section to know how everything works]

Step 6: Terraform Destroy

When your deployment is successful and your project purpose is met. You might want to delete all the resources created to avoid any uneccessary charges from AWS. You can delete all the created resources in one go by running the following command in the project repository where you initiated terraform.

terraform destroy -auto-approve
All Resources Destroyed!

πŸ”­ Future Enhancements

Automation : Implement CI/CD pipelines for automated deployments and updates, improving development efficiency and reducing the risk of human errors. Advanced Monitoring

Advanced Monitoring : Integrate more comprehensive monitoring and alerting systems, using services like AWS CloudWatch and third-party tools. Backup and Recovery

Backup and Recovery : Implement automated backup and recovery processes for critical data, ensuring data integrity and availability. Infrastructure as Code (IaC) Best Practices

Infrastructure as Code (IaC) Best Practices : Continuously improve code quality by following IaC best practices, such as version control, code reviews, and automated testing. Expand Services

Expand Services : Add more AWS services as needed, such as S3 for storage, Lambda for serverless computing, and more, to enhance the functionality and performance of your applications.

Container Orchestration : Implement container orchestration platforms like Amazon ECS or Kubernetes for containerized workloads.

Advanced Monitoring : Enhance monitoring and observability with AWS X-Ray, AWS CloudTrail, and third-party monitoring tools.

🏁 Conclusion

By following this comprehensive guide, you can create a robust, scalable, and secure multi-tier architecture on AWS using Terraform. This modular approach ensures that your infrastructure is maintainable and adaptable to future requirements. Embrace Terraform and AWS to enhance your infrastructure management and streamline your deployment processes.

Take this project as a guide and try to make a project using your own needs and creativity. Do not just copy paste the my github repo… It won’t be of any value if you don’t know how this project was actually made and functions :)

[Final Note: You might face a lot of errors if you are trying to work on this project for the very first time and it’s perfectly fine! It is not an easy project and requires a lot of knowledge to get the things running smoothly.

There can be multiple occasions when you get frustrated because even after so many attempts the deployment throws an error… the red logs flashing on your terminal. I got hundreds of errors while making this project, alot of frustration, but we need to keep trying! That’s the only way to win! The more errors you are going to solve the better you are going to get at terraform!

After all those efforts, when you will finally resolve your errors and see those green logs screaming SUCCESS! You will be proud of yourself!

And as I always say, the Internet is your best companion! Don’t shy away from taking its help if you ever get stuck. Also you can reach out to me on linkedin if you have any doubts ]

Good Luck with the project! See you soon!

Yasholo.

--

--