AWS Lambda is a serverless cloud computing service that offers Functions as a Service (FaaS). It has been called disruptive because it enables the deployment of any code, in any language, through one platform. As long as the code is wrapped inside a function and run through Lambda, you won’t need to handle any networking, database, or security tasks. You can simply work on your code and use the client-side logic, which is intuitive and easy to use.
In this article, we’ll review the basic principles of AWS Lambda, including the core concepts, components, how AWS Lambda works, and the best practices as recommended by AWS.
What Is AWS Lambda?
AWS Lambda is an on-demand serverless computing service, which provides developers with a fully-managed cloud-based and event-driven system for running code. To enable the use of any coding language, AWS Lambda uses lambda functions, which are anonymous functions that aren’t bound to an identifier. That means you can package any code into a function and run it.
In a serverless architecture model, the cloud provider manages the allocation and provisioning of servers, including the database, security, and backend logic. The customer enjoys the use of the front-end logic without the heavy lifting associated with computer networking work. The pricing is based on the number of executions, and functions are offered as a service (FaaS).
AWS Lambda use Cases Include but aren’t limited to
- Configuring triggers to execute codes for real-time data, file and stream processing.
- Building serverless backends for web, mobile, Internet of Things (IoT), and third party API.
- Automatically increasing volumes in AWS EBS.
How AWS Lambda Works
If you haven’t used Lambda yet, here are a few key principles to get you familiarized with the system.
AWS Lamda’s Components
- Lambda Functions—anonymous functions that contain pieces of code that trigger events.
- Packaging Functions—the process of compressing the function, with all its dependencies, and sending it to AWS by uploading the function to an S3 bucket.
AWS Lambda’s Execution Model
- Container—a piece of execution code that uses AWS-managed resources to execute the function.
- Instances—containers are immutable and can’t be used again after shut off. To enable on-demand usage, AWS Lambda creates instances—replicas—of the container. Lambda adjusts the number of instances according to the usage requirements.
- Stateless functions—the function in Lambda is controlled and optimized by AWS and usually invoked once per container instantiation.
- Events—requests which are served by a single instance of a Lambda function, and are managed by AWS Lambda.
AWS Lambda’s Specs
Lambda’s supported runtimes are Node.js: v10.15 and v8.10, Java 8, Python: 3.7, 3.6, and 2.7, .NET Core: 1.0.1 and 2.1, Go 1.x, Ruby 2.5 and Rust
- In Lambda, functions run inside containers, each with a 64-bit Amazon Linux AMI.
- The central processing unit (CPU) increases or decreases with the memory capacity. You can control the CPU only through the memory.
- A Lambda function can run for up to 900 seconds or 15 minutes, which means Lambda isn’t ideal for long-running processes.
- The /tmp directory serves as the ephemeral disk space. Subsequent invocations don’t have access to the /tmp directory.
- Uncompressed function packages are limited to 250MB and compressed function packages are limited to 50MB.
Best Practices for Working with AWS Lambda Functions
AWS Recommends the Following Best Practices for Each Lambda Function Code
- For creating a unit-testable function, separate the Lambda handler (entry point) from your core logic.
- Improve the performance of your function with the Execution Context reuse feature.
- Pass operational parameters to your function with AWS Lda Environment Variables.
- If you want full control of your function’s dependencies, package them with your deployment package. Otherwise, your functions will be subjected to AWS updates.
- To increase deployment efficiency, include only runtime necessities in your deployment package, put your Java dependency .jar files in a separate /lib directory, and use simple load frameworks on the Execution Context startup.
- To prevent bugs in volume scales and costs, avoid using recursive code in your Lambda function.
AWS Recommends the Following Best Practices for Function Configuration
- Test your Lambda function before choosing memory size configuration, because any memory increase triggers a CPU increase, and therefore a price increase. You can see your memory usage in the AWS CloudWatch Logs.
- To ensure optimum concurrency, put your Lambda function through a load test. Set your timeout value accordingly, to take into account possible problems with dependency services.
- To prevent unauthorized access to your functions, set up your IAM policies to most-restrictive permissions.
- When you configure runtime resource limits, take into account the payload size, file descriptors, and /tmp space.
- To ensure you get the most cost-effective pricing, delete inactive Lambda functions.
- When using Amazon Simple Queue Service, including the CreateFunction and UpdateFunctionConfiguration—set the value of the function’s expected execution time to a number that never exceeds the value of the Visibility Timeout on the queue.
It’s a Wrap!
AWS Lambda is a useful service for independent software vendors (ISV) and software developers. You can delegate your infrastructure jobs AWS Lambda, which handles all of the database, security, and backend logic work. AWS Lambda is serverless, cloud-native, and offers containerized FaaS and a cost-effective pricing model.
However, due to the runtime limitations of Lambda functions, the AWS Lambda service isn’t as effective for long-running processes. Be sure to compare the service’s specs with the specs of your project, in advance. It’s always best to run a test before introducing a new service or a product into your workflow. You can also take advantage of the free tier module to check out the service and make sure it suits your needs.