Triggering Lambda function with Application Load Balancer
Tags: aws, lambda, beginners, cloud
Published: 2023-11-20 16:54:47

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1x6af9zgsh9csl223r1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqj0d7bt1fhjc8aw6r4u.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gia3520wedy1ddesbd1u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tcvp3d4pgoz1m7rwiev.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ovq0jffsnco6tezuly0.png)

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

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v48ljyhcmmxn204n94bu.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i7gc3iv7c96u3a74oqc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otmhihxtzhpl28c3p0j0.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nvm1emlebjfr9jy4dg4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/liu0991yle5uf8f5p201.png)

Click on **_deploy_** 
Hit the endpoint again. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr6xlhmvj8mz5j8lzp9q.png)


## Conclusion


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tx4q0viqmh1dsnohxr5u.png)

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.