AWS Lambda (or Lambda for short) is a serverless computing service provided by AWS. In this chapter we are going to be using Lambda to build our serverless application. And while we don’t need to deal with the internals of how Lambda works, it’s important to have a general idea of how your functions will be executed.
AWS Lambda is a responsive cloud service that inspects actions within the application and responds by deploying the user-defined codes, known as functions. It automatically manages the compute resources across multiple availability zones and scales them when new actions are triggered.
AWS Lambda supports the code written in Java, Python and Node.js, and the service can launch processes in languages supported by Amazon Linux (includes Bash, Go & Ruby).
AWS Lambda is a Function-as-a-service (FaaS) computing platform provided by AWS. FaaS provides a computing platform to execute code in the cloud. As in any serverless system, it abstracts away the complexities of provisioning and managing a cloud infrastructure. It is commonly used when building microservices applications, but also serves monolithic and other types of architectures as well
Features of AWS Lambda Functions:
Extend other AWS services with custom logic:
Build custom back-end services:
You can use AWS Lambda to create new back-end services for your applications that are triggered on-demand using the Lambda API or custom API endpoints built using Amazon API Gateway. By using Lambda to process custom events instead of servicing these on the client, you can avoid client platform variations, reduce battery drain, and enable easier updates.
Bring your own code:
With AWS Lambda, there are no new languages, tools, or frameworks to learn. You can use any third party library, even native ones. You can also package any code (frameworks, SDKs, libraries, and more) as a Lambda Layer and manage and share them easily across multiple functions. Lambda natively supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions.
Completely automated administration:
AWS Lambda manages all the infrastructure to run your code on highly available, fault-tolerant infrastructure, freeing you to focus on building differentiated back-end services. With Lambda, you never have to update the underlying OS when a patch is released, or worry about resizing or adding new servers as your usage grows. AWS Lambda seamlessly deploys your code, does all the administration, maintenance, and security patches, and provides built-in logging and monitoring through Amazon CloudWatch.
Built-in fault tolerance:
Lambda has built-in fault tolerance. AWS Lambda maintains compute capacity across multiple Availability Zones in each region to help protect your code against individual machine or data center facility failures. Both AWS Lambda and the functions running on the service provide predictable and reliable operational performance. AWS Lambda is designed to provide high availability for both the service itself and for the functions it operates. There are no maintenance windows or scheduled downtimes.
AWS Lambda invokes your code only when needed and automatically scales to support the rate of incoming requests without requiring you to configure anything. There is no limit to the number of requests your code can handle. AWS Lambda typically starts running your code within milliseconds of an event, and since Lambda scales automatically, the performance remains consistently high as the frequency of events increases. Since your code is stateless, Lambda can start as many instances of it as needed without lengthy deployment and configuration delays.
Connect to relational databases
Use Amazon RDS Proxy to take advantage of fully managed connection pools for relational databases. RDS Proxy efficiently manages thousands of concurrent database connections to relational databases, making it easy to build highly scalable, secure, Lambda-based serverless applications that need to interact with relational databases. Currently, RDS Proxy offers support for MySQL and Aurora. You can use RDS Proxy for your serverless applications through the Amazon RDS console or through the AWS Lambda console.
Fine grained control over performance
Provisioned Concurrency gives you greater control over the performance of your serverless application.
Connect to shared file systems
With Amazon Elastic File System for AWS Lambda, you can securely read, write, and persist large volumes of data at low latency, at any scale.
Run code in response to Amazon CloudFront requests
With Lambda@Edge, AWS Lambda can run your code across AWS locations globally in response to Amazon CloudFront events, such as requests for content to or from origin servers and viewers. This makes it easier to deliver richer, more personalized content to your end users with lower latency.
Orchestrate multiple functions
You can coordinate multiple AWS Lambda functions for complex or long-running tasks by building workflows with AWS Step Functions. Step Functions lets you define workflows that trigger a collection of Lambda functions using sequential, parallel, branching, and error-handling steps. With Step Functions and Lambda, you can build stateful, long-running processes for applications and backends.
Integrated security model
AWS Lambda allows your code to securely access other AWS services through its built-in AWS SDK and integration with AWS Identity and Access Management (IAM). AWS Lambda runs your code within a VPC by default. You can optionally also configure AWS Lambda to access resources behind your own VPC, allowing you to leverage custom security groups and network access control lists to provide your Lambda functions access to your resources within a VPC. AWS Lambda is SOC, HIPAA, PCI, ISO compliant.
Only pay for what you use
With AWS Lambda, you pay for execution duration rather than by server unit. When using Lambda functions, you only pay for requests served and the compute time required to run your code. Billing is metered in increments of 100 milliseconds, making it cost-effective and easy to scale automatically from a few requests per day to thousands per second.
Flexible resource model
You choose the amount of memory you want to allocate to your functions and AWS Lambda allocates proportional CPU power, network bandwidth, and disk I/O.
Anatomy of a Lambda function
All Lambda functions consist of three key elements:
1) Handler function
The main function that will be executed each time an event occurs.
Takes two required arguments: an event and context objects (also takes an optional callback object)
2) Event Object
The first argument to the handler function
Contains information about the invocation event
An invocation event could be an API request event holding the HTTP request object, for example
3) Context object
Contains information about the Lambda runtime, such as the function name, version, memory limit, etc.
Provides a method that returns the remaining miliseconds left before the function times out.
Main Benefits and Advantages of Lambda:
- Simplified Cloud Infrastructure
- Improved Productivity
- Scalability and Concurrency
- Customer Satisfaction and Trust :As a platform fully managed by AWS DevOps teams, the chances of downtime or performance degradation are certainly much lower than an in-house cluster of servers. An application built on top of a stable and robust infrastructure like that can build higher customer satisfaction and earn trust from its users.
- Financial Benefits: Lambda improves the financials of running compute workloads in the cloud in many ways:
- Zero waste with resource over-provisioning leads to lower cloud bills
- Costs are 100% variable, hence the financial risks are very low
- Simplified infrastructure reduces the need for large budget allocated to DevOps
AWS Lambda Limits:
Following are the three types of Lambda limits.
The throttle limit is 100 concurrent Lambda function executions per account and is applied to the total concurrent executions across all functions within a same region.
The formula to calculate the number of concurrent executions for a function = (average duration of the function execution) X (number of requests or events processed by AWS Lambda).
When throttle limit is reached, then it returns a throttling error having an error code 429. After 15-30 minute you can start work again. The throttle limit can be increased by contacting AWS support center.
2. Resources Limit
The following table shows the list of resources limits for a Lambda function.
|Ephemeral disk capacity (“/tmp” space)||512 MB|
|Number of file descriptors||1,024|
|Number of processes and threads (combined total)||1,024|
|Maximum execution duration per request||300 seconds|
|Invoke request body payload size||6 MB|
|Invoke response body payload size||6 MB|
3. Service Limit
The following table shows the list of services limits for deploying a Lambda function.
|Lambda function deployment package size (.zip/.jar file)||50 MB|
|Size of code/dependencies that you can zip into a deployment package (uncompressed zip/jar size)||250 MB|
|Total size of all the deployment packages that can be uploaded per region||1.5 GB|
|Number of unique event sources of the Scheduled Event source type per account||50|
|Number of unique Lambda functions you can connect to each Scheduled Event||5|