EC2 vs Lambda? which one to choose?
If you ask someone who’s designing his application infrastructure “ what is the first question you ask yourself regarding computing type ?” probably, the answer will be “ what to choose EC2 or Lambda? “. well, to answer this question we need to understand and analyze the infrastructure management, performance required and acceptable budget. Both offer the user elastic computational resources, but it may not be immediately apparent which service best suits your given need.in this short blog, I will summarise the key differences between EC2 and Lambda, and when to pick one of them. Let's dig deeper.
About EC2 Computing
Amazon Elastic Compute Cloud (Amazon EC2) offers the broadest and deepest compute platform, with over 500 instances and a choice of the latest processor, storage, networking, operating system, and purchase model to help you best match the needs of your workload. [1]
EC2 is an AWS virtual cloud infrastructure service that provides on-demand resources to create a server in the cloud. There are many types of EC2 to choose from and all of them are shared with isolated hardware resources that are offered in the form of scalable instances for computing memory and processing power.
Amazon has made it easy to choose the operating system type — AMI and AWS service needs to be installed on it and allocate the desired memory, CPU, and storage. For instance, if you need a dedicated server for Node.js, there is an AMI ready for that. If you want to focus on machine learning, there’s an AMI for that too. Depending on your business needs, Amazon prepared preconfigured EC2 instances to meet virtually your requirements. Just like an on-premise server, EC2 enables you to access the instance by HTTP(s) API
About Lambda
Before understanding what is Lambda used for, it’s important to know what problems Lambda can solve.
Say you own a website that has very high traffic during specific events. Part of your website, when customers upload their images, you need to trigger a function to create thumbnails and store them in S3 buckets. Your site has been up for a couple of months so far, and the traffic started increasing and more images are being stored in S3 buckets. If you run your website in EC2 then you are responsible to manage the server by adding new RAM or CPU to handle increasing traffic during peak time, this might help for a while; but as traffic increases, your website code will stall and eventually give users 500 Error Codes.
Now, your website is down and users are leaving your website because it can’t handle more requests than it could process. If you think to add more instances as backup or use Auto Scaling Group ( ASG) to scale up /down your infrastructure based on the incoming requests, that could be a solution, but more expensive as well! here’s what Lambda can fix.
AWS Lambda is an on-demand cloud computing resource offered as function-as-a-service by AWS that allows you to deploy your code to the cloud, without worrying about the unpredictable issues in the server. With Lambda, you simply deploy the code and let AWS take care of the heavy lifting.
In summary, lambda is the perfect solution when you have code executed based on some external triggers — called event-driven.
EC2 vs Lambda Comparison
Below two diagrams summerise a high-level comparison between both EC2 and lambda.
Which one to choose?
It’s clear now that both EC2 & Lambda have a clear differences and can’t say one is better than other. However , there are some factors that help you to use one over the other. Check below example’s.
If you have a corn job run everyday midnight to proccess some files and send the result to S3 bucket. Then, a Lambda function would be a perfect solution and AWS will charge you only when the corn job is run and Lambda function being invoked. On the other hand , if you have an EC2 server and run a job only at midnight, AWS charges you for every second the instance is running.
EC2 is idle for web serving websites ( e.g. WordPress website) that need to be up and running all the time. In addition , using EC2 , you can create your own preconfigured AMI that can be moved to another region or cross account and reuse these resources and configurations later. in contrast , Lambda can’t do this!
A lot more to talk about .. Thanks for reading.