DEV Community

Syed Nashet Ali
Syed Nashet Ali

Posted on

Serverless Computing - AWS Lambda - Amazon Web Services

What is Serverless?
Serverless is a term that generally refers to serverless applications. Serverless applications are ones that don’t need any server provision and do not require to manage servers.

What is AWS Lambda?
AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. Therefore you don’t need to worry about which AWS resources to launch, or how will you manage them. Instead, you need to put the code on Lambda, and it runs.

In AWS Lambda the code is executed based on the response of events in AWS services such as add/delete files in S3 bucket, HTTP request from Amazon API gateway, etc. However, Amazon Lambda can only be used to execute background tasks.

AWS Lambda function helps you to focus on your core product and business logic instead of managing operating system (OS) access control, OS patching, right-sizing, provisioning, scaling, etc.

In this AWS Lambda tutorial for beginners, you will learn:

How does AWS Lambda work?
Events that Trigger AWS Lambda
AWS Lambda Concepts
AWS Lambda VS AWS EC2
AWS Lambda VS AWS Elastic Beanstalk
Use Cases of AWS Lambda
Best practices of Lambda function
When not to use AWS Lambda
Advantages of using AWS Lambda
Limitations of AWS Lambda
How does AWS Lambda work?
The following AWS Lambda example with block diagram explains the working of AWS Lambda in a few easy steps:
Step 1: First upload your AWS Lambda code in any language supported by AWS Lambda. Java, Python, Go, and C# are some of the languages that are supported by AWS Lambda function.

Step 2: These are some AWS services which allow you to trigger AWS Lambda.

Step 3: AWS Lambda helps you to upload code and the event details on which it should be triggered.

Step 4: Executes AWS Lambda Code when it is triggered by AWS services:

Step 5: AWS charges only when the AWS lambda code executes, and not otherwise.

This will happen in the following scenarios:

Upload files in an S3 bucket
When HTTP get/post endpoint URL is hit
For adding/modifying and deleting Dynamo DB tables
In the process of data streams collection
Push notification
Hosting of website
Email sending
Note: You should remember that you will charge for AWS services only when the AWS Lambda code executes, else you don’t need to pay anything.
Events that Trigger AWS Lambda
Here, are Events which will be triggered when you use AWS Lambda.

Insert, updating and deleting data Dynamo DB table
To include push notifications in SNS
To search for log history in CloudTrail
Entry into an S3 object
DynamoDB can trigger AWS Lambda whenever there is data added, modified, and deleted in the table.
Helps you to schedule the event to carry out the task at regular time pattern.
Modifications to objects in S3 buckets
Notifications sent from Amazon SNS.
AWS Lambda can be used to process the CloudTrail logs
API Gateway allows you to trigger AWS Lambda on GET/POST methods.
AWS Lambda Concepts
Function:

A function is a program or a script which runs in AWS Lambda. Lambda passes invocation events into your function, which processes an event and returns its response.

Runtimes:

Runtime allows functions in various languages which runs on the same base execution environment. This helps you to configure your function in runtime. It also matches your selected programming language.

Event source:

An event source is an AWS service, such as Amazon SNS, or a custom service. This triggers function helps you to executes its logic.

Lambda Layers:

Lambda layers are an important distribution mechanism for libraries, custom runtimes, and other important function dependencies. This AWS component also helps you to manage your development function code separately from the unchanging code and resources that it uses.

Log streams:

Log stream allows you to annotate your function code with custom logging statements which helps you to analyse the execution flow and performance of your AWS Lambda functions.

How to use AWS Lambda
Now, we will learn how to use AWS Lambda with AWS Lambda example:

Step 1) Step 1) Open AWS Lambda URL
Goto https://aws.amazon.com/lambda/ and Get Started

Get Started with AWS Lambda
Step 2) Create an account
Next, Create an account or sign in with your existing account

Step 3) Edit the code & Click Run,
In the next Lambda page,

Edit the code
Click Run
AWS Lambda Run Code
Step 4) Check output
You will see output

AWS Lambda Output

AWS Lambda VS AWS EC2
Here, are some major differences between AWS Lambda and EC2.

Parameters AWS Lambda AWS EC2
Definition AWS Lambda is a Platform as a Service (PaaS). It helps you to run and execute your backend code. AWS EC2 Is an Infrastructure as a Service (laaS). It provides virtualized computing resources.
Flexibility Does not offers any flexibility to log in to compute instances. It allows you to choose a customized operating system or language runtime. Offers the flexibility to select the variety of instances, customoperating systems, security patches, and network, etc.
Installation process You need to select your environment where you want to runthe 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 It is restricted to fewlanguages. No environment restrictions.
AWS Lambda VS AWS Elastic Beanstalk
Here, are some major differences between AWS Lambda and Elastic Beanstalk.

Parameters AWS Elastic Beanstalk AWS Lambda
Main task Deploy and manage the apps on AWS Cloud without worrying about the infrastructure which runs those applications. AWS Lambda is used for running and executing your Back-end code. You can’t use it to deploy an application.
Selection of AWS resources It gives you a Freedom to select AWS resources; For example, you can choose EC2 instance which is optimal according to your application. You can’t select the AWS resources, like a type of EC2 instance, Lambda offers resources based on your workload.
Type of system It is a stateful system. It is a stateless system.
Use Cases of AWS Lambda
AWS Lambda used for a wide range of applications like:

Helps you for ETL process
Allows you to perform real-time file processing and real-time stream processing
Use for creating web applications
Use in Amazon products like Alexa Chatbots and Amazon Echo/Alexa
Data processing (real-time streaming analytics)
Automated Backups of everyday tasks
Scalable back ends (mobile apps, loT devices)
Helps you to execute server-side backend logic
Allows you to filter and Transform data
Best practices of Lambda function
Here are some best practices of AWS Lambda functions:

Use the right “timeout.”
Utilize the functions of local storage which is 500MB in size in the /temp folder
Minimizing the use of start-up code which is not directly related to processing the current event.
You should use built-in CloudWatch monitoring of your Lambda functions to view and optimize request latencies.
When not to use AWS Lambda
Following are the situation where Lambda is surely not an ideal option:

It is not appropriate to use AWS Lambda software packages or applications which rely on calling underlying Windows RPCs
If is used for custom software applications with licensing agreements like MS-Office document processing, Oracle databases, etc.
AWS Lambda should not be used for custom hardware process such as GPU acceleration, hardware affinity.
Advantages of using AWS Lambda
Here, are pros/benefits of using AWS lambda:

AWS Lambda is a highly flexible tool to use
It helps you to grant access to resources, including VPCs
Author directly with WYSIWYG editor in console.
You can use it as a plugin for Eclipse and Visual Studio.
As it is serverless architecture, you don’t need to worry about managing or provisioning servers.
You do not need to set up any Virtual Machine.
Helps developers to run and execute the code’s response to events without building any infrastructure.
You just need to for the compute time taken, only when your code runs.
You can monitor your code performance in real time through CloudWatch.
It allows you to run your code without provisioning or to manage any other server
Helps you to execute the code only when needed
You can scale it automatically to handle a few requests per day and even support more than thousands of requests per second.
AWS Lambda can be configured with the help of external event timers to perform scheduled tasks.
Lambda function in AWS should be configured with external event and timers so; it can be used for scheduling.
Lambda functions are stateless so that it can be scaled quickly.
AWS Lambda is fast so it will execute your code within milliseconds.
Limitations of AWS Lambda
Here are the cons/disadvantages of using AWS Lambda:

AWS Lambda tool is not suitable for small projects.
AWS Lambda entirely relies on AWS for the infrastructure, so you can’t install any additional software if your code demands it.
Concurrent execution is limited to 100
AWS Lambda completely depended on AWS for the infrastructure; you cannot install anything additional software if your code demands it.
Its memory volume can vary between 128 to 1536 MB.
Event request should not exceed 128 KB.
Lambda functions help you to write their logs only in CloudWatch. This is the only tool that allows you to monitor or troubleshoot your functions.
Its code execution timeout is just 5 minutes.
Summary
Serverless is a term that generally refers to serverless applications.
AWS Lambda is one such serverless compute service. Therefore, you don’t need to worry about which AWS resources to launch, or how will they manage them.
A function is a program or a script which runs in AWS serverless Lambda.
Runtime allows functions in various languages which runs on the same base execution environment.
An event source is an AWS service, such as Amazon SNS, or a custom service.
Lambda layers are an important distribution mechanism for libraries, custom runtimes, and other important function dependencies.
Log stream allows you to annotate your function code with custom logging statements which helps you to analyse the execution flow and performance of your Lambda functions.
AWS Lambda is a Platform as a Service (PaaS). It helps you to run and execute your backend code.
AWS EC2 Is an Infrastructure as a Service (laaS). It provides virtualized computing resources.
Deploy and manage the apps on AWS Cloud without worrying about the infrastructure which runs those applications.
AWS Lambda is used for running and executing your Back-end code. You can’t use it to deploy an application.
AWS Lambda helps you for the ETL process.
The best practice of Lambda function in AWS is to use the right “timeout.”
It is not appropriate to use AWS Lambda software packages or applications which rely on calling underlying Windows RPCs
AWS Lambda is a highly flexible tool.
AWS Lambda tool is not suitable for small projects.
A common event which will be triggered when you use AWS Lambda is Insert, updating and deleting data Dynamo DB table.
You Might Like:
How to Change Instance Type & Security Group of EC2 in AWS
How to Create EC2 Instance in AWS: Step by Step Tutorial
AWS (Amazon Web Services) Tutorial: Basics for Beginners
Azure vs. AWS: What is the Difference Between AWS and Azure
AWS Lamda Documentation Official

Top comments (0)