Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 20, 2023 04:54 pm GMT

Triggering Lambda function with Application Load Balancer

This is a basic hands-on on triggering AWS Lambda function via Application Load Balancer DNS.

So, here is what we are going to do:

  • Create a Lambda function
  • Create an ALB (application load balancer)
  • Create a target group for the ALB
  • Set our Lambda function as the target grp
  • Trigger the lambda function and return the response whenever the user clicks the load balancer DNS

Image description

Before diving into the hands-on, lets take a moment to understand what we are doing and what its being called.

Synchronous Invocation

The concept of synchronous invocation typically refers to invoking a Lambda function and waiting for the response in a synchronous manner, where the calling application or service waits for the Lambda function to complete and returns the result immediately.

User invocations:

  1. Elastic Load balancer
  2. API Gateway
  3. CloudFront
  4. S3 batch

1. Create a lambda function

Login to your AWS Management console, and go the Lambda service.
Click on *Create function *

Image description

Enter the details like function name, choose Python as the runtime language and leave the rest setting as it is.

Image description

This will be the sample code that will be present.
We can change the return statement to anything you like:
body': json.dumps('Hello from Lambda! this is Parikshit ! ')

Creating ALB

Navigate to Load Balancer service in the management console
Select Application Load Balancer.

Image description

Fill in the details as given in the image

In Network Mapping , select all the checkboxes, i.e we are selecting all the AZs.

Let's create a new security group

Create a new security group, which has http incoming traffic from any address (
And default outgoing traffic.

Creating Target Group

In the listeners and routing section,
our target group for the ALB is the Lambda function that we created.
Click on the create new target group , select the Lambda function.

Image description

As mentioned above, give the target group name.
Click Next

In the Registering Target Group, select our Lambda function that we created.

Image description

Click on Create target group

Now back to our Create application load balancer page, hit refresh and select our new target group that we just created.

Image description

Keep rest configuration to default and click on Create load balancer

You can view the load balancer. It may take some time to provision.

Now, you can go the load balancer that you created, it will look something like this...

Image description

Copy the DNS name for the ALB and paste it into the browser.
A file will get downloaded, which is the response.txt for our lambda function.

Image description

Now, you can make changes to our lambda function, and print whatever you want. After making any changes, you will have to save the test event, give it a name, and hit deploy.

Getting the response on the ALB Webpage

Finally, we don't want to have our lambda function response downloaded in a text file, instead, we will display it on our ALB endpoint webpage.

To do this go to


Just past the below code

"statusCode": 200,
"statusDescription": "200 OK",
"isBase64Encoded": False,
"headers": {
"Content-Type": "text/html"
"body": "<h1>Hello from Lambda!</h1>"

In our Lambda function, paste this in our return statement.
Note: isbase64Encoded : False, is with F capital

Image description

Click on deploy
Hit the endpoint again.

Image description


Image description

So, we Trigger our lambda function whenever we hit the ALB endpoint, and the response is displayed on the webpage.

You can monitor how many times the function has been invoked in the Monitor tab for the lambda function.

Original Link:

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To