A Quick Introduction to AWS Lambda Functions
AWS Lambda functions, they sound so mysterious, when you first hear that name. They certainly did to me and I am sure, many of you might share that feeling. However, let me assure you that by the time you are done reading this post, you would be laughing on the account of the fact that your so called mysterious "lambda functions" are not at all that mysterious. 😃
Let's get started. In this post, we are going to discuss about what are lambda functions, using a situation that I've described below. This situation will help us understand the context in which Lambda is beneficial and later on will also help in understanding where it might not be best choice.
The Situation
Consider a simple situation in which you have a web application that uploads images to a backend and as a result of some processing done at the backend, the application receives a black and white version of the uploaded image. This is exactly what our application is doing currently. You can check it out, if you haven't already. Now one way of setting up this backend where the image is processed and converted to the black and white version of itself is by using AWS EC2 instances, which is just another way of saying that you are creating a server on AWS and you will be deploying your image processing application on this EC2 server. Whenever someone will upload an image to your application the image will be uploaded to the EC2 server and it will be processed and the server will return back the black and white version of the image.
Everything sounds great up and until this point, however, there's one thing that you should think about. What happens when there is no image to process, in other words, what is the server doing when no request is being processed by the server. The answer is absolutely nothing. It's just sitting there, completely idle. Now of course this is not good, since you are still paying for that server as it's still running and it doesn't matter if it's doing any productive processing or not. However, you think to yourself, we have to take this loss as surely there's no other alternative or is there 🤔?
Here comes AWS Lambda as our Dark Knight to the rescue 😎.
How is Lambda different than EC2?
In terms of processing in our particular case, lambda delivers the exact same result as our EC2 instance was doing, which is conversion of our original image to the black and white version of that image. However, there's one significant difference now. Now that we are using lambda, we are using something that's called serverless computing.
Don't panic, it's not as complex as it seems to be. The idea behind it is pretty simple actually. AWS Lambda which is a serverless computing service, lets user perform computing tasks like converting our image to black and white or whatever else they want. However, instead of paying by the hour for all the hours the instance is running, the only pay for the time duration their code is running. Yes, you heard that right, the user only pays for the duration of the code execution and not the duration for which instance is up and in fact, there is no instance for the user to be billed for. Well, that's not entirely true, but I will circle to that a bit later in the post. For now, let's just assume that AWS lambda executes your code without you creating a server and you only pay for the duration of time your code executes on AWS lambda platform.
Alright, so now that we are aware that we can do the same processing for conversion of our image to black and white colour schema, let's jump back to our application and let's see how we can create this application using AWS lambda instead of EC2 instances. First of all, we are going to introduce a few more components to our application architecture now since at this point it's more of a necessity. Initially, if we wanted to could do all of the computing, processing, api routing and storage using the EC2 instance. However, now that we are using AWS Lambda, it's not going to be possible since we don't have same capabilities as we did when we had a complete server to our disposal. However, not to sweat, we can still deploy this application with the same end result by combining a few other services with AWS Lambda which are mentioned as follows:
- For storage we can use AWS S3 storage service
- For API management, we can use API gateway service
- AWS Event Bridge to communicate between the AWS Lambda, S3 and API gateway service using events
Below is the simplified version of the application architecture for your backend that was initially working on a EC2 instance.
And that's it. using these two services in combination with AWS lambda you can easily create a "completely serverless" backend for your application to handle the image conversion task.
So is AWS Lambda really "Serverless"?
Well, to answer that we need to understand what "serverless" actually means. Serverless isn't exactly absence of server on the backend, as I hope most of you might have realized by now.
It's the concept that the server and the underlying infrastructure and OS and all the other dependencies that are being used to support that service is not managed by you. That's a mouthful, I know 🥱.
Let's try to break the above statement down in simpler terms using a comparision between EC2 and Lambda. When you create an EC2 instance, you get a virtualized machine (infrastructure) on AWS servers. What you do with this infrastructure is completely upto you. The OS you install, the patches you apply to update that OS, the runtime versions you install of different programming languages on your instance to support your application, etc, all of this has to be managed completely by you. If the OS breaks down or the you need the updated version of your programming language, you have to take care of it. However, in a serverless service the cloud provider manages the underlying infrastructure and dependencies that are needed by you to run your application and you can just put your application on top of that service and it just works. That's what serverless actually means.
So, again coming back to our original question. Is lambda really "serverless". Well, by the conceptual definition of serverless, yes, it is. However, if you think that there are no servers running in the backend which are running your code that you put up in a lambda function definition, well , that's just not the case. We still have servers in case of lambda, however, they are just completely managed by AWS and not you.
I hope you are now clear about what AWS lambda functions are and how exactly do they fit in realm of serverless computing. If you liked this post, and would want to continue to receive more content like this, please consider subscribing by clicking the button below. I post content twice a week and the content is generally related to Cloud Computing, Data Science, Machine Learning, basically anything that screams AI and Computing 🤓.