Lambda vs EC2 cost comparasion

Lambda vs EC2 cost comparasion

Over the last couple of years, lambda (Function as a compute service) and serverless are becoming the first choice for application developers over EC2  (Traditional compute service). However, when it comes to lambda vs ec2 cost comparison, on first look, lambda looks cheaper because of its monthly free tire. But is really lambda is cheaper in lambda vs ec2 cost comparison in all use cases. Let do a load test of 3 use cases on lambda and ec2 to see how costing comes out.

 

Use Cases

  1. Database Insertion (require low CPU, memory with low execution time)
  2. Create a thumbnail on s3 (good amount of execution time required with CPU and memory)
  3. Sorting algorithm to sort 5,000 numbers (reasonable execution time with high CPU and memory usages)

Setup and Configuration

Lambda

Memory: 128 MB

Execution Limit: 30 sec

Permissions: Dynamo, S3 Access

Language: NodeJS

EC2

Instance Type: t2.micro

CPU: 1

Memory: 1GB

Language: NodeJS

A node server is created to listen on port 80.

Load Generation

Use multiple EC2 (t2.micro) instances to generate load on Lambda and EC2. For simplicity, we will generate consistent load cycles for 10 min with a gap of 5 min. Repeat the cycles till the time EC2 is not exhausted for CPU utilization.  Then repeat the same cycles for Lambda. Use SNS to trigger EC2 URL and Lambda Invocation. Load servers will generate SNS messages as per following tables and which in turn invokes Lambda and EC2 URL.

Load Time (s)No of EventsEvents per secEvents per month
600500 0.83333333331.73M
60010001.6666666673.46M
60020003.3333333336.91M
60050008.33333333317.28M
6001000016.6666666734.56M
6002500041.6666666786.40M
6005000083.33333333172.80
600100000166.6666667345.60
600200000333.3333333691.20M

You can download code setup from GitHub https://github.com/dkkumargoyal/lambda-vs-ec2-cost

Result

Use case 1: Database Insertion

lambda vs ec2 cost comparison table along with CPU utilization and avg execution time of lambda from monthly events

Events per monthLambda Avg Exection TimeLambda CostEC2 CPU UtilizationEC2 Cost
1.73M76.50.150.828.5
3.46M58.30.490.8478.5
6.91M55.81.181.538.5
17.28M47.93.263.178.5
34.56M39.86.715.598.5
86.40M57.820.3111.78.5
172.80M40.647.5238.5
345.60M63101.8639.38.5
691.20M36.3210.5973.78.5

lambda vs ec2 cost comparison chart from monthly events

EC2 CPU utilization chart

Lambda avg execution time chart

Use case 2: Create a thumbnail on s3

lambda vs ec2 cost comparison table along with CPU utilization and avg execution time of lambda from monthly events

Events per monthLambda Avg Exection TimeLambda CostEC2 CPU UtilizationEC2 Cost
1.73M36406.5919.78.5
3.46M373020.6838.78.5
6.91M385049.9681.28.5

lambda vs ec2 cost comparison chart from monthly events

EC2 CPU utilization chart

Lambda avg execution time chart

Use case 3: Sorting algorithm to sort 5,000 numbers

lambda vs ec2 cost comparison table along with CPU utilization and avg execution time of lambda from monthly events

Events per monthLambda Avg Exection TimeLambda CostEC2 CPU UtilizationEC2 Cost
1.73M7940.153.178.5
3.46M10901.675.938.5
6.91M146015.5411.78.5
17.28M142047.7228.88.5
34.56M1480106.6257.38.5
86.40M1550289.471008.5

lambda vs ec2 cost comparison chart from monthly events

EC2 CPU utilization chart

Lambda avg execution time chart

Conclusion Lambda vs EC2 cost

  1. Lambda vs EC2 cost, Lambda wins if average utilization of EC2 is less in all three use cases.
  2. Lambda shines for use case if average execution time for Lambda is small 100ms to 200ms
  3. More the lambda execution time it is better to use EC2
  4. Costing on Lambda on the number of execution is very cheap in comparison to execution duration so reducing the duration is a must.
  5. In all use cases, lambda was able to handle approximately 2M per month requests which is more than enough for a large number of applications.
  6.  Go to EC2 for load where an instance can be utilized close to 100%. This average should be calculated for a considerable period.
By |2018-03-17T23:43:11+00:00February 28th, 2018|AWS, Lambda|0 Comments

About the Author:

Leave A Comment