An Interest In:
Web News this Week
- November 20, 2023
- November 19, 2023
- November 18, 2023
- November 17, 2023
- November 16, 2023
- November 15, 2023
- November 14, 2023
Some of Our Sources
- Team Treehouse
- Smashing Apps
- TutsPlus - Design
- Crazy Leaf Design
- Freelance Switch
- Design Modo
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
Before diving into the hands-on, lets take a moment to understand what we are doing and what its being called.
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.
- Elastic Load balancer
- API Gateway
- S3 batch
1. Create a lambda function
Login to your AWS Management console, and go the Lambda service.
Click on *Create function *
Enter the details like function name, choose Python as the runtime language and leave the rest setting as it is.
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 ! ')
Navigate to Load Balancer service in the management console
Select Application Load Balancer.
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.
As mentioned above, give the target group name.
In the Registering Target Group, select our Lambda function that we created.
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.
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...
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.
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
Just past the below code
"statusDescription": "200 OK",
"body": "<h1>Hello from Lambda!</h1>"
In our Lambda function, paste this in our return statement.
Note: isbase64Encoded : False, is with F capital
Click on deploy
Hit the endpoint again.
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: https://dev.to/brooklyn2706/triggering-lambda-function-with-application-load-balancer-2947