Deploying a 3-Tier Application on AWS with Terraform

In today's cloud-driven world, deploying scalable and resilient applications is crucial. A 3-tier architecture is a popular model that separates an application into three logical tiers: presentation, application, and data. In this blog post, we'll walk through the process of deploying such an architecture on AWS using Terraform.

What is a 3-Tier Architecture?

Before we dive into the implementation, let's briefly explain what a 3-tier architecture entails:

  1. Presentation Tier: This is the user interface layer, typically web servers that handle HTTP requests and responses.
  2. Application Tier: This layer contains the business logic, processing client requests and managing data flow between the presentation and data tiers.
  3. Data Tier: This is where data is stored and retrieved, usually involving databases.

Why Use Terraform?

Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using a declarative language. It's cloud-agnostic and provides a consistent workflow for managing resources across various cloud providers.

Prerequisites

Before we start, ensure you have:

  • An AWS account with appropriate permissions
  • Terraform installed on your local machine
  • Basic understanding of AWS services and Terraform syntax

Step-by-Step Implementation

1. Set Up the Terraform Configuration

First, create a new directory for your Terraform project and initialize it:

mkdir terraform-3tier-app
cd terraform-3tier-app
terraform init

2. Define the Provider and Variables

Create a main.tf file to define the AWS provider and declare variables:

provider "aws" {
  region = var.region
}

variable "region" {
  default = "us-west-2"
}

# ... (other variable definitions)

3. Create the VPC and Subnets

Next, we'll create the VPC and subnets for our 3-tier architecture:

resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr
  enable_dns_hostnames = true
  enable_dns_support = true
  tags = {
    Name = "3-tier-app-vpc"
  }
}

# ... (subnet resources)

4. Set Up Security Groups

Security groups act as virtual firewalls. We'll create one for each tier:

resource "aws_security_group" "web" {
  name        = "web-sg"
  description = "Security group for web servers"
  vpc_id      = aws_vpc.main.id

  # ... (ingress and egress rules)
}

# ... (app and db security groups)

5. Create EC2 Instances for Web and App Tiers

Now, let's create EC2 instances for our web and application tiers:

resource "aws_instance" "web" {
  count         = 2
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.public[count.index].id
  vpc_security_group_ids = [aws_security_group.web.id]

  tags = {
    Name = "Web Server ${count.index + 1}"
  }
}

# ... (app instances)

6. Set Up RDS for the Database Tier

For the data tier, we'll use Amazon RDS:

resource "aws_db_instance" "default" {
  allocated_storage    = 10
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  name                 = "mydb"
  username             = "admin"
  password             = "password"  # Use AWS Secrets Manager in production
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
  db_subnet_group_name = aws_db_subnet_group.default.name
  vpc_security_group_ids = [aws_security_group.db.id]
}

7. Create an Application Load Balancer

Finally, we'll set up an Application Load Balancer to distribute traffic to our web servers:

resource "aws_lb" "web" {
  name               = "web-lb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.web.id]
  subnets            = aws_subnet.public[*].id
}

# ... (target group and listener)

Conclusion

This Terraform configuration sets up a basic 3-tier application architecture on AWS. It includes a VPC with public and private subnets, EC2 instances for the web and app tiers, an RDS instance for the database tier, and an Application Load Balancer for the web tier.

Remember to adjust security settings, use proper secrets management, and optimize the configuration for production use. Always follow AWS best practices and consider factors like cost, performance, and security when deploying your application.

For the complete Terraform code and more detailed explanations, check out our GitHub repository.