Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Application Load Balancer (path-based-route) to AWS Lambda

Created with SnapApplication Load BalancerAWS LambdaAWS Lambdarule:(api/service1)rule:(api/service2)

Create an Application Load Balancer with path-based routing using AWS Lambda as target

This pattern shows how to create an Application Load Balancer with path-based routing along with associated listener and target as AWS Lambda. Implemented in Terraform.


# Create AWS VPC 
resource "aws_vpc" "vpc" {
  cidr_block = var.vpc_cidr

}

# Create public subnet 1
resource "aws_subnet" "public_subnet1" {
  cidr_block = "10.0.1.0/24"
  vpc_id = aws_vpc.vpc.id
  availability_zone = "${var.region}a"
  tags = {
    Name = "Subnet for ${var.region}a"
  }
}

# Create public subnet 2
resource "aws_subnet" "public_subnet2" {
  cidr_block = "10.0.2.0/24"
  vpc_id = aws_vpc.vpc.id
  availability_zone = "${var.region}b"
  tags = {
    Name = "Subnet for ${var.region}b"
  }
}

# Create a route table 
resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }

  tags = {
    Name = "public_rt"
  }
}

# Associate the route table with public subnet 1
resource "aws_route_table_association" "public_rt_table_a" {
  subnet_id      = aws_subnet.public_subnet1.id
  route_table_id = aws_route_table.public_rt.id
}

# Associate the route table with public subnet 2
resource "aws_route_table_association" "public_rt_table_b" {
  subnet_id      = aws_subnet.public_subnet2.id
  route_table_id = aws_route_table.public_rt.id
}

# Create an Internet Gateway
resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.vpc.id
  tags = {
    "Name" = "example-igw"
  }
}

# Create IAM Role for Lambda Function
resource "aws_iam_role" "lambda_role" {
name   = "Lambda_Function_Role"
assume_role_policy = <

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/alb-path-based-route-lambda-tf

Deploy

terraform apply --auto-approve


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the resource: terraform destroy --auto-approve.

Pushparaju Thangavel

Presented by Pushparaju Thangavel

Pushparaju is a Cloud Infrastructure Architect with Amazon Web Services

Follow on LinkedIn