• Articles
  • Tutorials
  • Interview Questions

AWS Lambda Tutorial: Learn AWS Lambda from Scratch

Table of content

Show More

Introduction to AWS Lambda

In this tutorial section, we will discuss the most reliable Amazon AWS serverless computing service, AWS Lambda. Here, we’re assuming that you have already worked on the AWS cloud and you’ve some idea about AWS Compute and Database services. This AWS tutorial will walk you through the main concepts of AWS Lambda while making you understand why we need this service, what it is exactly, its main agenda, etc., along with providing a hands-on exercise for you to practice.

Watch this AWS Lambda Tutorial for Beginners video:

Video Thumbnail
Youtube subscribe

Cloud Computing

Cloud ComputingBefore proceeding with AWS Lambda, let’s first understand its domain, Cloud Computing, where AWS has originated from.

Cloud Computing is simply a practice of using a network of remote servers hosted on the Internet to store, manage, and process data, rather than using a local server or a personal computer. For more information on Cloud Computing, you can refer to this informative blog on Cloud Computing.

But, why are we talking about AWS when there are numerous other Cloud Computing vendors? Here are some of the major players in the marketplace when it comes to Cloud Computing.
Introduction
If we look into the stats, currently, AWS is a pioneer in providing cloud services as is evident from the below Google Trends graph:
Cloud Computing Graph
If we talk of services, AWS Compute plays a major role while we work with AWS as it provides us secure and resizable computing capacity on the cloud.

In AWS Compute services, there are multiple reliable services:

AWS Compute services
In this tutorial section, we are going to focus on AWS Lambda, which is a very reliable serverless AWS Compute service.

Why do we need AWS Lambda when we already have two other reliable computing services?

Let’s see.

Need for AWS Lambda

As we all know how cloud works, let’s go ahead and understand the need for AWS Lambda, by taking the example of a website.

Suppose, a website is hosted on AWS EC2, in which currently 90–100 users are reading a blog and, in the back-end, the Admin User uploads 10 videos on the website for processing.
Need aws Lamda
This increases the load on the server, which triggers the auto-scaling feature, and hence EC2 provisions more number of instances to meet this requirement—since hosting and back-end changes are both taking place in the same instance.

However, auto-scaling takes a long time to provision more instances, which eventually becomes a reason for slow actions on the website when the initial spike in the task is received.
Need aws lamda website
This problem can be solved using distributed computing. In this, the website is hosted on one instance and the back-end code runs on another. While the users are reading a blog on the website, the Admin User can upload those 10 videos on the website.

This time, the website will forward the task of video processing to another instance. This makes the website resistant to video processing, and therefore website performance will not be impacted. However, video processing still takes a lot of time, when the load increases, because auto-scaling takes time on EC2.
website systemHere, we need a stateless system to solve this problem, and AWS does exactly this with the launch of AWS Lambda!

How? We shall discuss as we move along in this blog.

So, let’s first understand what exactly AWS Lambda is?

What is AWS Lambda?

AWS Lambda is one of the computing services provided by AWS, which is event-driven and serverless. It is a stateless serverless system that helps us run our background tasks in the most efficient manner possible.
What is AWS Lambda
Here, being serverless doesn’t mean that servers are nowhere in the play. Instead, it means that we don’t have to worry about the provisioning or management of our servers or instances; it just helps us focus on our main goal, i.e., coding. We just have to put our code in AWS Lambda, and we’re good to go!

Whatever resources are required for our code in response to our events, AWS Lambda automatically provides us. The best feature of it is that we just have to pay for every single request made during the time.

Now, let’s understand how it works.

How does it work?

Before we proceed and understand how AWS Lambda works, first, we need to understand a few aspects of the Lambda-based application.

Building Blocks

  • Lambda function: Whatever custom codes and libraries that we create are called a function.
  • Event source: Any AWS or custom service that triggers our function and helps in executing its logic
  • Log streams: Lambda monitors our function automatically and one can view its metric on CloudWatch, directly, but we can also code our function in a way that it provides us custom logging statements to let us analyze the flow of execution and performance of our function to check if it’s working properly.

Using AWS Lambda with S3

Using AWS Lambda with S3In this section, we will see how AWS S3 can be used with AWS Lambda. Let’s take an example where a user is uploading an image on a website for resizing it.

  • First, the user creates a Lambda function.
  • The user uploads the code to the Lambda function.
  • Then, the user uploads the image from the website to the S3 bucket as an object.
  • After receiving the object, the S3 bucket triggers the Lambda function.
  • Then, the Lambda function does its job by resizing the image in the back-end and sends a successful completion email through SQS.

The pseudo-code for the Lambda function:

<code to resize image>
<once the image is resized, send the email for successful operation through SQS>

From this example, it is clear how AWS Lambda performs its tasks in the back-end.

Check the below diagram for a summary to it:
diagram for a summary

Now, let’s understand the reason behind it.

Why not other computing services but Lambda?

