Many AWS users are getting confused are struggling to decide between EC2, ECS, and Lambda for their compute. The most frequent question I hear get asked is ‘How exactly do AWS EC2, AWS ECS, and AWS Lambda differ from each other? Further, how do I decide the right choice for me?
Today In this blog, we’ll be looking at three different computing services on AWS, namely, Amazon Elastic Container Service (ECS), Amazon Elastic Compute Cloud (EC2), and AWS Lambda. By the end of this blog, you will be able to understand which one among the three is the right fit for your next project. Without any further delay, let’s get started with our topic.
Before jumping into each of the related AWS services today, we quickly need to touch on different execution modes for our compute tier: specifically IaaS, CaaS, and FaaS.
IaaS: IaaS is abbreviated as Infrastructure-as-a-service. It is a form of cloud computing that provides virtualized computing resources over the internet. It is one of the significant and widely used cloud computing services. Primarily in this model, the cloud service provider manages IT infrastructures such as storage, server and networking resources on behalf of customers and delivers them to user organizations via virtual machines, accessible through the internet.
In relevance to the topic, AWS EC2 comes under the IaaS service category.
CaaS: CaaS is abbreviated as Containers-as-a-service, is a cloud service that allows software developers and IT departments to upload, organize, run, scale, manage and stop containers by using container-based virtualization. A typical CaaS provider will provide a structure that allows users to make use of the service and allows its users to make use of application programming interface (API) calls or a web portal interface.
In relevance to the topic, AWS ECS comes under the CaaS service category.
FaaS: FaaS is abbreviated as Function-as-a-Service, is a kind of cloud service that allows developers to build, compute, run, and manage application packages as functions of code without having to maintain their own infrastructure. A general FaaS provider facilitates this in an event-driven execution model that allows users to focus solely on individual functions in their application code.
In relevance to the topic, AWS Lambda comes under the FaaS service category.
Now, let us look into each of these aspects in brief.
What is AWS EC2?
EC2 is one of the oldest services offered by AWS cloud and is known as Elastic Compute Cloud. Users of EC2 can rent virtual machines from AWS that can spin up or down with a set of resources at any time. It provides instance types to fit any kind of workload. Amazon EC2 comes under infrastructure as service (IaaS), and with this, you can configure your CPU, memory, storage, and networking capacity for your instances.
While using EC2, you need to manage your own instances or virtual machines. What I mean by this is you can spin up or down at any time as long as you want. It is kind of like buying or renting your own hardware and hosting that in-house in your local network. With EC2, you can bring an instance up whenever you need it and tear it down whenever you need it, so you don’t need to pay for that long-term commitment to purchasing hardware.
In EC2, you have the freedom of picking the different types that are dependent on your use case. Within the EC2 instance, there are a handful of popular machine types:
- Memory optimized tier
- Compute optimized tier
- Storage optimized tier
- GPU processing tier
For a complete list of types, see this link.
On the whole, there are many other options available here, so the size you select depends on the capacity you need. Whether it’s memory, compute or storage, the classification that you choose depends on your use case.
One of the main reasons that people love using EC2s is because they’re extremely flexible, and that’s because, like you’re renting instances, you can do whatever you want with these. You can install databases; you can put a WordPress site; you can host a cluster; broadly speaking, you can essentially do whatever you want.
Apart from all these pros, there are few cons as well; that is, you need to worry about security, that is, making sure no vulnerabilities are getting in and out. So you need to take care of things like ensuring the OS is patched and frequently monitoring that everything is up to date and similar kinds of stuff.
What is AWS ECS?
ECS, abbreviated as Elastic Container Service (ECS), is a scalable container management service offered by Amazon Web Services (AWS) with a high-performance container orchestration service that supports Docker containers. It bypasses the need for you to install and operate your own container orchestration software, manage and scale a cluster of virtual machines, or schedule containers on virtual machines.
It helps developers with AWS accounts deploy and manage scalable applications that run on groups of servers called clusters through application program interface (API) calls and task definitions to be deeply integrated into the rest of the AWS ecosystem.
ECS is useful for applications that are already embedded in the docker ecosystem or those looking for container orchestration.
In order to run docker containers, you need some kind of infrastructure to run them, and for that, there are two options available to manage your Elastic container service (ECS).
One is a serverless option known as AWS Fargate that removes the need for you to interact with or think about servers or clusters. It is by far the best option for developers to focus only on designing, programming, and building their applications instead of managing the infrastructure that runs them.
The second way is using EC2. Here users need to take care of software upgrades, patches, identifying security vulnerabilities and so forth. There is a little bit of extra work for you to do if you decide to go with the EC2 option when you’re running ECS.
Underlying relation of ECS & EC2
Now perhaps you understood the usage of ECS and EC2 individually, but if you carefully read the above paragraph, there you see a co-existence between them and what I mean by that is now in the ECS model, you’re operating on clusters and tasks, and a cluster is nothing but an entity with a set of EC2 machines within.
Precisely speaking, the operational model to use for default ECS is that you treat a pool of EC2 instances as an abstract resource, and then you deploy tasks to these clusters. These tasks can either be long-living, such as hosting an application and just receiving traffic, or they could be one-off things kind of batch processing job that needs to take 20 minutes, 30 minutes, even up to an hour.
I have a detailed blog on a complete overview of AWS ECS; please read this blog to know more about the detailed process of container orchestration using ECS.
What is AWS Lambda?
Lambda is a server-less environment on AWS cloud that lets users run their code without the need of provisioning or managing machines to handle workloads. AWS Lambda will scale up or down by provisioning machines on behalf of me and deploying code whenever traffic comes in so that those machines can host your application. It comes under the category of FaaS.
You need not maintain anything here. All you need is to deploy your code and upload it to the AWS Lambda service. Then you need to invoke your Lambda function, and it can serve as a restful endpoint.
With lambdas, you operate at function level; there are no containers, and there’s no concept of infrastructure; there is no EC2 nor management of security. So, how do things work with Lambda?? Well, with Lambda, it essentially works on the code unit and essentially just uploads code to the lambda function backend. It can be a zip file or like a jar file which contains hundreds of thousands of files that consist of code and dependencies to run.
Once you finish uploading, give it an entry point. Behind the scenes, Lambda will provision an EC2 instance automatically. It’ll load up all this code onto that machine, and it’ll invoke the function of whatever you uploaded to the console. It will also have some other trade-offs, such as that you can only have a 15 minutes maximum invocation time for a single lambda function.
Comparison of EC2 vs ECS vs Lambda
If you look into these three services, they all are part of the Compute Engine services of the AWS ecosystem. Though they look very similar, they do have differences, and let’s compare each of them.
|Type of compute||Infrastructure as a service (IaaS)||Container as a Service |
|Function as a service (FaaS)|
|Defining Entities||Virtual machines||Containers||Build code files|
|Use cases||General computing; complete control over instance||Running Docker containers|
and container orchestration
|Serverless function deployment|
|Scalability||Combines with Auto Scaling services Amazon load balancer||Automatic scaling based on desired count of tasks||AWS Lambda automatically scales on behalf of user|
|Configurations||Memory, storage, Networking||Tasks and clusters||Invoke of lambda functions|
|Security||Should take care of security vulnerabilities||Should take care of security if ECS combined with EC2 Need not worry about security if ECS combined with Fargate||Everything is taken care of by service providers.|
|Pricing||Depends on the instance type, time run, and launch type.||Depends on the choice of EC2 and underlying resources or Fargate vCPU & memory used||Depends on the number of invocations, the memory allocation, and duration of invocations.|
|Management aspects||Need to manage your own resources individually||When ECS combines with EC2, you need to manage your resources. Whereas combination of ECS with Fargate doesn’t require any management of resources.||No need to worry about resources, everything is handled by the system itself.|
|Service Endpoints||IPv4, IPv6||Interface VPC endpoints||Interface VPC endpoints|
How to Decide which is right for you?
If you still feel unsure of which is right for you, let me help you. The three of the services mentioned above hail from the compute resources of the AWS ecosystem, and in one or another way, they do serve the same purpose but in a different pathway.
When to pick EC2
If you want to manage infrastructure right and pick the type of infrastructure you desire and are looking for an extremely flexible solution, then AWS EC2 is your go-to choice. Here you get to choose the instance types, whether memory compute or storage optimized, and you also get to pick the size of the instances. So if you require a large degree of flexibility and you want to completely tune exactly how your application is going to run, then I would say hands down, pick EC2
However, you need to worry about security vulnerabilities if you have chosen EC2 to serve your purpose.
When to pick ECS
Consider a scenario where you tend to operate in the system of docker, and you have docker images that get associated with tasks. You associate these tasks with clusters. These clusters can operate on a fleet of ec2 machines as abstract computing units in the case of ECS or AWS Fargate. You treat your resources as an abstract pool of resources and just deploy tasks to them then ECS is your go-to choice.
You can also associate it with load balancers to scale your system up and down according to traffic demands.
When to pick Lambda
Suppose you are a developer who loves code more than anything else and wants to relieve yourself from the stress of handling EC2 or managing docker containers or any infrastructure complexity. In that case, all you need to choose is AWS Lambda. I would say lambdas are better suited for people that are just looking to get out the door quickly, so you really get to offload a lot of the complexity of maintaining applications.
Here the entire system just operates on code, and you can upload your code onto the Lambda functions, and then you invoke your functions to serve your application. You can combine it with some other AWS services to achieve some very, very rich functionality. Learn about Lambda use cases in this article.
Before I conclude this topic, I would also like to speak a few sentences about the pricing as well.
The pricing model of Lambda is a little bit different when compared with both ECS and EC2. Lambda is a kind of premium model you are paying for the number of invocations, the memory allocation, and duration of invocations.
The pricing model of the EC2 is a kind of pay-as-you-go version where your pricing depends on the instance type, time run, and launch type.
The pricing model of ECS is also a pay-as-go version where your pricing depends on the choice of EC2 and underlying resources or Fargate vCPU & memory used.
I anticipate that now you have a fair idea of picking the right choice among AWS EC2, ECS, and Lambda and also be aware when to decide what.
I have a similar kind of video based on AWS Lambda with Docker overview. Watch it here.
If you think this blog helped you armed with enough information, please feel free to subscribe to my newsletter and see you soon with another exciting blog post. Until then, keep learning more about AWS from my previous blog posts.