Hello Readers, In this blog, we’ll learn about Lambda Vs EC2 i.e difference Of Lambda and EC2 in environment,use cases and money matters.so firstly we will see what is serverless and EC2 and then ll go through the topics one by one.
What is serverless ?
This is a way to build and run applications and services without having to manage infrastructure.
properities of serverless:
- firstly ,No servers to provision or manage
- It automatically scales with usage – It do not have to define autoscaling group ,autoscaling policy etc if the traffic goes up ,it will scale up and when traffic goes down it will scale down
- Never pay for Idle- you pay for what you use you.you never pay for idle.
- Highly available- It is inherently highly available in the backend .It’s automatically deployed in more than one availability zone.
so some of the example of serverless services
Amazon API Gateway
AWS step functions
Amazon simple queue service
When to Use Serverless
Trying to decide whether serverless is right for you? Consider these reasons below.
1.Firstly,If you want to scale your web application.(it scales with demand automatically)
2.If You want to reduce server costs.(it significantly reduces server cost, because you don’t pay for idle.)
3.If You want to free up developer resources.(it frees up developer resources to take on projects that directly drive business value)
What is EC2?
An EC2 is a virtual machine that represents a physical server for you to deploy your applications. Instead of purchasing your own hardware and connecting it to a network, Amazon gives you nearly unlimited virtual machines to run your applications while they take care of the hardware.
It provides scalable computing capacity in the Amazon Web Services (AWS) Cloud.It supports variety of operating systems and it is actually backbone of multiple services.for example
Amazon Elastic container service
Aws Elastic container
above all ,are the basic introduction of our topic now the difference of serverless (lambda) and EC2
AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an ecommerce website.
- Underlying infrastructure managed by Cloud Provider.
- Scales Automatically
- No Patching headache
- Can’t install software (e.g webserver , Appserver) in underlying environment
- Code libraries can be installed
- Easy selection of compute power
- 128 MB to 3 GB memory
- 1 sec to 15 minutes time limit
- No attached hard disk, deployment package size limited
- Inherently Highly Available( multiple availability zone)
Superpower of lambda :Easier to onboard,focus on solving business problem from get go.
so,now environment of ec2
- Users control underlying infrastructure – VM size,OS,AMI etc.
- User need to handle scalability, AMI rehydration etc.
- Requires management and Orchestration
- Install almost any software
- Prepackaged AMIs with different software available
- Adjustment of VM parameters requires some work
- Think of it as changing EC2 instance
- hard Disk(s) attached to EC2
- User need to establish high availability (multiple availability zone )
Superpower of EC2 : Complete control of environment,rich ecosystem.
After that ,differences in use cases
Use Cases difference
- Shines at event driven architecture
- Native integration with other services
- Example- Triggered by S3, Kinesis
- Suited when traffic is unpredictable
- Automatic autoscaling
- Pay as you go
- API Gateway integration
- Code is modular without software dependencies, e.g – python APIs
- Easier to migrate cloude native,green fields apps
Kryptonite – for brown field monoliths to lambda,major refactoring needed.
- Faster migration to cloud with other softwares
- Webserver, Appserver
- App requires third party software
- suited when traffic is predictable
- You pay for the underlying VM regardless
- Scales an entire VM
- Easy to move API with dependencies e.g. Spring Boot with Discovery layer
- Consider cost and complexity for green field
Kryptonite – Day 2 operational overhead,generally underutilixed CPU/Memory leads to cost waste.
after that, differences in money matters using some calculated use cases
Money Matters (use cases)
- let’s say
one of your lambda is running 3 million/month, 512 MB memory, 300 ms execution time, unpredictable traffic so, using the lambda cost calculator you are going to pay doller 8 per month( $8/month).
2. let’s say
one of your lambda is running 90 million/month, 512 MB memory, 250 ms execution time, unpredictable traffic so, using to pay dollar 206 per month($206/month).
one of your lambda is running 3 million/month, 512 MB memory, 300 ms execution time, unpredictable traffic so, using the lambda cost calculator you are going to pay dollar 29 per month ($14.4(t3.small)* 2 (for highly availbile)= $29/month)
so cost will increase during higher spike because of scaling.
one of your lambdais running 90 million/month, 512 MB memory, 250 ms execution time, unpredictable traffic so, using the lambda cost calculator you are going to pay dollar 58 per month.(($28.8(t3.small)* 2 (for highly availbile)= $58/month)).
so Predictable traffic makes it possible to select proper VM, higher CPU utilization.
in conclusion ,One is not cheaper or pricier than the other it depends on the use cases.
in conclusion :
so we’ve learned about Difference Of Lambda and EC2 in environment,use cases and money matters with the help of use cases.