What is AWS Lambda? Lambda Function with Examples

Before AWS Lambda function, let's understand:

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?

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:

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.

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) Goto https://aws.amazon.com/lambda/ and Get Started

Step 2) Create an account or sign in with your existing account

Step 3) In the next Lambda page,

  1. Edit the code
  2. Click Run

Step 4) You will see 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:

Best practices of Lambda function

Here are some best practices of AWS Lambda functions:

When not to use AWS Lambda

Following are the situation where Lambda is surely not an ideal option:

Advantages of using AWS Lambda

Here, are pros/benefits of using AWS lambda:

Limitations of AWS Lambda

Here are the cons/disadvantages of using AWS Lambda:

Summary

 

YOU MIGHT LIKE: