Confused with ECS and how it deploys

I read somewhere that if we use AWS ECS service, we will have to maintain the target server. In this case, I’m assuming that the target server is an EC2 instance running docker daemon. Am I right?

If my understanding is wrong, where do EC2 deploy containers?


Related discussions:

You can run ECS on EC2, where you’ll administrate the underlying instances yourself. Or you can run it on Fargate, ie serverless, where AWS maintains the underlying layer for you.

You have two options:

  1. Fargate - where you describe how you want the container(s) to run and let AWS worry about the underlying infrastructure.
  2. EC2 - where you have the containers run on EC2 instances in your account.

Fargate is better for scheduled or triggered tasks. But for long-running tasks (such as servers), it may work out more expensive than EC2. It’s also a little more restrictive in regards to configuration, but you should be aiming to make your containers as generic as possible anyway.

EC2 doesn’t require that you manually set up and configure every server in your cluster. I mean, you can do it that way. But it can also be set up in a minimal-maintenance way using capacity providers, autoscaling groups and generic AWS AMIs. This means that EC2 servers are automatically spun up (and terminated) within your account in response to the needs of your ECS tasks. The AMIs from AWS are already preconfigured with docker and the ECS agent installed, all you have to do is a couple of lines in the startup script to tell the instance the name of the ECS cluster that it belongs to.

ECS then takes care of everything else.

“Maintaining” the target servers in this case is relatively minimal. The main things you need to watch out for are:

  1. Disk space - ensure that your docker containers are restricted and the data they write is truncated/rolled over/mapped to EFS
  2. Patching - you will need to “upgrade” the AMI in your auto-scaling group on a regular basis to take the most secure version. Manually patching the servers yourself is a fool’s errand. You can then trigger an instance refresh to replace all of the servers in the cluster with zero downtime.
  3. Sizing - When you launch an 8GB task in Fargate, the Fargate service will figure out how to deal with it. If you launch an 8GB task in EC2 but all of your servers are 4GB, then it will just sit there and spin.
  4. Health checks - the EC2 servers do “break” like any others and develop weird issues. Not being able to write logs, losing DNS connectivity, whatever. So it’s not entirely fire-and-forget. For the last 18 months, I have about 50 EC2 servers in ECS, which on average get recycled every week or so. I’ve had to intervene and kill/refresh a buggy server about five times in total.

Good answer!

WOW! Amazing and very detailed answer. Our developers(the people we support) would like their containers running 24x7x365. From what you wrote, Fargate is not meant for long-running tasks. Our containers are actually micro services. That’s how our developers are doing it. Would that mean Fargate shouldn’t be used for some of our situations like the one I just described?

You can run ECS on EC2

So ECS is an agent that can be installed in a linux EC2 instance? From my little knowledge, what I know about ECS is that it is an AWS service. Thank you.

Fargate can absolutely be used for long-running tasks. It’s just that it may end up costing more (in terms of your AWS bill) to use it than EC2. However, there is little to no maintenance needed for Fargate, so you potentially trade the higher AWS bill for lower cost to your team to run the engine (things like refreshing EC2 instances with newer AMIs).

One other potential difference of note: any additional software you might normally run as a host-level agent would need to be added as a sidecar in the Fargate task. This could end up meaning you need a bigger SKU, which costs more.

There is no one-size-fits-all answer here. It very much requires a cost-benefit analysis based on your specific use case.

Fargate is absolutely fine for long running tasks. It’s just more expensive, the tax you pay for not having to bother with all the stuff mentioned above.

I personally I would recommend Fargate every time, since it removes a lot of operations costs and headaches. And that’s coming from being on call for the last 10 years.

+1 here.

If you do not need to have access to the EC2 for some reason, or if Fargate’s limitations are blocking you from doing something, then you need to use EC2-based ECS. This could be: having SMB/NFS mounts on EC2, needing access to docker.sock, special config on EC2 side, etc.

If you do not need this special cases, then I would strongly suggest you go for Fargate. It is crazy optimized, and works very well. For Fargate, you can buy Savings Plans. EC2 reserved instances turn out to be cheaper on the AWS cost side, but if you calculate the man-hours you spend on maintenance, EC2 becomes much more expensive.

Also, make sure you understand the interactions with LB healthchecks, ECS healthchecks, kill signals, and docker entrypoints / commands / tini if needed. You might have a lot of issues with deploying otherwise.

For simple use cases, Fargate is very much amazing.

You can make any EC2 server into an ECS instance. To do that, you:

  • Install ECS agent
  • Register the installed ECS agent with an ECS cluster

Then your EC2 is a ‘part of’ the ECS cluster. You can start initializing tasks on your EC2 instance.

You are correct, but I would clarify one thing: it is better to make an EC2 instance into ECS capacity by launching that EC2 instance using the ECS Optimized AMI. Installing and setting up the ECS agent yourself, from scratch, is pretty error prone. If you just launch the EC2 instance using the preprepared ECS Optimized AMI then it will be able to self register into ECS out of the box, and all ECS features will work.

I generally find when folks install the ECS agent by themselves, that they get some config wrong and some of the ECS features don’t work right because they are missing important underlying system settings, or something similar.

There’s no “not meant”. Fargate is equally robust for long-running tasks as it is for finite ones.

But do your calculations, remembering that once you get your container working properly in fargate, there is little or no maintenance for you, so that’s time you’re saving.

When pricing up, you have to be realistic. For example, if I price up a 2CPU/4GB Fargate “always on” task on Linux, it comes out at $70/month

If I price up a t3a.medium Ec2 instance (2cpu/4GB), it comes out at a like $30. Seems like a no-brainer, $40 cheaper per month, per task.

Except that’s not realistic. That task won’t run on a t3a.medium. You’ll need a minimum t3a.large. That’s $63/month. Now you’re saving just $7 per task, and you still have the overhead of managing that box.

On Windows, the price differential is much larger, but the overhead of managing Windows servers is also much larger.

Ecs is basically docker that Aws hosts for you.

This is true. Use the ECS-optimized AMI if you go the EC2 route.

we use fargate for our 24x7x365 microservices. It takes out a lot of the headache and makes integration into a CICD pipeline smoother.

What OP was saying is that EC2 instances can be cheaper from an infrastructure perspective if you’re running them long-term. Basically fargate is more expensive but requires less upkeep, whereas EC2 is cheaper but you need to maintain some of it yourself.

It’s also important to consider the load. Long running tasks that spend a lot of time idle aren’t as expensive.

Or Bottlerocket!

Thank you for the info. This is very good information!

Got it! Thanks a lot!

No, use fargate, unless you want to deal with all the overhead of managing more EC2 instances. Fargate is totally worth the relatively small amount of extra cost.