An Introduction to AWS Lambda
What is AWS Lambda?
AWS Lambda is serverless compute service offered by Amazon Web Services. It lets you run the code without provisioning the servers in advance. The code which users run on AWS Lambda is called functions. Each function runs in a self-contained environment and uses a runtime defined by the user. The concept of serverless means not needing to provision the servers while running your code. Users can upload their code and select the runtime they want to use to run the code, AWS Lambda will take care of all the other stuff. Users will pay for what they use.
Why do I need AWS Lambda?
AWS Lambda is an essential part of the serverless architecture. If you want to use serverless architectures and don't want the headache of maintaining servers, AWS Lambda empowers you to run your code without worrying about much. There are many runtimes supported by it and many more will come in future. There are some use cases for which AWS Lambda is a good option but with some use cases, you might want to choose something else.
What are the use cases for AWS Lambda?
AWS Lambda works best when there are events to process. Following are some good use cases for AWS Lambda:
- Data Processing: AWS Lambda can respond to events like change in data, change in state or something a user does. It can directly integrate with AWS Services like SNS, SQS, change streams from DynamoDB etc. All these events can be selected as a source to fire off the Lambda function to take some actions related to it.
- Real-Time Processing: Be it real-time file processing or stream processing, AWS Lambda can not only handle the processing in these cases but can also scale as per the requirement. The S3 upload event or data published to the Kineses stream can be used as an event to process the data further.
- Web Applications: AWS Lambda can also be triggered using the request coming to an AWS API Gateway. This means AWS Lambda can be used to create highly scalable APIs which can scale easily with the traffic demand for the app.
- Task Automation: Due to its event-based triggers functionality, AWS Lambda is a good fit for various business task automation for which you don't need servers running all the time. An example will be, sending an onboarding email whenever a user joins your app.
What are some benefits of AWS Lambda?
We have touched on this in the above questions in this article. Let's talk about them in a little bit of detail:
- Pay Per Use: You only pay what you use in AWS Lambda. At the writing of this article, AWS Lamdba is charged based on the 100ms cycle. That means you will be charged for min 100 ms and then for every next 100 ms irrespective of your actual use. We have an event coming at the end of this month hopefully AWS will come up with some good announcements related to the pricing.
- Managed Infrastructure: The AWS Lambda infrastructure is managed by AWS and users don't have to maintain anything. This means as a user, you don't have to think about maintaining the underlying servers, patching of those servers, scaling of those servers etc. This can be a huge cost and time saving for some of the users.
- Integration With Other AWS Services: As I mentioned earlier, AWS Lambda can be triggered by many AWS services. It is a good thing as you can now respond to the events happening in other parts of your code using those services.
What are some limitations of AWS Lambda?
No service only has benefits. There are always some limitations and AWS Lambda also has some. Following are some of the limitations of AWS Lambda:
- Cold Starts: It is the amount of latency between when the event is generated and when the function actually executes it. This happens because a function first needs to be downloaded into one of the servers managed by AWS and runtime is allocated to run the function. This includes downloading the code and other init procedures defined in the code to make sure the handler function is ready to take the event. Though it depends on many factors increase in latency makes AWS lambda not suitable for the latency-critical task and applications.
- Execution Time: The Lambda function will timeout after running for 15 minutes. There is no way to increase it. You can define a timeout in less than 15 mins but not more than that. If your task requires more than 15 mins to complete, you should look for any other options.
- Code Package Size: There is a size limit on the Lambda code package. It is 50 MB (zipped, for direct upload), 250 MB (unzipped, including layers) and 3 MB for (console editor).
- Concurrency: A single AWS account can run 1000 Lambda function concurrently. This is a default limit. You can ask AWS to raise the limit, but if you hit the limit before that, your executed functions above the limit will be throttled and forced to wait until other functions complete their job.
What is Next?
If you have now decided to give AWS Lambda a spin, here is a link from AWS to start working with AWS Lambda functions. Getting Started with AWS Lambda.
I will be posting more about the Lambda functions in this blog in near future.