How to choose between AWS Lambda in Serverless vs EC2 Instances

AWS Lambda

AWS Lambda is a computing service that is used to run the application code in a serverless environment. This can be used as a compute engine to run the existing code (uploaded as a zip file or container image). Also, one can write his own Lambda function to run it independently or with container tools. This Function as a Service (FaaS) is very helpful to developers as they do not need to worry about configuring and managing the servers and just focus on business logic.



AWS EC2 is also a compute service that is used to run the application code inside a virtual server. It provides virtual computing environments called as instances. Each instance may act as a virtual server.


Lambda Evolution

EC2 Generation

Initially AWS planned to provide infrastructure as a service through EC2 in order to overcome certain overheads like managing and provisioning of resources. It was providing monitoring, provisioning and alerting.

Unfortunately when it was launched, it was more volatile and failed to provide some of the above features. As a result, users were experiencing several issues like outages, failure in scheduled provisioning etc.

PS: This does not mean AWS EC2 as a service is bad. But, AWS wanted to make it user friendly that they took the below steps and packaged EC2 as a managed service with EB

EBS Generation

Then Elastic Beanstalk (EB) came. It provided all the facilities in a package and supported many languages. It helped users to upload their code to VMs through AWS console and provided the load balancer URL to access the application. In spite of it, DevOps could still manually interfere into these processes and manipulate it as needed. Best part of the EB is that load balancers, scaling, security etc could all be configured out of the box for applications deployed here at a marginal cost.

Serverless Lambda Generation

To overcome the manual intervention in the processes, Lambda was introduced. Lambda supported all the features of EB like multiple language support, uploading the code directly through AWS console. But it used ECS to automatically build the function in order to prevent manual intervention. Lambdas are exposed through API gateway. In this way, everything was automated.

Let us look at some of the common areas of infrastructure and highlight the key benefits of using Lambda/serverless over traditional EC2

Infrastructure Management

Setup and ManagementMuch work is not needed in order to deploy applications like container creation, deployment etc.It may not work for one who work in a container like environment and orchestration.Like a legacy way of deployment, one needs to SSH into the EC2 instance, get the application through git or container and deploy it.This comes in 2 options. One is to deploy the application like a desktop, another is doing it in an advanced way which will serve data faster.
SecurityProtects from manually updating the OS and patching.Higher flexibility here increases the surface attack. One need to consider the communication inside and outside application for this.If the number of functions go high, monitoring becomes tedious.Because of statelessness of the functions, malicious activities cannot be done.Function can be scaled automatically to avoid DDOS attacks which in turn will also increase the bill.In EC2 instances, one has to manage the security in the security layer like permissions, opening ports etc.It is tedious to have policies for each and every permission to meet business goals.Security is controlled at VPC , subnet level and also from an instance level using Security groups

Performance Comparison


Courtesy: freepik

TimeoutLambda has a timeout of 300 seconds. While it is getting triggered by the API gateway, the gateway takes upto 30 seconds to invoke the function which makes it challenging. Lambda also has the problem of cold starts which are addressed in the modern versionOn the other hand, EC2 has flexible options. One can run his program with high timeouts also. It is not error free though. Although this may not be needed for standard transaction processing, it’s ideal in case of batch applications or reporting needs.
DependenciesLambda has a 50 MB size limitation. However, it supports the 512 MB ‘/tmp’ file system. More time will be needed if the dependencies are greater.EC2 instances have their own temporary storage. So one just needs to configure it properly with correct CPU numbers. Otherwise, the CPU will be burnt.
Automatic Scaling is one of the key features of Lambda functions. If there is increased traffic, then the concurrent executions will also be increased predominantly.In EC2, everything is under one’s own control including scalability. He can configure it manually. However AWS has autoscaling groups for this feature as well.
AvailabilityLambdas are available all the time. It can be scaled down or up automatically based on the requests. This reduces the cost basically.EC2 instances are not on-demand like Lambda. Although one can configure auto scaling options for this, at least one instance has to be up all the time.
LatencyCold starts occur in lambda since the cloud provider has to provision the resources only after the request is given. This may take upto 5 seconds.In EC2 instances, there are no cold starts unless a new container is getting deployed.

Cost Comparison

Cost is based on the memory provisioning per second.Cost is based on the time instances are running and the configurations of the instances.
Ideal for small businesses.Feasible for large user requests.


Offers versioning control over the systems.Offers Snapshots from EBS (Elastic Block Storage)
One can have a beta version for testing without affecting the stable one. Also he can test the function without publishing.Data is stored in a snapshot from the EC2 creation and different snapshots will be created from time to time.
When a function is published, it is done as a new version.When an old snapshot is deleted, the data will be transferred to the next snapshot, In this way, the data will be mutually exclusive.

Common Problems


Courtesy: freepik


  • Storage – EC2 uses EBS (Elastic Block Storage) for storage. There are two types of it: standard and provisioned EBS. One need to understand clearly the Input Output Operations per second to make this efficient.
  • Idle Instances – Though AWS supports auto scaling groups, at least one instance needs to be running all the time even if it is idle. This increases the cost of EC2.
  • Memory Leaks – EC2 instances don’t allocate memory themselves. Rather one needs to predict how much memory will be needed. If the memory required exceeds the memory allocated, then the application crashes entirely.
  • Other common issues and troubleshooting are here


  • Cold Start – Application will not be up till all the resources get provisioned by AWS. This may take upto 30 seconds.
  • Timeout – Lambda has a default timeout of 30 seconds. If the application up time exceeds this, then the application will not go up.
  • Other common issues and troubleshooting are here

Related Posts

From Flutter to React Native: The Top Cross-Platform Frameworks to Choose in 2024

From Flutter to React Native: The Top Cross-Platform Frameworks to Choose in 2024

Cross-platform app development has become indispensable in the fast-paced world of mobile applications. In 2024,

Open Banking Revolution Leveraging APIs for a Frictionless Financial Future Open

Open Banking Revolution: Leveraging APIs for a Frictionless Financial Future

In the dynamic landscape of modern finance, the Open Banking Revolution stands as a beacon

The rise of wearables: How technology is transforming personal health tracking

In the era of digital transformation, wearables have emerged as pivotal tools, reshaping the landscape