DEV Community

Cover image for Triggering Lambda function with Application Load Balancer
Parikshit Chavan
Parikshit Chavan

Posted on

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 (0.0.0.0)
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 https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

OR

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

Conclusion

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.

Top comments (4)

Collapse
 
lewiscowles1986 profile image
Lewis Cowles • Edited

Nice article.

Why do you have Cloudfront (Edge) in the middle, rather than outer layers?

Surely Cloudfront is beaming out to the internet at edge locations, so would be far slower than in-data-center traffic; unless the data-center also has a cloudfront distribution. Even then, the weird things you can almost entirely eliminate by not having API-Gateway be the front-door, would almost certainly lead me to not using Cloudfront in the middle of a stack.

It's also mentioned but never shown. Perhaps it was a mistake?

Collapse
 
rdarrylr profile image
Darryl Ruggles

Nice article - thanks for sharing!

Collapse
 
brooklyn2706 profile image
Parikshit Chavan

Thank you for checking out!

Collapse
 
pravinkamane profile image
Pravin Kamane

nice and thanks for sharing. Just to point out one thing that may help others. When I tried this I found that a public subnet is required for the load balancer for the DNS URL to work.