AWS ECS technical series: Part I

Hey everyone,

If you missed my initial post, I am basically starting a technical series on AWS ECS. More details can be found here!

Here is the first part of it where we build out the network and compute components.

The flow of this module will be top-down. Meaning we will first examine the proposed architecture, design details and then tie it all together by creating the infrastructure in terraform.

Get started here.

I hope you enjoy it! As always feedback are always welcomed!

Good series Jerry. This is the content I like to see being shared on this subreddit.

Great work, thanks for sharing

Thank you for the that great write up. Seeing you are on the West coast hope you are not impacted by the 50mm of rain projected to come down!

great write up.

i’m a little confused when they say ECS spreads tasks across multiple AZs (with EC2). if my underlying EC2 infra is all within the same AZ, then how can the task placement strategy place them across AZs?

the docs state

If there is no placement strategy defined, balance tasks across the Availability Zones in your cluster with the following logic

Sort the valid container instances, giving priority to instances that have the fewest number of running tasks for this service in their respective Availability Zone. For example, if zone A has one running service task and zones B and C each have zero, valid container instances in either zone B or C are considered optimal for placement.

Place the new service task on a valid container instance in an optimal Availability Zone (based on the previous steps), favoring container instances with the fewest number of running tasks for this service.

hey u/the_milkdromeda, that is a very good call out!
I brushed over the details here but hopefully I can update my posts or follow-up with more articles related to configuring using EC2 as there are some subtle differences.

I meant to say that it automatically spreads across AZ (assuming you have multiple AZ configured) using fargate launch types (I should really clarify there).

Either way, you are totally right! If you decide to run ECS workloads using EC2 launch types.

You need to ensure that they span multiple AZ (availability zones):

ECS groups available capacity used to place Tasks into ECS Clusters with ECS Tasks being launched into an ECS Cluster. An ECS Clusters configured to use EC2 will have EC2 Instances registered with it and each EC2 instance resides in a single Availability Zone. You should be ensuring that you have EC2 instances registered with your Cluster from multiple Availability Zones. You can read more about launching EC2 instances into ECS Clusters here.

source - Amazon ECS availability best practices (AWS article)

You can also configure the placement strategy as part of that - task placement strategy.

I hope that helps. Thanks for pointing that out!

Thank you!
Fortunately, I am not impacted as much but its rough out here for us but I believe we will make it through!

Thank you! I am glad I can help.

This is the type of content I wish I had when I was learning this stuff. So, I am just sharing it!

Definitely join my email list. I will also be putting out surveys to ensure my content is relevant to you and what you hope to learn or the area you’d like to grow in!

I’ve been working with ECS for a while, so a couple questions and maybe tips if you will indulge me:

As you mention, you’d use a NAT in each AZ to provide redundancy but I’d rather ask the question:

Why use a NAT with a private subnet at all? As long as you manage your sg group rules well enough, only allowing traffic from your ELB, you can ignore deploying a NAT with its associated cost (~35$/month each) altogether.

I’d understand the usage of it better if you required a fixed outgoing IP in an autoscaling cluster for whitelisting purposes.

Moving on to ECS itself. It’ll totally depend on your underlying application, but with this fixed target port config, you will be limiting the your ability to dynamically map ports to your tasks. Unless you plan on:

  • Running your service as a daemon
  • Only running on Fargate
  • Using awsvpc with ec2 (and so each running task will have an ENI) instead of bridge networking? Then I suppose you’re only concern would be the max network interfaces per instance.

I would assume the NAT would be useful if your service running needs to talk to anything on the internet.

I am not too familiar with that approach of exposing private resources to the internet without a NAT instance or gateway.

Its the AWS recommended way of doing this in their documentation.

Either way, do you have more information about that, I’d love to learn more.

For demonstration purposes, I am only show casing usage of AWS ECS on fargate.

However, yes, you are right, that is a very good call out with AWS ECS on EC2. We do lose the ability for dynamic port mapping with awsvpc network mode (with ENI).

There are definitely trade offs between the different network modes. The biggest one is statically mapped ports and ENI limit on EC2s. Then again, if you use dynamic ports, you are trading off security.

Like you said, it depends on the underlying application and how you will use it. It may be a good follow-up post.

Only if those services require a fixed IP. Every ENI is allocated a public IP address unless I’ve missed something.

Well, I guess you have to ask yourself the purpose of the NAT. If it’s just to obscure your instance and ports from any kind of network traffic from the outside, you can accomplish that by only allowing incoming traffic from the ELB as a security group ingress rule like you applied anyway.

The second reason is cost, and it’s also a reason I measure AWS docs against their propensity to end up charging you obscene amounts of money :money_mouth_face:

Each NAT is roughly $35/month. Multiply that by availability zones and environments, and it’s not negligeable. If I absolutely need a NAT, of course I use one in each AZ for production. For test environments, I’ll use a NAT instance running a micro or nano for a fraction of the cost.

About what I was saying before, awsvpc would work fine if you stay within the limits of the ec2 max Eni but you can’t overlook the overhead time that it would take to allocate an ENI and attach it to the instance before spinning up your scaled out task. Depends the architecture of the app and if you have multiple microservices exposed and running across a capacity provider; I doubt the max Eni would be sufficient in that case.

I haven’t tried it myself, but if a VPC-enabled lambda is any indication, it adds a good 20s to the spin up time.

Now as far as the security aspect, I’m not sure you have a hole. Consider: you have multiple microservices, each with defined rules on the ALB listener. Those rules are your first defense. If a rule is matched, it’s the target group that has the responsibility of matching the request to the dynamic port of one of the healthy hosts (your tasks). And since your EC2 is only allowing requests from the ALB, no other port other than the one attached to the TG by ECS is actually exposed.

I’ll be interested to hear your scaling config solution with the EC2s in a follow up. It’s been by far my biggest headache with ECS though I think I finally found a solution that I’m comfortable with.

With a private subnet you only get a private ip and not a public ip.

Yes. I’m questioning the usefulness, at least in this case, of using a private subnet at all.

For security purposes mainly. Things in the private subnet aren’t publicly accessible if an intentional or unintentional change occurs to your security groups for example. You can also set up NAT instances instead of a NAT gateway if you want to save some cost.

Very true and I definitely agree with you on the cost of AWS resources :frowning:

That’s an interesting approach to give private resources access internet.

However, how does one provide access to say pull a public docker image from docker hub ?

Let’s say I want to run a public nginx image in my AWS ECS, do I whitelist on ALB ? I am confused about that part.

I tried this and It took me ages to debug. This only works if I add NAT gateway into the subnet and connect it to my VPC.

Either way, those are very good points about overhead time. I haven’t really benchmarked that.

Thanks for the feedback. I will keep those points in mind!