Getting started with AWS Lambda [Tutorial]


Written by - Mahnoor Malik
Reviewed by - Deepak Prasad

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.

AWS Lambda synchronous invocation

 

  • 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.

AWS Lambda asynchronous invocation

 

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.

Getting started with AWS Lambda [Tutorial] Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

Now let's test our Lambda function by creating an event. We will provide the event name and then click on Test.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

In order to see what we are receiving in the event, we will add a print statement in our Lambda Function.

Getting started with AWS Lambda [Tutorial]

 

Testing

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.

Getting started with AWS Lambda [Tutorial]

 

 

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.

Getting started with AWS Lambda [Tutorial]

 

Now when we enter the DNS name in the browser again, we can see Hello from Lambda displayed on the webpage.

Getting started with AWS Lambda [Tutorial]

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.

Getting started with AWS Lambda [Tutorial]

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

For now, we will select AmazonSQSFullAccess and then attach this policy to the Execution role of the AWS Lambda function.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

Here we will select the Target to Lambda function and then select our Lambda function i.e. myDemoLambdaFunction from the list of available functions.

 

Getting started with AWS Lambda [Tutorial]

 

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.

 

Getting started with AWS Lambda [Tutorial]

Testing

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.

Getting started with AWS Lambda [Tutorial]

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.

 

Getting started with AWS Lambda [Tutorial]

 

We can clearly see in the logs that the Lambda function has not been executed successfully and has raised an exception.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

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.

Getting started with AWS Lambda [Tutorial]

 

Conclusion

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.

Happy learning!

 

References

What is AWS Lambda?

 

Views: 7

Mahnoor Malik

She is a Computer and Information Systems Engineer with a master's degree in Computer Networks and Systems Security. She excels in backend development, specializing in Java with the Spring Boot framework, and is proficient in other languages such as C, C++, and Python. You can reach out to us on Facebook page.

Categories AWS

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment