AWS Lambda Vs AWS EC2: How to choose one over another?

Promatics Technologies
9 min readJan 12, 2021

--

Cloud computing has grown in to a mature market during the last decade. With humble beginnings several years back it is a reckoning force now which is looking to eat all other alternatives soon. But many people who are not from a tech background do not know that within the cloud world there are many services. Many a times it is difficult to to distinguish between two cloud offerings. Choosing one over another can make a hell lot of difference to your server costs. Two of the most popular offerings are Amazon Web Services EC2 and AWS Lambda. Lets dig deeper.

About AWS EC2

Amazon Web Services’ Elastic Compute Cloud came around to ease the provision of computing resources for developers. It aimed at providing self-service architecture to businesses serving them on-demand with resilience.

a.) AWS EC2 reduces the time required to spin up a new server to minutes from days or weeks of work, unlike the on-premise world.

b.) AWS EC2 scales up and down quickly and almost instantly based on the computing requirement.

c.) The service architecture facilitates an interface to configure capacity with minimal effort.

d.) AWS EC2 allows wholesome admin access to the servers simplifying infrastructure management.

e.) The architecture also enables monitoring, security, and support for multiple instance types like a wide variety of Operating Systems, Memory, and CPUs.

About AWS LAMBDA

AWS Lambda allows businesses to eliminate infrastructure management of computing. With Lambda, developers can concentrate on writing the function code without worrying about provisioning infrastructure.

a.) Lambda is designed to emulate an event-based service that gets triggered by events like a new file being added to an S3 bucket, a new record added in a Dynamo DB table, and so on. Lambda can also be invoked through API Gateway to expose the function code as a REST API.

b.) There is no need for forecasting of the resources (CPU, Memory, Storage, etc.).

c.) AWS Lambda can scale resources up and down automatically, which makes it the epitome of server-less

d.) It reduces the idle time of computing resources when the application is not in use.

e.) Lambda logs can be monitored through Cloud Watch, like EC2.

f.) Lambda is charged based on the execution time and memory used.

Infrastructure Management

I.) Setup & Management Environment

Businesses often need to install and configure all the required software in an automated and reproducible manner. Even a simple application on EC2 needs an intelligent forecast about the capacity it would need. Next, businesses need to configure how to spin up the Virtual Machine. After setting up a bastion server to securely SSH to the VM, they must install the required software, web server, and so on. EC2 allows businesses to manage the scaling by setting up an Auto Scaling group. And that’s not all; companies need to do the load balancing in case multiple instances of applications are installed using various EC2 instances.

Regardless of whether a business needs to set up multiple or single environments, AWS Lambda makes it simple. AWS Lambda is not an on-demand development environment with containers and orchestration in place. Lambda omits the need to spin up or provision containers or make them available for the applications, thus making scaling fully automated for businesses that leverage it. In the case of Lambda, there is no need to worry about the provisioning of VMs, software, scaling, or load balancing. It is all handled by the AWS Lambda service. Businesses just need to compile the code and deploy to Lambda service, making scaling automated. They need to configure how many max concurrent executions they can allow for a function. Load balancing is handled excellently by Lambda itself. So here, we can see AWS Lambda is a clear winner.

II.) Security

Businesses that opt for AWS EC2 need to take care of the security layer at the instance level. The security layer decides and controls the traffic allowed to communicate with each instance. Each instance may have multiple security layers that dictate the allowed inbound traffic through specific protocols like TCP, UDP, ICMP, etc. Managing permissions for every particular business needs changing policies often, and you end up increasing the unwanted granularity. Meanwhile, with Lambda, this is entirely taken care of by AWS along with OS patches and system maintenance. In the case of DDOS attacks, businesses need to opt for other services of AWS Shield. They can also do it manually by using ELB to scale under the attack or by limiting the rate of requests from a particular id. The security groups and firewalls for EC2 are not enough to monitor resource-based traffic monitoring. Here the AWS Shield helps in automatically detect the type of AWS resource behind the elastic IP address and apply the appropriate DDOS protections.

The serverless architecture of AWS Lambda omits out the patching and OS update manual work. It lets businesses enjoy higher flexibility of workflows, but at the same time, it also increases the surface attack. Companies need to consider how secure communication is happening inside and outside your application. With Lambda, the granularity of functions obtained is really high. Business apps with multiple features, find it hard to monitor, which in turn is a threat to decaying functions. However, with Lambda, vulnerability breaches are less likely due to the stateless characteristic of the services. Also, features are automatically scalable, which gives excellent protection against DDOS attacks, but it also increases your bill.

Performance Comparison

A.) Timeout

Amazon EC2 comes with pretty flexible options. It facilitates long-running tasks because EC2 instances are available for different types of requirements with different configurations. However, EC2 is plagued by connection timeout due to overlapping security group rules and unidentified user key by the server. Another common error results from an insecure network over SSH.

AWS Lambda has the prescribed timeout of 300 seconds. The prescribed timeout puts a limit on the type of tasks AWS Lambda can deal with, making it a bad pick for long-running functions and other complex tasks. Further, there is a time limit of 30 seconds to be imposed by the API Gateway for the function. Even if a business succeeds at coming up with a solution that handles timeout sufficiently, there is a need to constantly monitor and debug the issues when they surface. Some timeouts also occur due to the bugs introduced or when communication happens over external servers, which is time-consuming. Still, AWS has introduced Step Functions to overcome the Lambda’s timeout constraint.

