What is AWS Lambda and why it is important?
Suppose you are in the medieval age and you are a warlord of somewhere wanting to raid a village but you are on limited manpower. Now you have two options – recruit a group of soldiers permanently and raid the village. After plundering the village, since you have hired the soldier permanently, they will stay at your barracks. They will eat your food, exhaust your resources, and maybe their leader will start plotting to wed your daughter so he can become the next warlord. Something that Griffith, Leader of the Band of the Hawks did with the king of Midland. Doesn’t look like a nice option I suppose.
Now comes the second option, you hire mercenaries like Guts. They plunder the village with extra vigor, take their fees and leave. They will not eat your food, will not exhaust your resources, and certainly will not try to wed your daughter. No fear of any coup. You can hire them at will and fire them at will. Now, this option looks much flexible. Much safe. Much less resource consuming. Sums up everything and it looks much much better.
Now you may be wondering why am I talking about Berserk and not about AWS? Dude! I am talking about AWS. You see the first option here is EC2 service or any other server and the second option is Lambda. AWS Lambda is a service that boots up, runs your code and you can drive it away. No need for any kind of commitment.
The Game of Strengths, Weaknesses, and usecases
Okay so it is that kind of service so when should you use it you may be wondering? Fear not. I am here to clear things up. so here is the list of usecases for a Lambda function.
- You don’t need a stay awake server. The maximum time Lambda function can right now provide is 15 minutes. Which is enough for most of the normal tasks like performing some calculations, database queries, or calling external APIs. I even ran deep learning model on Lambda.
- Your task isn’t memory consuming. By memory I mean RAM. Right now Lambda provides 10240MB( 10GB) which is again more than enough for all the above-mentioned tasks. Even more than my computer. Keep in mind Lambda’s processing power increases proportionally to the allocated memory.
- Your task is on-demand type or scheduling type. Like sending an invoice on every end of the month. Scheduling an alarm. Sending a kid in 33 years past in every 33 years. Or maybe none of this. Bear in mind that Lambda is, after all, A versatile service.
- Your code size is not greater than 250MB. But we can bypass that in some aspects with some tricks I will probably write about later.
- Lambda runs on demand so suppose you have an application with 200 Lambda function but only 150 are frequently used but rest 50 are still needed since they do important jobs. So you don’t have to pay for the rest 50 Lambda as much as the first 150 since you are paying based on how many times you have invoked the Lambda. Isn’t it cost-efficient?
So now we know the strengths and limits of Lambda functions I need to remind one key benefit apart from what we already discussed.
When we are creating a Lambda functions based application or the more popular name serverless application(because we don’t have to run a server for it), we can choose different runtimes for different functions so isn’t it Awesome? This task is easy in Python then let’s use Python. This task is easy in Node then let’s use Node. Lambda can support a number of runtimes like Python, Nodejs, Java, .NET and they keep adding more. It is like the mercenary army with all sorts of weapons and tools from swords and spikes to battering rams and they keep upgrading all the way to guns and rocket launchers.
So first came the funny intro, then came a Sheldon Cooper style fact machine. Now, what’s next? Bazinga!!!! The most important part-
How to create a AWS Lambda function?
So what do we need to create a Lambda function? of course an AWS account but besides that? Nothing. Absolutely nothing. It is as easy as a walk in a park. Here is how-
Open your AWS Console. In search box type lam. Now you may think something lame would come up but let me the one to say the truth that you are dead wrong. What came has nothing lame about it.
Now select the Lambda option and it will take you to the Lambda service which will have a list of existing Lambda functions and some other options on the left side that may be alien to you but worry not! I will try to clear them in future blogs. Especially the layer part.
Now hit that Create Function button which will open a form to create a Lambda functions.
Type the name but remember it is sticky. Very sticky. You will have to create a new Lambda function and move everything there if you ever find yourself obligated to change its name.
Choose your favorite programming language. Choose a role. This role will decide if your Lambda function can interact with other AWS services or not. But don’t worry. You can change it or update it’s permission later. Now smash that Create Function button so hard its pixels will turn into pudding.
So now, You will be greeted with a screen which has “I am awesome” written all over it with a hidden ink.
Voila! Lambda is ready!
You see that small piece of code. Yup. That’s what you have to edit to create new magic spells. But beware! Sectumsempra not allowed!
Probably I should help you in understanding the default code snippet. You see that exports.handler there? That’s what will be called when you invoke the function. Your Lambda execution starts from there. And you see the event object? That’s the payload that is being sent to your aerospace company’s rocket-launching Lambda.
But for now, We will stick to a simple task. Like adding your first name and last name to create your full name. This is supposed to be hello world for Lambda after all. Update your code as below and hit the save button.
Now to test the function you have to create a test event. You can simply do this by clicking on Select a test event > Configure test events then a window will open which will ask you what you want to pass in your event object. Edit the JSON as below and save.
{
"first_name": "Johan",
"last_name": "Liebert"
}
Now hit the Test button. If you did everything right then you will see your last name appended to your first name like this –
So here it is. Your first-ever Lambda function. It is simple but worry not! I am sure pretty soon, you will be able to edit it and add more Lambda functions to develop awesome stuff.
To invoke this Lambda we had to hit the test button but don’t worry, you can invoke it from API Gateway, from triggers, from schedulers, from AWS SDK and all sorts of things.
In the end
So we gotta summarize what we have learned in a breathless monologue. So repeat after me.
AWS Lambda function is an AWS service that lets you run your code on-demand thus removing the need for enacting and managing a Server. You can create really big applications by combining multiple Lambda functions and such applications are known as Serverless applications. A Lambda function can be triggered in various ways like API Gateway, Triggers, Schedulers, AWS SDK, etc. AWS Lambda supports different runtime like Node, Python, .NET, Java, etc.
Now that’s done. Time for me to say bye for now. Let me know what you think of my post in the comment section or if you have any doubts. Since I am still learning how to write blogs, any feedback will be appreciated. Who knows maybe I can find a topic for my next post in there and write on that.
Later.
useful links:
Liked what you read? Check out my other blog post on AWS Cognito Identity Pool
Amazing write-up. Interesting to read and very well explained.
Thanks. I am glad you liked it.
As a beginner I feel its a very interesting way of explaining and thanks for making it look so easy.
Thanks. I will try to continue this way.
super dear,
Nice blog, interesting with information.
love to see your blog
-sohan malviya
Thanks. I will be sure to keep things informative.
Yeah. I will be sure to use them a lot in future posts.
Hehe. Thanks. I will be sure to use them a lot in future posts.
Great, well explained! Being a beginner also, understood most of it. Thanks for the good work.