AWS Lambda with Application Load Balancer (ALB): A Complete Guide
Introduction
AWS Lambda is a serverless computing service that executes code in response to triggers without managing servers. One such trigger is Application Load Balancer (ALB), which enables direct HTTP(S) access to Lambda functions.
In this guide, we will walk through:
- Creating an AWS Lambda function
- Setting up an Application Load Balancer (ALB)
- Configuring ALB to trigger Lambda
- Understanding event object formats and execution flow
Step 1: Creating an AWS Lambda Function
To integrate AWS Lambda with ALB, you first need to create a Lambda function.
Steps to Create a Lambda Function:
- Go to AWS Lambda Console → Click Create Function.
- Choose “Author from Scratch”.
- Enter Function Name:
myALBLambdaFunction
. - Select Runtime:
Python 3.x
(or any preferred runtime). - Set Execution Role: Use an existing role or create a new one with Lambda permissions.
- Click “Create Function”.
Example Python Lambda Function:
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from AWS Lambda via ALB!')
}
Deploy the function and test it using AWS Lambda’s built-in test feature.
Step 2: Creating a Target Group for Lambda
A Target Group is required to link the Application Load Balancer with the Lambda function.
Steps to Create a Target Group:
- Go to AWS EC2 Console → Target Groups → Create Target Group.
- Select Target Type → Choose Lambda Function.
- Enter Target Group Name:
lambda-target-group
. - Choose Lambda Function: Select the function you created earlier.
- Select Latest Version or Alias → Click Create Target Group.
✅ Your ALB will now be able to route traffic to the Lambda function.
Step 3: Creating an Application Load Balancer (ALB)
Steps to Set Up an ALB:
- Go to AWS EC2 Console → Load Balancers → Create Load Balancer.
- Select Application Load Balancer (ALB).
- Enter Load Balancer Name:
lambda-alb
. - Choose Scheme:
Internet-facing
. - Select Availability Zones and VPC.
- Set Up Security Group:
- Open port 80 for HTTP traffic.
- Open port 443 for HTTPS if needed.
- Configure Routing:
- Listener: Set HTTP (Port 80).
- Forward requests to the Target Group (lambda-target-group) created earlier.
- Click “Create Load Balancer”.
✅ Once the ALB is created, it will be in the provisioning state. Wait until it becomes active.
Step 4: Testing ALB-to-Lambda Integration
Once the ALB is active, test it by sending an HTTP request to the ALB’s DNS name.
Test Using cURL Command:
curl http://<ALB-DNS-Name>
Expected Output:
{
"statusCode": 200,
"body": "Hello from AWS Lambda via ALB!"
}
✅ This confirms that ALB is successfully forwarding traffic to Lambda.
Event Object Format for ALB-Lambda Integration
When ALB invokes a Lambda function, it passes an event object containing request details.
Example ALB Event Object:
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target-group/1234567890abcdef"
}
},
"httpMethod": "GET",
"path": "/",
"queryStringParameters": {
"param1": "value1"
},
"headers": {
"accept": "application/json",
"host": "lambda-alb.amazonaws.com"
},
"body": null,
"isBase64Encoded": false
}
✅ The Lambda function can process the request details from this event object.
Step 5: Understanding Execution Flow
- ALB receives an HTTP request.
- ALB forwards the request to the Lambda function.
- Lambda executes the function and returns the response.
- ALB synchronously waits for the response and sends it back to the client.
💡 Key Takeaway: ALB invokes Lambda synchronously, meaning it waits for execution to complete before returning a response.
Conclusion
AWS Lambda’s integration with Application Load Balancer (ALB) allows seamless HTTP-based execution, enabling direct request handling.
Advantages of Using ALB with Lambda: ✅ No need for API Gateway. ✅ Scalable and cost-effective. ✅ Supports multiple routing rules.
🚀 Start Learning AWS Lambda Today!
- 📩 Subscribe for updates: https://learning-ocean.com/subscribe/
- 🎓 Explore paid courses: https://courses.learning-ocean.com