ECS task in internal subnet cannot connect to the internet

Configured ECS service to have only internal subnets as allowed subnets because this service cannot be accessed from the internet but is should be able to access internet.

However, the issue is that the service cannot access internet anymore but it can access other services in the same VPC.

Here is the terraform configuration for the VPC and ECS service - https://gist.github.com/genert/7ae99dbff9c7c1ba64edc08694fe460c

As you can see, the route table configuration should make this work but it does not.

Any ideas what the issue could be?

I would also point out that the instance itself where the ECS task is running on is in an external subnet and the other tasks that do not have “subnet allowed” configured can access internet without any problems so security groups are fine.

Your external subnet is the only one with a route to the internet. Your internal subnets is missing a route table with a route for 0.0.0.0/0 that goes via the NAT Gateway.

I am not sure of the implementation of “aws_subnet.internal”, but most of the time internal subnet refer to a subnet with no inbound and no outbound connectivity.

What you are probably looking for is an private subnet. This has no inbound connectivity, but does have outbound connectivity via a NAT gateway.

(For completeness, there is also public subnet with both inbound and outbound via the internet gateway.)

I’m on my phone, so apologies if I’ve missed it, but it appears is if you haven’t created a default route pointing to your nat gateway on the internal subnet(s):

You’ve properly created the nat gateway in a public subnet pointing to an internet gateway, but the private subnets have no way of using it without having the private route table pointing to the nat gateway as it’s default route.

Strictly speaking, the only difference between a public and private (internal) subnet is how you configure their routes. It sounds like there are potentially a few things wrong with the current config:

  • No 0.0.0.0/0 route on the private subnet’s routing table pointed to a NAT gateway.

  • Restrictive network ACLS on the private subnet’s which don’t allow outbound ephemeral ports

  • VPC security group has no outbound rule (not created by default, in terraform!) Thereby blocking outbound internet access.

When stuff goes wonky like this, I look at the networking components in the console to try and track things down. Best of luck!

edit: looked at gist:

I think on line 115, you want to reference your private/internal subnets instead of external. You probably want to add a dedicated route table with references to the nat gateway with 0.0.0.0/0 too, and associate that with private subnets.

Added NAT gateway route to VPC’s main route table and everything works now. Thanks!

Good point, I will rename external to public and internal to private, makes it more understandable.

BTW, what kind of services would exist in internal subnet at first place then? Some kind of batch jobs?

Interesting observation, what differentiates between subnets is indeed their route configuration.

BTW, what do you mean by “Restrictive network ACLS on the private subnet’s which don’t allow outbound ephemeral ports”? I checked the ACLs, somehow they were already configured to allow “0.0.0.0/0” and “::/0”…

Sure. In the end they’re just names :slight_smile:

An internal or isolated subnet might indeed be used by batch jobs, rds databases, or elasticache clusters.

And if your containers don’t have a need to connect to external APIs you could also use it for ECS+Fargate. (For AWS APIs you could use vpc endpoints)

So your NACLs allow 0.0.0.0/0 outbound for all protocols? Probably not the issue. It’s likely just the nat gateway route.