AWS Lambda
Overview
AWS Lambda is an all-inclusive serverless offering by Amazon. It provides the platform to run serverless applications without having to worry about provisioning resources, deployment, or scaling. Let's explore this cost effective service in AWS.
AWS Lambda
AWS Lambda is a feature-rich, serverless computing service offered by Amazon. AWS Lambda is an excellent choice to run serverless applications, from a lightweight API service to data processing.
AWS Lambda supports a wide range of languages (Python, Node.js, Go, Java etc.) and architectures (x86_64, arm64), making it a versatile option to build applications. The direct integration of other AWS services like Amazon S3, AWS EventBridge etc. further enhances AWS Lambda's capabilities.
In the following sections, we will explore what serverless computing is and how AWS Lambda works. Later we will walk through an AWS Lambda tutorial to demonstrate how to use an AWS Lambda function.
How does AWS Lambda work?
Before we understand how AWS Lambda works, we need to understand serverless computing. In cloud computing, the hardware is managed by "cloud providers" like AWS. These providers manage the hardware and charge developers to use the hardware. Typically in the cloud ecosystem, developers would write code and then create a virtual instance in the cloud to run their code. Developers will also need to configure some kind of logging and monitoring service.
Serverless computing takes this concept to the next level. In serverless computing, the cloud provider takes responsibility for the end-to-end infrastructure - provisioning resources, running the code, auto-scaling, logging, monitoring etc. This allows developers to focus only on writing code and developing applications, boosting their efficiency.
AWS Lambda builds on the principles of serverless computing, providing an excellent platform to build your applications and integrate easily with other AWS services. AWS handles provisioning resources, deploying your code with the required dependencies, running your code, and finally terminating resources after code execution is completed.
AWS Lambda uses the term function to describe the resources that run your code in Lambda. An AWS Lambda function can be triggered due to various reasons, which we will discuss shortly in another section. A trigger is an event that invokes a Lambda function.
Let's take a look at the AWS Lambda Execution Lifecycle to fully understand how a Lambda function is exeucted:
When an AWS Lambda function is triggered, there are three stages in its execution lifecycle as shown in the above image.
- Init: During this stage, Lambda creates the execution environment with the configured resources, downloads the source code, initializes any external extensions, and runs the function's initialization code.
- Invoke: After the Init stage, the Lambda function is ready for execution. Lambda invokes the function handler on each trigger. After the function handler completes execution, Lambda executes the next innovation if available, or goes to the Shutdown stage.
- Shutdown: If there are no more triggers available, then Lambda shutdowns the execution environment.
AWS Lambda Concepts
Let's take a look at some key AWS Lambda concepts:
- Function: The code or logic used to run in Lambda. The same code is used for every trigger or innovation.
- Trigger: Any event that causes an AWS Lambda to be invoked. There are various events that can be considered as triggers, and are listed in the next section.
- Event: Is a JSON structure containing the data for a Lambda function to process. This event object is passed to the function handler at runtime.
- Execution Environment: Is an isolated runtime for the Lambda function to be executed. The AWS Lambda Execution Lifecycle illustrated is executed in an execution environment.
- Runtime: The runtime is the language-specific configuration that runs in an execution environment. For example, for Node.js the available runtimes are Node.js 16, Node.js 14, and Node.js 12, as of the time of writing this article. Another related property is the instruction set architecture which can be x86_64 or arm64.
- Layer: A .zip file achive containing additional code which can be used in your main Lambda function. This allows you to conveniently package dependent libraries separately.
- Concurrency: Concurrency is the number of instances of your function that are running at any given time. When your AWS Lambda function is invoked, Lambda provisions an instance of it to handle the event. When the function code completes execution, it can process another invocation. If the function is triggered again while a request is still being processed, another instance is provisioned. This increases the function's concurrency.
Events that Trigger AWS Lambda
AWS Lambda works with over twenty-five other AWS services, and this list continues to grow! Lambda can either be triggered by another service or take actions on services.
Here are a few examples of how AWS Lambda functions can be triggered:
- Object creation or deletion in S3
- Insertion, deletion, or update to DynamoDB
- Notification sent to SNS
- Cron job configured in EventBridge
- Process streaming data sent on Kinesis
- Authorization using Cognito
- Trigging based on state change for EC2
- Process messages sent to a queue in SQS
Triggers can even be configured cross-region or even cross-account. But note that a Lambda function will execute only in the region it has been setup in.
Use Cases
Let's illustrate some of the use cases of AWS Lambda functions:
- Backend API Service: AWS Lambda can be used as a backend API Service for simple requests that do not require complex computing. This can be configured using AWS API Gateway.
- File Processing: AWS Lambda functions can also be used in file processing. For example, you can trigger a Lambda function if a new file gets created in a particular Amazon S3 bucket and automatically make changes to the file or trigger another operation based on the contents of the file.
- Task Automation: A very common use case of AWS Lambda is task automation. AWS EventBridge can be used to trigger Lambda functions based on a schedule for rebooting instances, sending notifications via AWS SNS based on certain conditions, or patching.
- Serverless Orchestration: AWS Step Functions use a chain of AWS Lambda functions to accomplish tasks. These powerful integrations make serverless orchestration painless.
Benefits and Limitations of Using AWS Lambda
Now that we have understood how AWS Lambda functions work and their use cases, let's take a look at the benefits and limitations of using AWS Lambda:
Benefits of AWS Lambda
- Offers a fully managed infrastructure - from provisioning resources to code deployment and function execution.
- Has automatic scaling built in. AWS Lambda functions can scale to handle thousands of requests within a few seconds.
- Integrates with a wide range of other AWS Services, making it an easy choice to build serverless applications.
- AWS Lambda has a very flexible pricing model, which ensures you pay only per invocation. AWS does not charge separately for the test environment.
Limitations of AWS Lambda
- One of the biggest drawbacks of AWS Lambda is the cold starts - which is the time the AWS Lambda takes to provision an instance and deploy dependencies for a function to run. Lambda functions can have quite high cold start times based on the number of dependencies used.
- AWS Lambda is restricted to only a set of languages and runtimes. This does not allow the same flexibility offered by Amazon EC2.
- AWS Lambda functions are also limited in system resources like memory size (max of 10240 MB), concurrency (max of 1000 concurrent innovations by default), and execution time (max 15 minutes).
How To Use AWS Lambda?
Let's create an example AWS Lambda function that calculates the sum of two numbers and outputs the results. The two numbers are provided by part of the event variable, which is passed to the function handler at runtime.
Pre-Requisites
- AWS Account
Steps
-
Login to your AWS Account.
-
Open the AWS Console. Search for "Lambda" in the Search Bar. Select Lambda.
-
Click on the Create function button.
-
In the Create function page:
- Keep "Author from scratch" selected.
- Give the "Function name" as "Sum".
- Select the "Runtime" as "Node.js 16.x".
- Select the "Architecture" as "x86_64".
- Don't change any other settings.
- Click the Create function button.
-
Wait for few seconds till the AWS Lambda function is created. After the function is created you will be automatically re-directed to the Sum AWS Lambda Function page.
-
Replace the code in the "Code source" section with the below code:
- Click the Deploy button. After a few seconds, the Sum AWS Lambda function will be ready to use.
- Create a Test Event by first clicking the arron next to the Test button and selecting "Configure test event". Then, in the "Configure test event" page, add an "Event name" and replace the "Event JSON" with the below value:
9. After the Test Event is saved, click the Test button. You should see the following output:
You can see the sum in the first red box. Then in the second red box, you will information about the execution time as well as memory used.
AWS Lambda VS Amazon EC2 VS AWS Elastic Beanstalk
Characteristic | AWS Lambda | Amazon EC2 | AWS Elastic Bean Stalk |
---|---|---|---|
Description | Serverless computing platform to run specific functions/code | Platform to create virtual machines on the cloud to perform a wide variety of tasks | Platform to create and host web applications |
Use Case | Simple operations that can be performed within a few minutes, duration controlled by AWS | Simple to complex operations that can last indefinitely, duration controlled by user | Web applications that can be deployed/scaled automatically, duration controlled by user |
Environment | Allows for only specific set of languages and runtimes | Multiple operating systems, runtimes, instance types, and languages are supported | Multiple operating systems and instance types supported but limited of languages |
Management | Completely controlled by AWS, only the application code needs to be provided | Mostly controlled by the user, AWS only provisions the resources, deployment is manual | Mostly controlled by the user, AWS provisions resources and handles scaling, deployment is automated |
Best Practices of AWS Lambda Function
Let's understand a few best practices while using AWS Lambda functions:
- Take advantage of execution environment reuse and improve the performance of your function. Simple things like initializing database connections outside the function handler will enable your functions to reuse these connections and reduce execution time.
- Use a keep-alive configuration to maintain persistent connections. This will allow you to avoid cold starts and new connections to databases, which increase your overall execution time.
- Use environment variables to pass operational configs to your function.
- Avoid using recursive code which will re-trigger AWS Lambda functions. This could lead to a high number of invocations and increase costs dramatically.
- Write idempotent* code to ensure duplicate tasks are handled the same way.
- Load test your Lambda function to determine an optimum timeout value and memory configuration. You can use tools like locust to generate high traffic.
- Use most-restrictive permissions when setting IAM policies. Apply the principle of least privilege for the AWS Lambda function.
*idempotent: An idempotent task is where operations can be run multiple times without changing the result. For example, updating the same key in a database with the same value can be considered an idempotent task. But programmatically creating a database is not an idempotent task as repeated runs of the program might create multiple copies of the database.
For the full list as well as detailed examples, you can refer to the official AWS documentation linked here.
AWS Lambda Pricing
AWS Lambda has an unique pricing model compared to other AWS Lambda. Since AWS Lambda functions are short-lived, the pricing for AWS Lambda is based off:
- Number of function invocations.
- Duration of each function execution.
- Memory used for each function execution.
AWS uses a metric called GB-second, which is the duration of the function execution multiplied by the memory configured for a AWS Lambda function.
Each AWS Account gets one million free requests and 400,000 GB-seconds of compute time per month. Ever after this limit is crossed, AWS Lambda is quite cost-effective and hence an excellent choice to build serverless applications.
Conclusion
- In this article, we first looked at what AWS Lambda is and how AWS Lambda works.
- We understood the key concepts related to AWS Lambda - function, trigger, event, execution environment, runtime, and layer.
- We listed down some of the events that can trigger an AWS Lambda function - object creation in S3, message in SQS, and data entry in DynamoDB.
- Then we illustrated a few of the use cases of AWS Lambda - backend API service, file processing, task automation, and serverless orchestration.
- We understood how to use AWS Lambda by creating an example function.
- We took a look at some of the benefits of using AWS Lambda, as well its limitations.
- We compared a few of different the compute offerings by AWS - like AWS Lambda, Amazon EC2, and AWS Elastic Bean Stalk.
- We outlined some of the best practices to use while developing AWS Lambda functions - execution environment reuse, using environment variables, writing idempotent code, and avoiding recursive code.
- Finally, we learnt how the AWS Lambda pricing model works.