AWS Lambda is a serverless compute service that lets you run your code without managing any servers. Developers can use Lambda functions to create various applications that respond to events or run processes on a schedule.
Everything sounds simple, but where and how can you get started using it?
This article will cover the most important information about AWS Lambda, including AWS Lambda common use cases, code examples of Lambda integrations with various AWS services, and potential anti-patterns for using it. Let’s get started!
Table of contents
What is AWS Lambda?
Serverless computing is a significant paradigm shift from traditional virtualization, allowing you to eliminate operational efforts on managing compute resources and focus on the applications development process.
AWS Lambda is an AWS compute service that enables you to run code without provisioning or managing servers. You can use AWS Lambda to run code responding to events, such as changes to data in Amazon S3 buckets, messages arriving in Amazon Kinesis data streams, or executing periodic tasks.
With AWS Lambda, you don’t have any servers to manage because the function code execution runtime is managed by AWS, including patching and managing the underlying hardware, networking, and security. When the code of the Lambda function is not executed, your function code is not running.
To create an AWS Lambda function, you need to deploy its code to the AWS Lambda service using the AWS Lambda console or Lambda API:
- Deploy Lambda function using CloudFormation
- Deploy Lambda function using AWS SAM
- Deploy Lambda function using AWS CDK
- Deploy Lambda function from Python script using Boto3 library
- Deploy Lambda function using Terraform
Every Lambda function execution time is limited to 15 mins, so if you need to build a complex workflow, consider splitting your code into multiple Lambda functions and using Step Functions to orchestrate their execution.
AWS Lambda scales automatically to handle any traffic spikes or increases in workload. AWS automatically provides additional compute capacity (runs Lambda functions) when needed.
AWS Lambda pricing is straightforward. You pay per AWS Lambda invocation request and its execution time. AWS Lambda free tier includes 1,000,000 requests, and 400,000 GB-seconds of compute time (each Lambda function can have from 128 MB to 10,240 MB of RAM).
Benefits of AWS Lambda
AWS Lambda is integrated with all AWS services and supports many programming languages, including Python, Java, C#, Node.js, .NET, Ruby, Go, and others, through Lambda custom runtime.
Monitoring Lambda functions has its difficulties. However, you can simplify this process using AWS CloudWatch, X-Ray, or any third-party serverless monitoring solutions.
When you increase Lambda function memory (RAM), AWS will automatically improve Lambda function CPU and network performance.
AWS Lambda also supports Lambda Container Images. The container image must implement Lambda Runtime API.
AWS Lambda Limits
First, you must know that AWS Lambda is a regional AWS service (AWS Global Infrastructure: Regions and Availability Zones).
Next, there are some critical Lambda execution limits:
- Memory allocation: 128 MB – 10 GB (1 MB increments)
- Maximum execution time: 900 seconds (15 minutes)
- Environment variables: 4 KB limit – all environment variables and their data can’t be more than 4 KB
- Temporary disk storage (
/tmpfolder): 512 MB – 10 GB
- Concurrent executions (provisioned concurrency): by default, up to 1,000 (can be increased)
Lambda deployment limits
- Lambda function deployment size (compressed ZIP archive): 50 MB
- Uncompressed size of Lambda function: 250 MB (for a larger Lambda functions codebase, use
/tmpfolder or EFS volumes)
AWS Lambda is integrated with all AWS services and AWS resources, here are some of the most widely used integrations:
Amazon API Gateway
You can integrate AWS Lambda with API Gateway to build serverless web and mobile backends for your applications.
Amazon Kinesis Data Firehose is using AWS Lambda to do data transformations on the fly.
DynamoDB streams (DB data changes) can be sent to AWS Lambda to give you the ability to react to them.
One of the common patterns to process files uploaded to an S3 bucket is using AWS Lambda functions.
Amazon CloudFront is integrated with Lambda@Edge to help with HTTP requests’ transformations and filtering on the fly.
EventBridge is integrated with AWS Lambda and allows Lambda to react to various events in AWS infrastructure. We can automatically react to this event whenever something happens within AWS.
AWS Lambda is integrated with CloudWatch, allowing you to react to any string pattern in your application logs, send custom business metrics to CloudWatch and build custom Dashboards based on that data.
Lambda can subscribe to an SNS topic and automatically react to SNS notifications.
Lambda functions can process messages coming from SQS queues.
You can customize your web application users’ authentication flow using Lambda integration with Cognito.
For more information on the integration of AWS Lambda with other AWS services, check the AWS Lambda Use Cases – The Ultimate List article.
Examples of using AWS Lambda
You may find many code examples you can use in AWS Lambda to interact with various AWS services in our Python Boto3 Tutorials. Here, we’d like to mention a couple of the most common examples of using Lambda:
Using AWS Lambda for thumbnails and GIFs generation
Example of using AWS Lambda as a AWS CRON job (Scheduled Lambda Functions)
AWS Step Functions – How to manage long-running tasks
AWS Lambda provides broad functionality for you to build applications as needed. While this flexibility is essential, it can also lead to technically functional designs that may be suboptimal from an architecture and cost point of view. Here are examples of how you may not want to use Lambda functions.
Additionally, it is crucial to:
- Test the Lambda function code before deploying it:
- Taking advantage of Lambda’s caching features
- Minifying or compressing Lambda code
- Not deploying unnecessarily large files to Lambda
Some time ago, I published a list of Issues to Avoid When Implementing Serverless Architecture with AWS Lambda in the AWS Architecture blog, which covers additional aspects of improving and optimizing your Lambda functions use cases.
What is AWS Lambda used for?
AWS Lambda is used for various tasks, including running code responding to events, providing back-end services for mobile apps, and handling website processing. It can also be used to create serverless applications, which take advantage of the scalability and pay-as-you-go pricing model.
What are the three components of AWS Lambda?
Logically, AWS Lambda consists of the following three components:
- The function – the actual code that performs the task or executes a part of your application logic.
- The configuration – You can configure Lambda function execution runtime and set up Lambda execution limits, environment variables, etc.
- The event source mapping (optional) – you can specify the AWS service that triggers the function.
How is AWS Lambda different from EC2 servers?
AWS Lambda provides a more fine-granular code execution runtime for your application’s code that can be quickly executed in response to events such as file uploads, website clicks, or messages from AWS services. EC2 servers, on the other side, also allow you to execute your applications’ code, but they consume more compute resources and take more time to scale.
When should I use AWS Lambda?
AWS Lambda can be used in several different ways, depending on the needs of your business. Some common use cases include:
- Processing images and videos
- Handling webhooks from other services
- Serving dynamic content to users
- Automating tasks or workflows
- Executing machine learning models
For more information on the topic, review the above article.
How does AWS Lambda work?
You upload code to the AWS Lambda service and configure it to respond to external events. When an event happens, the AWS Lambda application code is executed in the Lambda execution environment to run your business processes or operations. AWS Lambda service automatically scales the number of invoked functions in response to the number of received events.
Free hands-on AWS workshops
For gaining hands-on experience with AWS Lambda, we strongly suggest you check out Lambda AWS workshops:
- AWS Lambda Workshop
- Content Generation Lambda@Edge
- Building CI/CD pipelines for lambda canary deployments using AWS CDK
- Migrate Spring Boot applications to AWS Lambda
In this article, we’ve summarized all the required information to start with AWS Lambda. Please let us know in the comments section below if you have any comments or suggestions.