As we are aware, AWS Lambda is one of the computing services provided by Amazon. Now, when we have other computing services, such as AWS EC2 or AWS Elastic Beanstalk, to execute such a task, why do we choose AWS Lambda? Let’s see.

AWS Lambda vs AWS EC2

AWS Lambda VS AWS EC2
As we know, in AWS EC2, we can host a website as well as run and execute the back-end codes. So, how is AWS Lambda different from it?

AWS Lambda AWS EC2
A Platform as a Service (PaaS) with a remote platform to run and execute the back-end code An Infrastructure as a Service (IaaS) provided with virtualized computing resources
No flexibility to log in to compute instances and choose a customized operating system or language runtime Has the flexibility to choose from a variety of instances, custom operating systems, network and security patches, etc.
Choose the environment where we want to run the code and push the code into AWS Lambda The first time, we have to choose the OS and install all the software required and then push our code into EC2
Has environment restrictions to a few languages No environment restrictions at all

AWS Lambda vs AWS Elastic Beanstalk

AWS Lambda VS AWS Elastic Beanstalk

AWS Elastic Beanstalk AWS Lambda
To deploy and manage apps on AWS cloud Only to run and execute the back-end code; not for deploying apps
Freedom to select AWS resources optimal for the apps Lambda provides AWS resources based on our workload
A stateful system A stateless system

Now that we understand how AWS Lambda plays its part, let’s take a sneak peek on its pros and cons.

Benefits of AWS Lambda

AWS Lambda provides us the following benefits:

Benefits of AWS Lambda

  • Due to its serverless architecture, no need to provision or manage servers
  • No need to set up any virtual machine (VM)
  • Allows developers to run and execute codes in response to events without the need for building any infrastructure
  • Pay as you go: Just pay for the compute time taken, only when the code runs. Also, pay only for the used memory and the number of processed code requests, and the code execution time is rounded up by 100 milliseconds.
  • Easily monitor code performance in real time through CloudWatch

Limitations of AWS Lambda

Limitations of AWS LambdaAlthough AWS Lambda offers so many benefits, we might face a few limitations while working with AWS Lambda, due to its various characteristics w.r.t. its hardware and its architecture. Some of these limitations are listed below:

  • The maximum execution duration per request is set to 300 seconds (15 minutes)
  • In the case of hardware, the maximum disk space provided is 512 MB for the runtime environment, which is very less
  • Its memory volume varies from 128 MB to 1,536 MB
  • The event request body cannot exceed more than 128 KB
  • Its code execution timeout is only 5 minutes
  • Lambda functions write their logs only to CloudWatch, which is the only tool available in order to monitor or troubleshoot our functions

These limitations of AWS Lambda basically exist to ensure that the services are used as intended.

Now, let’s come to its pricing part!

AWS Lambda Pricing

Just like any other AWS service, AWS Lambda is also a pay-per-use service.

AWS Lambda Pricing

As per Amazon’s official website, below are the prices for using AWS Lambda:

  • Based on the number of requests made: Pay only for the number of requests made on our Lambda functions. Here, the prices are as follows:
    • For the first 1 million requests, every month, the service is free
    • Thereafter, pay US$20/million requests
  • Based on the duration: The pricing here depends on the amount of memory we allocate to our functions:
    • The first 400,000 GB-seconds/month, up to 3.2 M seconds of computing time is free
    • Thereafter, pay US$0.00001667 for every GB-second used

Cloud Computing EPGC IITR iHUB

Now that we’ve discussed its pricing, let’s move forward and investigate some of the general use cases of AWS Lambda.

Use Cases of AWS Lambda

Serverless Websites

Serverless WebsitesBuilding a serverless website allows us to focus on our website code. This way, we don’t have to manage and operate its infrastructure. Sounds cool, isn’t it? Yes. This is the most common and interesting use case of AWS Lambda, where we are actually taking advantage of its pricing model. Hosting our static website over S3 and then making it available using AWS Lambda makes it easier for us to keep track of our resources being used and to understand if our code is feasible or not, all these we get with the ability to troubleshoot and fix any problem in no time.

Automated Backups of Everyday Tasks

Automated Backups of everyday tasksWe can easily schedule Lambda events and create backups in our AWS accounts. Creating backups, checking if there are any idle resources, generating reports, and other tasks can be implemented by Lambda in no time.

Filtering and Transforming Data

Filter and Transform dataWe can easily transfer data between Lambda and other Amazon Services, such as S3, Kinesis, Amazon Redshift, and other database services, along with the filtering of the data. We can easily transform and load data between Lambda and these services. When we investigate its industrial use cases, a very apt implementation of Lambda can be found in a company named Localytics.

How is AWS Lambda used in Localytics?

Localytics is a Boston-based, web and mobile app analytics and engagement company. Its marketing and analytics tools are being extensively used by some major brands, such as ESPN, eBay, Fox, Salesforce, Rue La La, and The New York Times, to understand and evaluate the performance of their apps and to engage with the existing as well as the new customers.

Use case in LocalyticsThe software developed by Localytics is employed in more than 37,000 apps on more than 3 billion devices all around the world.

