In this tutorial, we will discuss Amazon’s very reliable serverless computing service AWS Lambda. Before starting this tutorial, we’re assuming that you’ve already worked on AWS cloud and you’ve knowledge of AWS compute and database services. We will dive you into the main concepts of AWS Lambda by understanding why we need this service, what is this service, its main agenda, and a hands-on part.
Watch this AWS Certification full course for Beginners video
Here we have the list of topics if you want to jump right into a specific one:
- Cloud Computing
- Need AWS Lambda?
- What is AWS Lambda?
- How does it work?
- Why not other compute services but Lambda?
- Benefits of AWS Lambda
- Limitations of AWS Lambda
- AWS Lambda Pricing
- Use Cases of Lambda
- Hands-On: How to create a Lambda function using Lambda console
Before proceeding towards AWS Lambda, let’s understand its domain cloud computing where AWS 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 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 cloud computing vendors. Here are some of the major players in the marketplace when it comes to Cloud Computing.
If we look into the stats, currently AWS is a pioneer in providing cloud services, as is evident from the google trends graph below:
If we talk of services, AWS Compute services plays a major role when you want to start AWS, as it provides you secure, resizable, computing capacity on cloud.
In AWS Compute services, there are multiple reliable services:
We are going to talk about AWS Lambda today, which is a very reliable serverless compute service.
But why do we need AWS Lambda, when we already have 2 other reliable computing services?
Don’t worry, we will answer all your questions regarding AWS Lambda today in this blog.
Learn more about AWS in this insightful AWS blog!
Watch this AWS Certification full course for Beginners video
Need of AWS Lambda
As you all know how cloud works, so let’s take an example and understand the need of AWS Lambda. Let’s take an example of a website, suppose this website is hosted on AWS EC2, in which currently 90-100 users are reading a blog, and in the back-end, admin user uploads 10 videos on the website for processing.
This increases the load on the server, which triggers the auto scaling feature, so EC2 provisions more number of instances for this task, as hosting and back-end changes are both taking place in the same instance. Autoscaling takes a long time to provision more instances, which eventually becomes a reason for slow actions in the website when the initial spike in the task is received.
This problem was solved using distributed computing. In this, the website is hosted on one Instance and the back-end code runs on another instance.
While the users are reading a blog on the website and the admin user uploads those 10 videos on the website. 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. But video processing still took a lot of time, when the load increased because auto-scaling took time on EC2.
We needed a stateless system to solve this problem, and AWS did exactly this with the launch of AWS Lambda!
How? We shall discuss as we move along in this blog.
So, let’s understand what exactly is AWS Lambda?
What is AWS Lambda?
It is one of the computing services provided by AWS, which is event-driven and serverless.
AWS Lambda is a Stateless Serverless system which helps us run our background tasks in the most efficient manner possible.
From serverless it doesn’t mean that servers are nowhere in play but, you don’t have to worry about the provisioning or management of your servers or instances, it just helps you focus on your main goal i.e., CODING, just put your code in AWS Lambda and you’re good to go. Whatever resources are required for your code in response to your events, it automatically provides you with that. The best feature about AWS Lambda is that you just have to pay for every request made during that time.
Now let’s understand how it works?
Go through the AWS Course in London to get clear understanding of AWS.
What is AWS Lambda video
How does it work?
Before we proceed forward and understand that how AWS Lambda works, first we need to understand few aspects of Lambda-based Application.
- Lambda Function: Whatever the custom codes and libraries that you’ve created are what a Function
- Event Source: Any AWS or custom service that triggers your function and helping in executing its logic.
- Log Streams: As we know that Lambda monitors your function automatically and one can view its metric on CloudWatch directly, but you can also code your function in a way that allows you a custom logging statements to let you analyze the flow of execution and performance of your function to check if it’s working properly.
Learn more about AWS in this AWS training in New York to get ahead in your career!
Using AWS Lambda with S3
In this section, we will see how AWS S3 can be used with AWS Lambda.Let’s take an example where User is uploading an Image in the Website for resizing it.
- The user creates a Lambda function.
- User uploads the code to the Lambda function.
- Then uploads the image from the Website in the S3 bucket as an object.
- After receiving the object, our 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.
Are you preparing for AWS interview? Then here are latest AWS interview questions
Pseudo Code for Lambda function:
<once the image is resized, send the email for successful operation through SQS>
So, from this example, you must be able to figure out that how AWS Lambda performs its tasks in the back-end.
Watch this Problem Solving Tips for Cracking AWS-Cloud Interview Questions video:
Check the below diagram for a summary to it:
Let’s understand the reason behind it.
Why not other computing services but lambda?
As we are aware that AWS Lambda is one of the computing services provided by Amazon and if we talk about other computing services to execute a task, like AWS EC2 & AWS Elastic beanstalk, why should we choose Lambda in place of them. Let’s try to understand this:
Become Master of AWS by going through this online AWS course in Toronto.
Watch this AWS Services for Beginners video:
AWS Lambda VS AWS EC2
As we know that in AWS EC2, one can host a website as well as run and execute the back-end codes.
AWS Lambda is a Platform as a Service (PaaS) with a remote platform to run & execute your back-end code.
AWS EC2 is an Infrastructure as a Service (IaaS) provided with the virtualized computing resources.
No flexibility to log in to compute instances, choose customized operating system or language runtime.
Offers the flexibility to choose the variety of instances, custom operating systems, network & security patches etc.
Just choose your environment where you want to run your code and push the code into AWS Lambda.
For the first time in EC2, you have to choose the OS and Install all the software required and then push your code in EC2.
Environment restrictions, as it is restricted to few languages only.
No environment restrictions are there.
Get certified from top AWS course in Singapore Now
AWS Lambda VS AWS Elastic Beanstalk
AWS Elastic Beanstalk
Deploy and manage the apps on AWS Cloud without worrying about the infrastructure that runs those applications.
Whereas AWS Lambda is used only for running and executing your Back-end code, it can’t be used to deploy an application.
Freedom to select AWS resources, like, choose an EC2 instance type which is optimal for your application.
Whereas in Lambda, you cannot select the AWS resources, like a type of EC2 instance, lambda provides you resources based on your workload.
|It is a stateful system.|
It is a stateless system.
Now that we understand how AWS Lambda plays its part, let’s take a sneak peek on its pros and cons.
Learn Complete AWS at Hyderabad in 24 Hrs.
Watch this AWS Tutorial for Beginners video:
Benefits of AWS Lambda
- Due to its serverless architecture, one need not worry about provisioning or managing servers.
- No need to set up any Virtual Machine (VM).
- Allows the developers to run and execute the codes response to events without thinking of building any infrastructure.
- Pay as you go: Just pay for the compute time taken, only when your code runs.
- Pay only for the used memory, the number of processed code requests and the code execution time rounded up by 100 milliseconds.
- You can easily monitor your code performance in real time through CloudWatch.
Limitations of AWS Lambda
There are several limitations of AWS Lambda due to its hardware as well as its architecture:
- The maximum execution duration per request is set to only 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 between 128 to 1536 MB.
- 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 your functions.
So, these are the limitations of AWS Lambda which are basically there to ensure that the services are used as intended.
Now let’s come to its pricing part!
If you have any doubts or queries related to AWS, do post on AWS Community.
Watch this AWS Certified DevOps Engineer video:
AWS Lambda Pricing
Just like every service provided by AWS, AWS Lambda is also a pay per use service.
- It is based on the number of requests made
- Pay only for the number of requests you made on all your lambda functions.
- Prices are as follows:
- First 1 million requests, every month are for free.
- 20$ per million requests thereafter.
- Also based on the duration
- These prices depend on the amount of memory you allocate to your function.
- First 400,000 GB-seconds per month, up to 3.2M seconds of computing time, are free.
- $0.00001667 FOR EVERY GB-SECOND USED THEREAFTER.
*Source: AWS Official Website
Now that we’ve discussed its pricing, now let’s move forward and investigate its very general use cases.
Use Cases of AWS Lambda
Building a serverless website allows you to focus on your website code. You 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 people are actually taking advantage of its pricing model. Hosting your static website over S3 and then making it available using AWS Lambda makes it easier for the users to keep track of their resources being used and to understand if their code is that feasible or not. That even with the ability to troubleshoot and fix the problem in no time.
Automated Backups of everyday tasks
One can easily schedule the Lambda events and create back-ups in their AWS Accounts. Create the back-ups, check if there are any idle resources or not, generate reports and other tasks which can be implemented by Lambda in no time.
Filter and Transform data
One can easily use it for transferring the data between Lambda and other Amazon Services like S3, Kinesis, Redshift and database services along with the filtering of the data. One can easily transform and load data between Lambda and these services. When we investigate its Industrial use cases, then a very apt implementation of Lambda can be found in a company name Localytics.
Watch this AWS SysOps Administrator Training for Beginners video:
Use case 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, RueLaLa and the New York Times to understand and evaluate the performance of the apps and to engage with the existing as well as the new customers. Their software is employed in more than 37000 apps on more than three billion devices all around the globe.
Regardless of how popular Localytics is now, Localytics faced some serious challenges before they started using lambda.
Let’s see what those challenges were before we discuss how lambda came to the rescue and helped Localytics overcome these challenges, let’s check what were the challenges.
- Billions of data points, that are 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.
- Platform team was more inclined towards enabling self-service for engineering teams.
- Every time a microservice was added, the main analytics processing service for Localytics had to be updated.
The big solution to all these challenges is “Lambda”.
- Localytics now uses AWS to send about 100 billion data points monthly through Elastic Load Balancing where ELB helps in distributing incoming application traffic across multiple targets
- Afterward, it goes to Amazon Simple Queue Service where it enables you to decouple and scale microservices, distributed systems, and serverless applications.
- Then to Amazon Elastic Compute Cloud, and finally into an Amazon Kinesis stream which makes it easy to collect, process, and analyze real-time, streaming data so you can get timely insights and 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 the Amazon Kinesis. Microservices can access the data in parallel with others.
Rest you can understand from the below diagram for the same:
With all the benefits it provides, Lambda has contributed to the popularity of Localytics.
- Lambda rules out the need to provision and manage infrastructure in order to run each Microservice.
- Processing tens of billions of data aren’t as big of a hassle as it were before as Lambda automatically scales up and down with the load.
- Lambda enables the creation of new Microservices to access 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 end our tutorial with running our first lambda function.
Watch this AWS Solution Architect Certification video:
In this Hands-on, we will take you through the step wise guide on how to create a lambda function using lambda console.
Let’s start by creating a Lambda Function.
- After setting the AWS Account.
- Go and type AWS Lambda in your AWS Management console.
- Click on Create a Function.
- You’ll see a setup page shows up where you have to fill up few aspects for your function as the name, runtime, role, you can choose from blueprints as well but here we’re going to author it from scratch.
- Enter the name and all the credentials, now in case of the runtime, you can choose any based on your understanding of that language, we’re choosing NodeJS 8.10, here you can choose from any option like python, java, .Net, Go (these are the languages it supports).
- Then create a role, you’ll have to create a new role if you don’t have one, either you create a new template for the role or leave the template blank.
- Like in our case, we’ve chosen an existing role that we have created
- As here we have already defined our role with the name of service-role/shubh-intel.
- The next step after this is Writing Code for your Lambda Function.
- We’re choosing Lambda Console here, you can choose from different code editors like Cloud9 Editor or on your Local machine.
- You can check your function being created, as here we have created it with the name of example-lambda.
- When you created the function, 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.
return ‘Hello from Lambda!’
- If you want to define the key value then you can, like here we’ve defined key1 and key2 and key 1= ‘Hello from Lambda!’
- Then create the event like we created it with the name of mytestevent click Save and Test in order to run your function.
- After running it, you will get an output where you check the details and you will get the output as below:
Here ends this AWS Tutorial on Amazon Web Services. Wish to learn more? Check out Intellipaat’s AWS Course to get an in-depth understanding of Amazon Web Services!
Congratulations! Now you’ve created and executed your Lambda Function successfully.
Watch this AWS Certification full course for Beginners video
So, from this tutorial, you got a detailed understanding of AWS Lambda workflow, its need, Lambda Functions, how to create lambda functions and its various use cases.
While here we covered quite a bit of Lambda’s core functionality, still there is a lot 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 the exhaustive topic wise assignments, hands-on sessions and various industrial based projects which prepares you from scratch to top-notch understanding of Amazon Web Services.
We would suggest you practice this example which is explained above. If you’re willing to enter in a cloud computing domain or up-skill yourself with this domain, then you must go with AWS Solution Architect Certification Training which will help you understand the most important tools and services that you must learn and practice to become a successful and a productive team member at your workplace.
Happy Learning! 😊