Overview
This article explores how to implement path-based routing using Amazon Web Services (AWS) Application Load Balancer (ALB) with EC2 instances through Terraform. By leveraging Infrastructure as Code (IaC), organizations can automate the provisioning and management of scalable, fault-tolerant web architectures.

Introduction to Path-Based Routing with ALB

Path-based routing allows traffic to be directed to different backend services based on the URL path. AWS Application Load Balancer supports this functionality, enabling organizations to serve multiple applications under a single domain. This reduces cost, simplifies DNS management, and enhances overall infrastructure scalability.

Why Use Terraform for ALB and EC2 Deployment

Terraform, an open-source Infrastructure as Code tool by HashiCorp, allows users to define and provision data center infrastructure using a declarative configuration language. Using Terraform to automate the deployment of ALB and EC2 instances provides the following benefits:

  • Repeatability: Infrastructure can be version-controlled and reused. 
  • Scalability: Easily manage multiple environments (dev, test, prod). 
  • Speed: Rapidly spin up and tear down resources. 
  • Auditability: Maintain a clear, trackable infrastructure change history. 

Architecture Overview

The architecture consists of the following components:

  • AWS VPC with public and private subnets 
  • Application Load Balancer (ALB) for routing traffic 
  • Two or more EC2 instances, each hosting a unique service (e.g., /app1, /app2) 
  • Security groups configured to allow HTTP traffic 

Path-based routing is configured such that:

  • Requests to /app1 are routed to EC2 Instance A 
  • Requests to /app2 are routed to EC2 Instance B 

Terraform Implementation Steps

  1. Provider and VPC Setup: Define the AWS provider and create a VPC with public subnets and route tables. 
  2. Security Groups: Configure security groups for the ALB and EC2 instances to allow HTTP traffic on port 80. 
  3. Launch EC2 Instances: Use Amazon Linux AMI and user data scripts to install and start a simple web server (e.g., Apache or NGINX) for each instance. 
  4. Create the ALB: Define the ALB resource, along with target groups for each EC2 instance, and attach listeners. 
  5. Configure Path-Based Routing: Set up listener rules to forward traffic to the appropriate target group based on the request path. 
  6. Output: Provide the ALB DNS name for testing access to /app1 and /app2. 

Sample Terraform Snippet for Listener Rules

hcl

resource “aws_lb_listener_rule” “app1_rule” {

  listener_arn = aws_lb_listener.http_listener.arn

  priority     = 100

  actions {

    type             = “forward”

    target_group_arn = aws_lb_target_group.app1_tg.arn

  }

  conditions {

    path_pattern {

      values = [“/app1*”]

    }

  }

}

Testing and Validation

After applying the Terraform configurations using terraform apply, users can navigate to the ALB DNS name followed by the path (/app1 or /app2) in a browser to verify correct routing. Each path should respond with a message from its respective EC2 instance.

Conclusion

Implementing path-based routing with AWS Application Load Balancer and EC2 using Terraform provides a powerful and scalable solution for modern web applications. It ensures high availability, modularity, and efficient resource utilization. Organizations looking to streamline their cloud infrastructure deployments can benefit significantly from integrating Terraform into their DevOps pipeline.