Regardless of how popular Localytics is now, Localytics had faced some serious challenges before they started using Lambda.

Let’s see what the challenges were before we discuss how Lambda came to the rescue and helped Localytics overcome these challenges.

Challenges

  • Billions of data points uploaded every day from different mobile applications running Localytics analytics software are fed to the pipeline that they support.
  • Additional capacity planning, utilization monitoring, and infrastructure management were required since the engineering team had to access subsets of data in order to create new services.
  • The platform team was more inclined toward enabling self-service for engineering teams.
  • Every time a microservice was added, the main analytics processing service for Localytics had to be updated.

Challenges
After a while, they realized that the only solution for all these challenges is Lambda.

The Solution

  • Localytics now uses AWS to send about 100 billion data points monthly through Elastic Load Balancing where ELB helps in distributing the incoming application traffic across multiple targets.
  • Afterward, it goes to Amazon Simple Queue Service where it enables us to decouple and scale microservices, distributed systems, and serverless applications.
  • Then, it reaches Amazon Elastic Compute Cloud and, finally, into an Amazon Kinesis stream that makes it easy to collect, process, and analyze real-time, streaming data so that we can get timely insights and can react quickly to new information.
  • With the help of AWS Lambda, a new microservice is created for each new feature of marketing software to access Amazon Kinesis. AWS Microservices can access data in parallel.

The below diagram explains this whole process in detail:
solutions
With all the benefits it provides, Lambda has contributed much to the popularity of Localytics.

Check out this video on the AWS Course to enhance your knowledge:

Video Thumbnail

Benefits

  • Lambda rules out the need to provision and manage infrastructure in order to run each microservice.
  • Processing tens of billions of data is not as big of a hassle as it was before since Lambda automatically scales up and down with the load.
  • Lambda enables the creation of new microservices to access the data stream by decoupling the product engineering efforts from the platform analytics pipeline, eliminating the need to be bundled with the main analytics applications.

After addressing AWS Lambda, its function, along with its workflow and use cases, now, let’s move on with running our ever first Lambda function.

Hands-on

In this hands-on section, we will take you through the steps on how to create a Lambda function using the Lambda console.

Let’s start off by creating a Lambda function.

  • Login to your AWS account
  • Go and type AWS Lambda in the AWS Management console
  • Click on Create a Functionhands on
  • You’ll see a setup page, in which you have to fill up a few aspects for your function such as the name, runtime, role, etc. You can choose from blueprints as well, but here we’re going to author it from scratch

hands on graph

  • Enter the name and all the credentials; in the case of the runtime, you can choose any based on your understanding of that language; we’re choosing NodeJS 8.10 from among the options such as Python, Java, .Net, and Go (these are the languages it supports)

languages it supports

  • Create a role; you’ll have to create a new role if you don’t have one; either create a new template for the role or leave the template blank; in our case, we’ve chosen an existing role

existing role

  • Here, we have already defined our role with the name service-role/shubh-intel

defined our role

  • The next step is writing the code for your Lambda function; we’re choosing Lambda Console here; you can choose from different code editors like Cloud9 Editor or anything on your local machine
  • Check the function being created; here, we have created it with the name example-lambda

function being created

  • Once the function is created, you will be directed to a Function Code screen, where you will be defining your function; either you can choose the code from below or you can make your own template for it; it’s quite easy!

Function Code

exports.handler = async (event) => {// TODO implement
return ‘Hello from Lambda!’
};
  • If you want to define the key-value, then you can define the same as follows: key1 and key2 and key 1= ‘Hello from Lambda!’
  • Then, create an event like below with the name mytestevent and click on Save and test in order to run your functionTest in order
  • After running it, you will get an output as below:

check the detailsCongratulations!

You’ve created and executed your first Lambda function successfully!

Conclusion

Here ends this AWS tutorial on AWS Lambda function.

From this tutorial, you got a detailed understanding of AWS Lambda, its workflow, various functions, how to create Lambda functions, its need, and various use cases.

While here we have covered quite a bit of Lambda’s core functionality, there is a lot more to know about AWS Lambda. If you’re looking forward to learning more about it, then you must go for a structured AWS training provided by Intellipaat, where you will work on various case-based scenarios, along with several topic-wise assignments, hands-on sessions, and various industrial-based AWS projects that would prepare you for grabbing a top-notch job.

We would suggest you practice the above example. If you’re willing to upskill yourself to enter into the Cloud Computing domain, then enroll in this AWS Master Certification Training that will help you understand the most important tools and services you must learn and practice to become a successful and productive team member at your workplace.

Happy learning! 😊

Course Schedule

Name Date Details
AWS Certification 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Cloud Computing Associate

Rupinder is a distinguished Cloud Computing & DevOps associate with architect-level AWS, Azure, and GCP certifications. He has extensive experience in Cloud Architecture, Deployment and optimization, Cloud Security, and more. He advocates for knowledge sharing and in his free time trains and mentors working professionals who are interested in the Cloud & DevOps domain.