Besides limits, timeouts can also occur due to the downstream system’s application integration as well. And that holds for both EC2 and Lambda functions.

B.) Dependencies

All applications run with the help of external libraries. AWS EC2 imposes no constraint to limit the number of dependencies for an application. Still, the increase in the number of dependencies for an app, also increases the time it takes to start, further adding burden to the CPU as well. Businesses must consider the size of software packages and corresponding instance CPU.

Heavy processing tasks like image processing, video conversion require dependencies for the application. In the case of AWS Lambda, constraints come in the form of the max. Size of — 50 MB (zipped, for direct upload) and 250 MB (unzipped, including layers). These sizes can be insufficient in certain cases, especially for ML programs, where a lot of third-party libraries are required. But Lambda gives developers the option of downloading the dependencies once your function is executed from its “/tmp” file storage. Moreover, “/tmp” file storage has a limit of 512 MB. Although it can take significant time to download all the dependencies from scratch when a new container is being created, the 512 MB size still offers a restricted avenue for limited use only.

AWS EC2 VS Lambda: The basics

C.) Scalability and Concurrency

EC2 puts businesses in full control of implementing the concurrency and scaling. Auto Scaling groups help businesses define the policies for scaling up and down. Businesses find it tiresome to identify the threshold limits and accurately forecast the # of instances required. It requires efforts like careful monitoring of the metrics.

AWS Lambda scales dynamically in response to the increased traffic. Lambda handles concurrency and scaling through a built-in feature. Businesses define the maximum number of concurrent executions they want a function to be restricted to. There are limitations like max. 3 GB memory (vertical) and 1,000 concurrent executions (horizontal) that limit the program from scaling vertically or horizontally for memory.

D.) Availability

For EC2, businesses pay for the amount of time EC2 instances are up and running without the benefit of on-demand availability.

Lambda charges for the amount of time functions are up and running because Lambda is brought up and spun down automatically, based on the event sources and triggers. Unlike EC2, Lambda is available based on the request invocation. Businesses that opt for Lambda are no longer paying for the idle time between invocations saving up money in the long run.

Use Cases: When to use EC2?

#1. High-performance computing

A large number of EC2 instances allow businesses to create virtual servers to meet their demands. This makes EC2 perfectly suitable for processing complex tasks.

#2. Disaster recovery

EC2 can be leveraged as a medium for disaster recovery for both active and passive environments. It can be turned on easily in case of an emergency, which is what facilitates minimal downtime.

#3. Easy DevOps

DevOps processes are developed in a full-proof way when it comes to EC2.

#4. Development/Testing

Since EC2 provides on-demand computing resources, companies can deploy a large scale testing environment, even in the absence of any upfront investment in the hardware.

#5. Secure Environment

EC2 is known to provide outstanding security.

Use Cases: When to use AWS Lambda?

#1. Low complex code

Lambda is a perfect choice to execute code with minimal variables and 3rd party dependencies. It can process easy tasks containing low complex code in a streamlined manner.

#2. Shorter executing time

Lambda is suitable for executing tasks that are occasional and can be executed within minutes.

#3. Infrequent traffic

Businesses loathe it when servers sit idle, and they still have to pay for it. The pay-per-function model can help them in significantly cutting down their computing cost.

#4. Real-time processing

Paired with AWS Kinesis, Lambda works best for real-time batch processing.

#5. Scheduled CRON jobs

AWS Lambda functions serve well for scheduled events to function at a fixed scheduled time.

Conclusion

Many factors need to be analyzed while making a choice between Lambda and ec2. AWS Lambda is perfect for businesses that want to avoid wasting compute resources due to unpredictable traffic for an application but still want a scalable and cost-friendly solution. If you want to do complex processing, but the process can’t be executed in the limited execution time, Lambda is not suitable for you. Even if you’re going to run a complex application that has consistent traffic and wants to operate in a tried and tested deployment environment, EC2 is the right choice. However, for EC2, the only drawbacks are a complex setup environment and provisioning of servers.

All in all, AWS services promise multiple features that come highly optimized and are highly beneficial for business infrastructure. Businesses that lack in knowledge and ability to conduct an in-depth analysis often face difficulty when it comes to a choice between Lambda vs. ec2.

A team that wants to steer clear of infrastructural difficulties must opt for AWS Lambda. However, Lambda offers certain limitations in terms of the use cases. Moreover, choosing Lambda over EC2 means businesses need to constantly monitor between the good balance that it provides and the cost it incurs.

On the other hand, aws ec2 has become the preferred choice of businesses for hosting an application. It promises complete flexibility to configure the infrastructure for businesses. Still, it is not the best choice to meet all needs of a business sometimes and is preferred next to Lambda.

Fully automated services are always better, not better, especially when they lack features to mitigate the errors. Furthermore, businesses find it difficult to configure manually at an early stage due to a lack of expertise when it comes to platform choice.

If you are a business planning to move your infrastructure to the cloud, or even if you already have the cloud infrastructure in place but require the expertise of AWS professionals, feel free to reach out to us. We at Promatics are a dedicated team of AWS certified consultants and professionals who offer AWS consultancy and solutions.

Note: This article first appeared at: AWS EC2 Vs AWS Lambda

--

--