What is AWS Lambda? It is a serverless compute service which runs seamlessly without the need/intervention of manually creating or configuring servers for it.
What is Serverless ?
Serverless is a model where in we are not required to manage servers on our own. Basically, it allows developers to build and run an application without managing servers.
So who manages these servers ? The cloud provider like AWS, Google, Microsoft handles the rest, provisioning the cloud infrastructure required to run the code and scaling the infrastructure up and down on demand as needed.
Serverless does not mean ‘no servers’ : It means that servers are managed by cloud providers not be developers, that is the reasons it’s called serverless.
What is AWS Lambda
What is AWS Lambda is a serverless compute service that runs the code, and it responds to the events and automatically manages the compute resources that are required by the application. For example of events lets suppose on shopping sites like Flipkart, Amazon we place the items in carts or remove them, these are the events and lambda responds to it and runs the code that is required to perform the action.
With AWS Lambda, you can run any type of service or application without any type of administration (Administration means managed by AWS).
What AWS Lambda Manages
All the below is managed by AWS lambda itself:
- Provisioning and capacity of compute resource required by the application like CPU, Memory, network, etc.
- Server and OS maintenance. All backend servers and OS it manages.
- High Availability and automatic scaling
- Monitoring health of all the AWS Lambda function.
- Security Patches and monitoring.
- Code deployment
- Monitoring application logs and performance.
Features of AWS Lambda
Some of the key features of lambda are :
No need to manage servers: AWS Automatically manages all the servers for you. Lambda runs your code on highly available, fault-tolerant infrastructure spread across multiple Availability Zones (AZ’s) in a single Region, seamlessly deploying code, and providing all the administration, maintenance, and patches of the infrastructure.
Lambda also provides built-in logging and monitoring, including integration with Amazon CloudWatch, CloudWatch Logs, and AWS Cloud Trail.
Scaling: AWS Lambda functions scales automatically as it responds to events and runs multiple events parallelly and process each event individually.
Increase Innovation: As most of the work behind the scenes is taken care by lambda itself, thus it allows you to focus on the work and innovating it.
Pay-as-use: It charges only for the milliseconds your code uses and number of times you run.
Deployment and logging: AWS Lambda automatically deploy your code and logs the events for you to use.
When to use AWS Lambda
Lambda is ideal in few scenarios like :
1. File Processing: We can use Amazon Simple Storage Service (Amazon S3) to trigger Lambda data processing in real time after an upload. We can use Amazon Simple Storage Service (Amazon S3) to trigger Lambda data processing in real time after an upload. Here, upload on S3 is an event we are using.
2. Steam Processing: We can use AWS Lambda and Kinesis to track real time stream of data processing like transaction’s data, activity monitoring, log filtering, indexing, social media analysis where we have stream of data and events on the same.
3. Web Applications: We can even use AWS lambda to work with other AWS service to build web applications.
4. Mobile 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.
Some of the other use cases when you can use lambda:
Automated Backups of everyday tasks can be done with the help of lambda, Scalable back ends (mobile apps, loT devices) for these we can use lambda like API executions, etc. Lambda helps you to execute server-side backend logic, allows you to filter and transform data when needed.
How Lambda Works
Let’s take a small example of file upload and the same file is displaying on mobile or tablet screens.
Problem Statement:
How to resize any of the images taken from high-end camera or downloaded (high quality image) from internet for display on mobile screen ?
Resolution
We are going to write one lambda which will take care of resizing of images and publish it.
See below,
Here, we are uploading images, photos to the S3 bucket storage and then on upload event the lambda function gets triggered which contains the code to run the image resizing logic and after processing it will be available to display on the mobile devices.
Use Cases:
Most common use cases of AWS Lambda
- Process data at scale
- Run interactive web and mobile backends
- Enable powerful ML insights
- Create event-driven applications
AWS Lambda Runtimes
Lambda runtimes are the runtime environments supported by AWS lambda. AWS lambda supports multiple languages and runtime for those languages.
The following runtimes are supported by AWS:
Node.js | Versions: 18,16,14,12 |
PYTHON | PYTHON: 3.7, 3.8, 3.9 |
JAVA | JAVA 8, 11 |
.NET | dotnet5.0 and 6 |
Go 1.x | go1.x |
Ruby | 2.7 |
Please note that AWS has a runtime deprecation policy, and it keeps on updating their runtime support to the community.
Limitations of AWS Lambda
- AWS Lambda’s concurrent execution is limited to 1000. Lambda allocates CPU power in proportion to the amount of memory configured. You can increase or decrease the memory and CPU power allocated to your function using the Memory (MB) setting. At 1,769 MB, a function has the equivalent of one vCPU.
- Storage for uploaded functions (.zip file archives) and layers. Each function version and layer version consumes storage. Default of 75 GB
- Its Function memory allocation: 128 MB to 10,240 MB, in 1-MB increments.
- Function Timeout : 15 mins.
- Function environment variables is 4 KB
- Invocation payload (request and response) 6 MB each for request and response (synchronous)
- 256 KB (asynchronous).
- Deployment package (.zip file archive) size 50 MB (zipped, for direct upload) 250 MB (unzipped)
- Execution processes/threads: 1,024
- AWS Lambda functions are not suitable for long-running workloads as it has the limitation of execution time of 15 minutes. It limits concurrent function executions to 1,000.
AWS Lambda Java Code Example
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class AwsLambdaExampleSLB implements RequestHandler<String, String> {
@Override
public String handleRequest(String input, Context context) {
String output = "Hello there " + input + "!";
context.getLogger().log(output);
return output;
}
}
Checkout some of the common interview questions and answers on AWS Lambda