When I first started learning about the AWS services, one of the most interesting service to me was AWS Lambda. For some reason, AWS Lambda seemed so mythical. This was when I was just getting started.
Slowly, I stated using AWS Lambda and slowly everything started to make more sense. I still have a lot to learn about AWS Lambda, but I am now able to explain AWS Lambda in layman’s term to anyone.
Get our AWS Lambda Beginners course on Udemy: https://www.udemy.com/lambda-course/?couponCode=MAHALESS
What is AWS Lambda?
AWS lambda is a on demand compute service. Instead of having a computer running listening to your request, you have a function or logic saved on AWS. Some kind of event, for example, making a request to API gateway endpoint or uploading something to S3 will trigger the function and the function will execute on one of the servers Amazon provisions within milliseconds. That function for example could resize an image in one of your buckets.
AWS describes Lambda in simple words like this,” Run code without thinking about servers. Pay only for the compute time you consume.”
So there are 3 main steps: An event —> triggers a function —> Something is done.
Breaking the 3 components down:
Event: Like I mentioned earlier, an event can be a lot of things. The event is responsible for triggering the function that you store on AWS Lambda. Some of the supported services that can act as a trigger are mentioned in the images below.
I’ll provide 4 quick examples:
API Gateway trigger: You can create an api endpoint using API gateway. Then you can integrate the API gateway with Lambda. This lets you perform actions like GET and POST. So when you ask for something with GET method, API gateway send the request to Lambda. In Lambda you can have some hardcoded response . Or you could have your database connect to AWS Lambda and send a dynamic response. Similarly, you can let users post to a database where Lambda acts as a middle man. Lambda can parse the response and process it before posting to the database.
S3 trigger: You can integrate Lambda and S3 so that for example, you upload an image to a bucket in S3, Lambda resizes that image and puts it in a different bucket.
Cloudwatch Events: With Cloudwatch Events you can schedule a cron-job like triggering event where a lambda function is executed every X hours/days/minutes or everyday at 6pm or 7pm etc.
You can do something like stop all the EC2 instances at 6pm. Previously, you had to have a server running to do that. Now you can shut down all of your EC2 servers. Here’s a demo of using Lambda and CloudWatch integration to stop EC2:
Alexa Skill: The popular choice to store the code for Alexa skills is on AWS Lambda. AWS Lambda acts as a backend for your voice Skill. Here’s a video where I build a simple Alexa skill using AWS Lambda:
How does Lambda Scale?
Unlike EC2 instances where you have to setup autoscaling groups and a load balancer, AWS Lambda does all the provisioning and scaling for you. If you suddenly get thousands of requests , then Lambda should be able to handle that, you don’t have to worry about scaling up and down. The default limit is something like 1000 concurrent requests. Keep in mind that a typical Lambda function runs for less than a second.
Other things to consider:
- Lambda functions can run on an environment with a maximum memory of 3008 MB. The memory used can range between 128 MB- to 3008 MB. This means if you need a high memory compute environment, you have to consider finding a workaround or using something else.
- Lambda now supports execution durations upto 5 minutes. So if you have a task that takes longer to process than 5 minutes, you should rethink about using Lambda.
- You can call another Lambda function from a Lambda function. So you can break up tasks and have a step by step execution. This can solve the problem mentioned of the execution time being maximum of 5 minutes because you can break a task into smaller tasks.
- Lambda has a /tmp directory that you can use to temporarily store data up-to 512 mb.
- Lambda has this concept of cold starts. Let’s say your function executes 100 times a day. The first execution will take a little bit longer because that will be a clean start of your function execution on an environment. Lambda doesn’t close the environment immediately after the function is completed. It is possible to reuse the same environment for a second execution. So, the second and executions after that might execute faster than the first execution. You can do things like trigger a function periodically to keep the Lambda environment warm.
- You can provide IAM roles to AWS lambda and give AWS Lambda access you your other AWS resources without having to store access keys in the function.
The best part about Lambda? 1 million requests per months are free and you pay extremely little for millions more requests.
The possibilities with AWS Lambda are limitless. And AWS is always adding new features to AWS Lambda. I encourage you to try using AWS Lambda. There are blueprints provided by AWS in the console that span a variety of tasks like image recognition, notification etc.
Here’s a playlist where I do various things with AWS Lambda:
I will be adding more content to this article as I learn more interesting things so you’re welcome to check back from time to time. Let me know if I missed anything interesting in this article in the comments below.