Skip to content

ECS Networking with the Host using awsvpc mode

1

I have a backend application running as a docker container on ECS cluster using EC2 launch type and am using awsvpc network mode , There are somethings that i need clarification for

1- Am deploying the host EC2 instance in a public subnet given that i am using awsvpc network mode ,So a separate ENI is created in one of the subnets in the vpc and since am not specifying an explict subnet ,The default behaviour is to create the ENI in a private subnet , What i can't understand is the subnet in which the ecs task is deployed in doesn't have a route to the internet in the route table, However the application is responding and everything is working and it can pull ecr images , How is the ecs task is able to make it it to the internet ( my backend application is integrating with external APIs and it is pulling docker images from ECR without using privatelink)

Is it using the ec2 instance as a proxy to make api calls ?

My architecture is as follows

Client Requests -> Cloudfront distribution -> load balancer --> ecs target group

2 Answers
0

In your scenario with the awsvpc network mode, there are a few key points to understand about how networking works:

  1. When using awsvpc mode, each ECS task gets its own Elastic Network Interface (ENI) with a private IP address within the VPC. This ENI is separate from the EC2 instance's ENI.

  2. Even though the task's ENI is created in a private subnet without direct internet access, the task can still reach the internet through the EC2 instance it's running on, which is in a public subnet.

  3. The EC2 instance essentially acts as a NAT (Network Address Translation) gateway for the tasks running on it. This means that outbound internet traffic from the tasks is routed through the EC2 instance's network interface.

  4. For inbound traffic, your setup with CloudFront, load balancer, and ECS target group allows requests to reach your tasks even though they're in a private subnet.

  5. For outbound traffic (like pulling ECR images or making API calls), the tasks use the EC2 instance's internet gateway to access the internet. This is why your application can pull ECR images and integrate with external APIs without using PrivateLink.

So, to answer your specific question: Yes, the EC2 instance is effectively acting as a proxy for the ECS tasks to make outbound internet calls. The tasks aren't directly connected to the internet, but they can reach it through the EC2 instance they're running on.

This setup provides a good balance of security and functionality. Your tasks are protected in a private subnet, but they can still access the internet when needed through the EC2 instance. Meanwhile, inbound traffic is controlled and routed through your load balancer and target group setup.
Sources
AWSVPC mode - Amazon Elastic Container Service
Amazon ECS task networking options for the EC2 launch type - Amazon Elastic Container Service
Network security best practices for Amazon ECS - Amazon Elastic Container Service

answered 2 years ago

0

@Mahmoud. I am unsure if I am understanding it correctly. Can you please correct me? In first statement , its mentioned that

Am deploying the host EC2 instance in a public subnet given

Down below you have also mentioned

can't understand is the subnet in which the ecs task is deployed in doesn't have a route to the internet in the route table.

If I understand correctly a subnet is public if its associated with routing table with route having destination cidr 0.0.0.0/0 . so if a ec2 instance is in public subnet and ecs container is on the ec2 instance, then ecs container is in public subnet. But I agree that with `networkMode: awsvpc" separate eni is created . I had similar setup as yours, the issue i have is , my backend service get pulled from ghcr.io , but if I do a curl command from inside my container, its doesn't work. I have verified that I am able to perform the same curl command from ec2 instance. Any thoughts?

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.