Create an Application Load Balancer with path-based routing using AWS Lambda as target
# 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 = <
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/alb-path-based-route-lambda-tf
terraform apply --auto-approve