There are 3 different types of compute services provided by AWS: EC2, ECS Fargate and Lambda. They could be confusing at the beginning if you do not know their differences and how they work. I have finished many designs which involve choosing the right compute service based on their pros and cons. In this blog, I will summarize and try my best to explain how you evaluate when you use them in your solutions.
Have you ever heard of Infrastructure-as-a-Service(IaaS), Container-as-a-Service(Caas) and Function-as-a-Service(FaaS)? These are different levels of abstraction. The more abstraction it becomes, the less operational burden it has and the less flexibility it has. These 3 different types of compute services refers exactly to these 3 abstraction level. We will get to the details later.
EC2
Infrastructure-as-a-Service(IaaS) is offered by EC2 compute service. With EC2, you specify the configuration environment needed for your application, and IaaS provider ensures that your resources are always availble. For example, you tell EC2 which host type, which operating system you need for you application and then EC2 prepares all the resources to you.
You manage all the complexity of a fully available server environment with IaaS provider, which means most flexibility and most operational burden. If your service needs that flexibility, such as customizing auto scaling strategy, EC2 is your choice.
Furthermore, achieving high abstraction usually add more overhead cost to the service, such as latency. If you run the same application, EC2 has the lowest latency. Therefore, if your application has a strict requirement on latency, EC2 outstands from all choices.
Lambda
Function-as-a-Service(FaaS) is offered by Lambda. With Lambda, resources are assigned when service is triggered, and then released when service is not running. This is the highest level of abstraction. You do not need to worry about how infrastructure is set up, and just focus on the service logic.
One advantage of using Lambda is that it is easy to manage and maintain. All infrastructure set up are taken care of by Lambda. However, this also limits flexibility. Lambda functions has restrictions on cpu, memory and service execution time. If your service has heavy workflow and requires such a long time to finish that Lambda does not allow, Lambda is not available option. Also, you do not know the ip address of host which runs the service, so you cannot log onto the host to check artifacts generated by your service or application logs, which makes troubleshooting difficult. Therefore, Lambda is more suitable for event-driven applications, but not for long running applications.
Fargate
Container-as-a-Service(CaaS) is offered by ECS Fargate. With Fargate, you can escape from EC2 orchestration. The difference from EC2 is that you handle with containers running on the host, instead of host. You register containers on the ECS service, and deploy those containers to EC2 instances, which does add more operational complexity.
Fargate is a compromised solution between EC2 and Lambda, which provides a great balance between operational burden and flexibility. Compared with EC2, Fargate becomes slower, however Fargate makes it easier to set up scalability and security. Compared with Lambda, Fargate has long running resources created and task ips are provided. You are able to log onto tasks to check artifacts and logs. And there is less restriction on cpu, memory and service execution time. Therefore, Fargate is suitable for long-running applications.
Summary
EC2 has the most operational burden, AWS Lambda has the least and AWS Fargate is somewhat in the middle on the spectrum.
EC2 | ECS Fargate | Lambda | |
---|---|---|---|
Type of Compute | Instance | Container | Function |
Use case | All applications | Long-running applications | Event-driven applications |
Scalability | Manual scaling by ASG | Automatic scaling | Automatic scaling |
Latency | Low | Higher | Higher |
Timeout | N/A | N/A | < x minites |
CPU & Memory | N/A | N/A | has limitations |
IP address | Instance IP | Task IP | N/A |
Pricing | Instance type, Launch type | Task resources used | # of request and execution time |
Comments powered by Disqus.