Table of Contents
This is an introductory tutorial about AWS Lambda. First, we will have a brief overview of AWS Lambda. Then we will learn about synchronous and asynchronous invocations of AWS Lambda functions. After that, we will walk through the process of the creation of Lambda functions. Then we will also do hands-on related to synchronous and asynchronous invocations of AWS Lambda functions. So let's get started!
What is AWS Lambda?
Let's look at some points that explain AWS Lambda.
- Lambda is a compute service that lets us run code without provisioning or managing servers.
- Lambda runs our code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring, and logging.
- We can run code for virtually any type of application or backend service with Lambda. All we need to do is supply our code in one of the languages that Lambda supports.
- Lambda runs our function only when needed and scales automatically, from a few requests per day to thousands per second.
- We pay only for the compute time that we consume—there is no charge when the code is not running.
Invoking AWS Lambda Functions
We can invoke AWS Lambda functions directly using the Lambda console, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits. We can also configure other AWS services to invoke our function, or we can configure Lambda to read from a stream or queue and invoke our function. Note that we can choose to invoke the Lambda function synchronously or asynchronously.
- With synchronous invocation, we wait for the function to process the event and return a response.
- With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. For asynchronous invocation, AWS Lambda handles retries and can send invocation records to a destination.
Create AWS Lambda Function
First, we will log in to our AWS account then type Lambda under the Services tab. After that, we will click on Create Function that will take us to the following screen. Here we will select Author from scratch. Then we will provide the function name. After that we will select Runtime for our Lambda function, here we will select Python 3.7. You can select any other Runtime that Lambda supports. We will be using the default Execution Role for our Lambda function, we will update it later. We will leave the rest of the configurations to default and then click on Create function.
Here we can see that we have an editor for writing our Python code. Note that the console code editor supports only Node.js, Python, and Ruby. We can also upload our project code directly. Here we have a sample code that returns a JSON object having a status code and a message. We will leave it as it is for now.
Now let's test our Lambda function by creating an event. We will provide the event name and then click on Test.
In the Execution Results, we can see that our Lambda function executed successfully and returned a Response as shown earlier. Note that this was a synchronous invocation.
Integrating Lambda with Application Load Balancer
We can use a Lambda function to process requests from an Application Load Balancer. Elastic Load Balancing supports Lambda functions as a target for an Application Load Balancer. Elastic Load Balancing invokes our Lambda function synchronously with an event that contains the request body and metadata. Our Lambda function processes the event and returns a response document to the load balancer in JSON. Elastic Load Balancing converts the document to an HTTP success or error response and returns it to the user.
Create Application Load Balancer
Now we will learn that how we can invoke a Lambda Function from an Application Load Balancer. To learn how to create an Application Load Balancer you can check out our tutorial. The only difference will be that the target type in the current case is Lambda function. Here we will provide the Target group name i.e. mylambdatg.
To ensure that our load balancer routes traffic to the target group we will register targets. Here we will choose the Lambda function we created i.e myDemoLambdaFunction from the list.
Now when we go to the Permissions section of our Lambda Function we can see that there is a policy statement that is allowing access of our ALB to Lambda Function. Note that resource-based policy lets you grant permissions to other AWS accounts or services on a per-resource basis. Here we can see that the Effect is Allow for Service elasticloadbalancing.amazonaws.com for the Action lamba:InvokeFunction on Resource arn:aws:lambda:us-east-2:407566125470:function:myDemoLambdaFunction only when AWS:SourceArn is the ARN of Application Load Balancer.
In order to see what we are receiving in the event, we will add a print statement in our Lambda Function.
Now we will copy the DNS name of our Load Balancer and enter it in the browser. We will see that a file is downloaded with the text "Hello from Lambda". But we wanted the text to be displayed on the webpage. Now let's see how it can be done.
Lambda function should return the response in the following format to the Application Load Balancer. Here "Content-Type": "text/html" tells that the text should be displayed on the webpage.
Now when we enter the DNS name in the browser again, we can see Hello from Lambda displayed on the webpage.
Now let's have a look at the logs. For that we will go to the Monitor tab of our Lambda Function then we will click on our Recent Invocation of the Lambda Function. Here we can clearly see the call Application Load Balancer made to our Lambda function. Note that Lambda logs all requests handled by our function and automatically stores logs generated by our code through Amazon CloudWatch Logs.
Now let's have a look at an asynchronous invocation of our Lambda Function.
Integrating Lambda with Amazon Event Bridge
Here we will see that how the Lambda function is invoked by an Event Bridge Rule asynchronously. This is how things will work
- We will create an Event Bridge Rule that will have our Lambda function as its target and it will be called every 1 minute.
- Then we will define a Dead Letter Queue using Amazon SQS.
- In case of failure in the processing of the Lambda function, it will be retried twice.
- If it still fails, then the event will be sent to the Dead Letter Queue.
Create queue using Amazon SQS
First, we will type Simple Queue Service under the Services tab. Then we will click on Create Queue. After that, we will provide the name of the queue and leave the rest of the configurations to default.
Attach policy to Lambda Execution Role
We will go to the Configuration tab of our Lambda function and click on Permissions. Here we can see our default Execution Role which was created while creating the Lambda function. We will click on it.
It will take us to the following screen. Here we can see the policy attached to this role. We will attach another policy so that the AWS Lambda function has permission to call SendMessage on SQS.
For now, we will select AmazonSQSFullAccess and then attach this policy to the Execution role of the AWS Lambda function.
Now we will go to the Configuration tab, click on Asynchronous Invocation and then click on Edit. In asynchronous invocation, we can define how many Retry Attempts we want and a Dead-Letter queue service if we want to.
Under Dead-letter queue service, we will select Amazon SQS and then select mylambdadlq that we just created. Note that we can send unprocessed events from an asynchronous invocation to an Amazon SQS queue or Amazon SNS topic.
Create Amazon Event Bridge Rule
Now we will select Event Bridge under the Services tab and click on Create Rule. Note that here we will create a rule that performs an AWS action regularly on a set schedule. Here we will provide the name of the rule i.e. DemoLambdaRule. Under Define pattern, we will select Schedule and fix the rate to 1 minute. Now our AWS Lambda function will be invoked asynchronously every minute.
Here we will select the Target to Lambda function and then select our Lambda function i.e. myDemoLambdaFunction from the list of available functions.
Now when we look at the Resource-based policy of our lambda function, we can view in the policy document that a source with ARN "arn:aws:event:us-east-2:407566125470:rule/DemoLambdaRule" has permission to invoke myDemoLambdaFunction.
Since the AWS Lambda function will be invoked every minute now we can see that happening in the logs. We will go to the Monitor tab and click on Recent Invocation and can clearly see that the lambda has been invoked asynchronously.
Now in order to view the Dead-letter queue in operation, we will change our code and raise Exception deliberately so that we can send unprocessed events from an asynchronous invocation to an Amazon SQS queue. Note that the Lambda function will retry twice before sending the unprocessed events to SQS. One important thing to mention over here is that the AWS Lambda function should be idempotent so that in case of retries, the result is the same.
We can clearly see in the logs that the Lambda function has not been executed successfully and has raised an exception.
Now we will go to Amazon SQS, click on our queue i.e. mylambdadlq, and then click on Send and receive messages. Here we will poll for messages. We can clearly view the messages received every time an event is unprocessed.
Now when we click on the message we can see in the Attributes the ErrorMessage i.e. Something went wrong that we have set in our code.
With this, we have come to the end of our tutorial. In this tutorial, we learned about AWS Lambda and walked through the process of synchronous and asynchronous invocations of AWS Lambda functions from Application Load Balancer and Amazon Event Bridge respectively.
Stay tuned for some more informative tutorials coming ahead. Feel free to leave any feedback or query in the comments section.