Ex.
15 Create Application Load Balancer Using AWS Lambda function
Use Application Load Balancer (ALB) to trigger a Lambda function via HTTP requests.
Steps to Create ALB with Lambda Target
🔹 Step 1: Create Your Lambda Function
1. Go to AWS Lambda Console → Click Create function.
2. Choose Author from scratch.
3. Provide:
○ Function name (e.g., MyLambdaHandler)
○ Runtime (Python, Node.js, etc.)
Add simple handler code, for example (Node.js):
javascript
CopyEdit
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify("Hello from Lambda behind ALB!"),
};
};
4.
5. Click Create Function.
🔹 Step 2: Add Permission for ALB to Invoke Lambda
Use AWS CLI or Lambda console > Permissions > Add permission:
bash
CopyEdit
aws lambda add-permission \
--function-name MyLambdaHandler \
--statement-id alb-access \
--action "lambda:InvokeFunction" \
--principal elasticloadbalancing.amazonaws.com \
--source-arn arn:aws:elasticloadbalancing:<region>:<account-
id>:loadbalancer/app/<alb-name>/<id>
(You’ll get the ALB ARN in Step 4)
🔹 Step 3: Create a Target Group for Lambda
1. Go to EC2 Console > Target Groups.
2. Click Create target group.
3. Select Target type: Lambda function.
4. Name it (e.g., lambda-target-group).
5. Choose your Lambda function.
6. Click Create.
🔹 Step 4: Create an Application Load Balancer
1. Go to EC2 Console > Load Balancers → Click Create Load Balancer → Choose Application Load
Balancer.
2. Name your ALB (e.g., alb-lambda).
3. Choose Internet-facing, protocol: HTTP (or HTTPS).
4. Select 2+ public subnets.
5. Assign a Security Group that allows inbound traffic on port 80 (or 443).
6. In the Listeners section:
○ Protocol: HTTP
○ Action: Forward to Target Group
○ Choose the Lambda target group you created.
🔹 Step 5: Test the ALB
● Copy the DNS name of the ALB from the console.
● Open it in a browser or use curl:
bash
CopyEdit
curl http://<ALB-DNS-Name>