AWS lambda
Darshika Srivastava
Associate Project Manager @ HuQuo | MBA,Amity Business School
What is AWS Lambda
PDF
RSS
AWS Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your 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, and logging. With Lambda, you can run code for virtually any type of application or backend service. All you need to do is supply your code in one of the?languages that Lambda supports.
You organize your code into?Lambda functions. Lambda runs your function only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time that you consume—there is no charge when your code is not running. For more information, see?AWS Lambda Pricing.
Lambda is a highly available service. For more information, see the?AWS Lambda Service Level Agreement.
Note
In the AWS Lambda Developer Guide, we assume that you have experience with coding, compiling, and deploying programs using one of the?languages that Lambda supports.
When to use Lambda
Lambda is an ideal compute service for many application scenarios, as long as you can run your application code using the Lambda?standard runtime environment?and within the resources that Lambda provides. For example, you can use Lambda for:
File processing:?Use Amazon Simple Storage Service (Amazon S3) to trigger Lambda data processing in real time after an upload.
Stream processing:?Use Lambda and Amazon Kinesis to process real-time streaming data for application activity tracking, transaction order processing, clickstream analysis, data cleansing, log filtering, indexing, social media analysis, Internet of Things (IoT) device data telemetry, and metering.
Web applications:?Combine Lambda with other AWS services to build powerful web applications that automatically scale up and down and run in a highly available configuration across multiple data centers.
IoT backends:?Build serverless backends using Lambda to handle web, mobile, IoT, and third-party API requests.
Mobile backends:?Build backends using Lambda and Amazon API Gateway to authenticate and process API requests. Use AWS Amplify to easily integrate your backend with your iOS, Android, Web, and React Native frontends.
When using Lambda, you are responsible only for your code. Lambda manages the compute fleet that offers a balance of memory, CPU, network, and other resources to run your code. Because Lambda manages these resources, you cannot log in to compute instances or customize the operating system on?provided runtimes. Lambda performs operational and administrative activities on your behalf, including managing capacity, monitoring, and logging your Lambda functions.
If you need to manage your own compute resources, AWS has other compute services to meet your needs. For example:
Amazon Elastic Compute Cloud (Amazon EC2) offers a wide range of EC2 instance types to choose from. It lets you customize operating systems, network and security settings, and the entire software stack. You are responsible for provisioning capacity, monitoring fleet health and performance, and using Availability Zones for fault tolerance.
AWS Elastic Beanstalk enables you to deploy and scale applications onto Amazon EC2. You retain ownership and full control over the underlying EC2 instances.
Lambda features
The following key features help you develop Lambda applications that are scalable, secure, and easily extensible:
Concurrency and scaling controls
Concurrency and scaling controls?such as concurrency limits and provisioned concurrency give you fine-grained control over the scaling and responsiveness of your production applications.
Functions defined as container images
Use your preferred?container image?tooling, workflows, and dependencies to build, test, and deploy your Lambda functions.
Code signing
Code signing?for Lambda provides trust and integrity controls that let you verify that only unaltered code that approved developers have published is deployed in your Lambda functions.
Lambda extensions
You can use?Lambda extensions?to augment your Lambda functions. For example, use extensions to more easily integrate Lambda with your favorite tools for monitoring, observability, security, and governance.
Function blueprints
A function blueprint provides sample code that shows how to use Lambda with other AWS services or third-party applications. Blueprints include sample code and function configuration presets for Node.js and Python runtimes.
Database access
A?database proxy?manages a pool of database connections and relays queries from a function. This enables a function to reach high concurrency levels without exhausting database connections.
File systems access
You can configure a function to mount an?Amazon Elastic File System (Amazon EFS) file system?to a local directory. With Amazon EFS, your function code can access and modify shared resources safely and at high concurrency.
Getting started with Lambda
To work effectively with Lambda, you need coding experience and expertise in the following domains:
Linux OS and commands, as well as concepts such as processes, threads, and file permissions.
Familiarity with security services and concepts: AWS Identity and Access Management (IAM) and access control principles, and AWS Key Management Service (AWS KMS) and public key infrastructure.
Cloud concepts and IP networking concepts (for public and private networks).
Distributed computing concepts such as queues, messaging, notifications, and concurrency.
How does AWS Lambda work?
The following AWS Lambda example with block diagram explains the working of AWS Lambda in a few easy steps
AWS Lambda Block Diagram
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
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
Step 4) Check output
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:
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.: