Introduction
On your laptop, container networking feels simple. You run docker run -p 3000:3000 my-app, open localhost:3000, and it works. Docker creates a bridge network, gives the container an internal IP, and maps a port on your machine to a port in the container. You rarely need to think about it.
Then you move the same image to Amazon ECS on AWS Fargate, and the questions start. There is no host you can SSH into. There is no -p flag. The task shows a private IP from your VPC, the load balancer says the target is unhealthy, and you are not sure whether the problem is the subnet, the security group, the route table, or the application itself.
Fargate networking is interesting because each task behaves less like a process on a server and more like a small machine plugged directly into your VPC. Once you understand that idea, most networking problems become much easier to reason about. This article walks through what happens, from a networking point of view, when a Fargate task starts, and how to troubleshoot it when something does not connect.
A quick introduction to ECS and Fargate
Amazon ECS is a container orchestrator. You describe your application in a task definition (image, CPU, memory, ports, environment variables, logging), and ECS takes care of running it. If you want a fixed number of copies running behind a load balancer, you create an ECS service, and ECS keeps that number of tasks alive.
ECS still needs somewhere to run those containers. With the EC2 launch type, that somewhere is a fleet of EC2 instances you manage yourself. You choose instance types, patch the AMI, install or update the ECS agent, and make sure there is enough capacity for new tasks.
AWS Fargate removes that layer. You ask for a task with, say, 0.5 vCPU and 1 GB of memory, and Fargate provides the compute for it. You never see the underlying server. That is convenient, but it also means you cannot debug networking from the host. You need to understand the pieces Fargate creates in your VPC, because that is where your control and your troubleshooting live.
What happens when an ECS Fargate task starts?
Let's follow a single task from request to traffic. The exact internals belong to AWS, but the steps that affect your network are visible and well documented.
-
ECS receives a request to start a task. This can come from
aws ecs run-task, or from an ECS service that needs one more task to reach its desired count. The request includes a network configuration: which subnets to use, which security groups to attach, and whether to assign a public IP. -
Fargate provisions compute for the task. While this happens, the task's
lastStatusshowsPROVISIONING. - An Elastic Network Interface (ENI) is created and attached. Fargate creates one ENI for the task in one of the subnets you listed. This is the task ENI. It is fully managed by Fargate, so you cannot detach or modify it yourself. Stopping the task releases it.
- The ENI gets a private IP from your subnet. The address comes from the subnet's CIDR range, just like an EC2 instance launched there. If the subnet has no free IPs, the task cannot start.
- Security groups are applied to the ENI. The groups you specified decide what traffic can reach the task and what it can send out.
-
The image is pulled and containers start. The task moves to
PENDING. On platform version 1.4.0 and later, the image pull, ECR authentication and log delivery all travel through the task ENI, so your subnet's routing must allow it. Then the containers start and the task becomesRUNNING. - The application becomes reachable. Your app listens on its container port, for example 3000, on the task's private IP. If the service is attached to a load balancer target group, ECS registers that IP and port as a target. Traffic can now flow, subject to security groups and health checks. The main point: before your code even starts, a network interface already exists in your VPC, with an address and firewall rules. Many "my container is broken" problems actually happen in steps 3 to 6.
Understanding the awsvpc network mode
The behaviour above comes from the awsvpc network mode. In a task definition, you set it like this:
{
"family": "web-api",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"containerDefinitions": [
{
"name": "api",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-api:1.0.0",
"portMappings": [{ "containerPort": 3000, "protocol": "tcp" }],
"essential": true
}
]
}
With awsvpc, every task gets its own ENI and its own private IP in the VPC. Fargate requires this mode; you cannot choose bridge or host networking there. Notice that there is no hostPort to think about. With awsvpc, the host port and container port are the same, so port 3000 in the container is port 3000 on the task's IP.
Compare that with Docker's default bridge network. On a single machine, containers sit on a private virtual network inside the host. The outside world only sees the host's IP, and you publish ports to avoid collisions: one container on host port 8080, another on 8081, both pointing at container port 80.
A simple analogy: bridge networking is like an office building with one public phone number and a receptionist forwarding calls to extensions. awsvpc gives every employee their own direct line. You can call them directly, and you can decide, person by person, who is allowed to call. That last part matters, because security groups are attached to each task's ENI, not to a shared host. Containers inside the same task share the ENI, so they can reach each other on localhost.
Understanding public and private subnets
A subnet is "public" or "private" because of its route table, not because of a checkbox. A public subnet has a route 0.0.0.0/0 → Internet Gateway. A private subnet does not.
You can run Fargate tasks in a public subnet with assignPublicIp=ENABLED. The task ENI gets a public IP, and it can reach the internet through the Internet Gateway. This is fine for quick experiments. Two details catch people out. Without a public IP, a task in a public subnet cannot reach the internet, so image pulls fail. And with a public IP, the only thing standing between your app and the whole internet is the security group.
For application workloads, the common pattern is to put the load balancer in public subnets and the tasks in private subnets. Users reach the ALB; only the ALB reaches the tasks. The tasks have no public address at all.
It helps to separate two directions of traffic:
- Inbound (internet to your app): handled by the ALB in the public subnet, which uses the Internet Gateway. Tasks in private subnets never need to be directly reachable.
-
Outbound (your task to the outside): pulling images from ECR, sending logs to CloudWatch, reading secrets, calling a payment API. A private subnet needs a path for this.
There are two common outbound options, and they solve different problems. A NAT Gateway in a public subnet lets private tasks start connections to the internet while staying unreachable from it. It is the right choice if your app calls third-party APIs or public package registries. VPC endpoints let tasks reach specific AWS services privately, without internet access. For pulling from ECR on platform version 1.4.0 or later, you need the
ecr.apiandecr.dkrinterface endpoints plus an S3 gateway endpoint, because image layers are stored in S3. Add alogsendpoint for CloudWatch Logs and a Secrets Manager or SSM endpoint if you use them.
So NAT is not mandatory for every private task. A task that only talks to AWS services can work with endpoints alone. A task that calls the public internet needs NAT (or another egress path).
flowchart TB
user([Internet users]) --> igw[Internet Gateway]
subgraph vpc["VPC 10.0.0.0/16"]
subgraph pub["Public subnet 10.0.1.0/24"]
alb["Application Load Balancer<br/>sg-alb: allow 443 from 0.0.0.0/0"]
nat[NAT Gateway]
end
subgraph priv["Private subnet 10.0.2.0/24"]
subgraph task["ECS Fargate task"]
eni["Task ENI<br/>private IP 10.0.2.15<br/>sg-app: allow 3000 from sg-alb"]
app["Container: Node.js app<br/>listening on 0.0.0.0:3000"]
eni --- app
end
end
vpce["VPC endpoints (optional)<br/>ecr.api, ecr.dkr, S3, logs"]
end
igw --> alb
alb -- "HTTP to 10.0.2.15:3000" --> eni
eni -- "outbound to internet APIs" --> nat
nat --> igw
eni -. "private access to AWS services" .-> vpce
How security groups affect container communication
Because security groups attach to the task ENI, each service can have its own firewall. Security groups are stateful: if inbound traffic is allowed, the reply is allowed back automatically, and the reverse is true for outbound connections the task starts.
A tempting shortcut is to allow port 3000 from 0.0.0.0/0 on the task's security group. Don't. If the task ever gets a public IP, the app is exposed directly, bypassing the ALB, its TLS and any rules you put there. Inside the VPC, it also lets any workload talk to your app.
A better approach is to reference the ALB's security group as the source. The rule then means "anything that carries the ALB's security group may connect on port 3000", and nothing else:
# Task security group: allow port 3000 only from the ALB security group
aws ec2 authorize-security-group-ingress \
--group-id sg-0app1234567890abc \
--ip-permissions 'IpProtocol=tcp,FromPort=3000,ToPort=3000,UserIdGroupPairs=[{GroupId=sg-0alb1234567890abc}]'
sg-0app… is attached to the tasks, and sg-0alb… to the load balancer. Since task IPs change on every deployment, referencing a security group is much easier than maintaining IP lists. Also check the ALB security group's outbound rules: if someone has restricted them, they must still allow traffic to the tasks on port 3000.
For outbound traffic from tasks, the default "allow all" is common. If you tighten it, remember that the task still needs HTTPS (443) to ECR, CloudWatch Logs and any endpoints it uses.
Following a request to the application
Now let's trace one request: Internet → Application Load Balancer → Fargate task ENI → container.
A user opens https://api.example.com. DNS resolves to the ALB, which has nodes in your public subnets. The request arrives through the Internet Gateway, and the ALB's security group allows port 443. The ALB terminates TLS and checks its listener rules to pick a target group.
For Fargate, that target group must use target type ip, because tasks are registered by their private IP, not by an instance ID. ECS registers each new task as something like 10.0.2.15:3000 and deregisters it when the task stops.
The ALB opens a new connection from its own private address to 10.0.2.15:3000. Both sit in the same VPC, so the local route handles it with no NAT involved. The task's security group checks the source, sees the ALB's security group, and allows it. The packet arrives at the ENI and is delivered to the process listening on port 3000. The response goes back the same way, allowed automatically because security groups are stateful.
The ALB only sends traffic to targets that pass its health check. Until your app answers the health check path (for example /health) with a success code, the target stays out of rotation, even if the container is running.
Common networking problems and troubleshooting
These are the failures you are most likely to run into, roughly in the order I would check them.
The task starts, but the app is unreachable. Often the app is listening on 127.0.0.1. That works on your laptop, but on Fargate the ALB connects to the ENI's private IP, and a localhost-only process never sees the request. In Node.js, app.listen(3000, '0.0.0.0') fixes it. Also confirm that containerPort in the task definition matches the port the app actually uses.
Health checks keep failing. Check that the target group's port and health check path exist and return the expected code (200 by default). Then check that the task's security group allows the ALB's security group on that port. A slow-starting app may also need a health check grace period on the service.
The task never reaches RUNNING, or the container cannot reach external APIs. Look at stoppedReason. Messages like CannotPullContainerError or timeouts usually mean there is no outbound path: a private subnet with no NAT Gateway and no ECR endpoints, or a public subnet without assignPublicIp=ENABLED. If ECR works but calls to a third-party API time out, you probably have endpoints but no NAT.
Wrong subnet or route table. A subnet you think is private might route to the Internet Gateway, or the NAT route might point to a gateway in the wrong place. Also check free IPs: a small subnet can run out during a deployment, when old and new tasks run at the same time.
Practical troubleshooting checklist
Start with the task itself. This shows its status, why it stopped, and the ENI details:
aws ecs describe-tasks \
--cluster my-cluster \
--tasks <task-id> \
--query 'tasks[0].{status:lastStatus,stoppedReason:stoppedReason,eni:attachments[0].details}'
The eni output includes networkInterfaceId, subnetId and privateIPv4Address. Use the ENI ID to see which security groups are really attached:
aws ec2 describe-network-interfaces \
--network-interface-ids eni-0abc1234567890def \
--query 'NetworkInterfaces[0].{ip:PrivateIpAddress,subnet:SubnetId,groups:Groups}'
Check the subnet's free IPs and its routes:
aws ec2 describe-subnets \
--subnet-ids subnet-0abc1234567890def \
--query 'Subnets[0].{az:AvailabilityZone,freeIps:AvailableIpAddressCount,vpc:VpcId}'
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0abc1234567890def \
--query 'RouteTables[0].Routes'
If the route table query returns nothing, the subnet uses the VPC's main route table; check that one instead. Look for 0.0.0.0/0 pointing to nat-… (private) or igw-… (public).
Inspect the security group rules, and ask the ALB what it thinks of each target:
aws ec2 describe-security-groups \
--group-ids sg-0app1234567890abc \
--query 'SecurityGroups[0].IpPermissions'
aws elbv2 describe-target-health \
--target-group-arn <target-group-arn>
describe-target-health returns a state and a reason code, such as Target.Timeout (often a security group or listen-address problem) or Target.ResponseCodeMismatch (the app answered, but with the wrong status code).
Conclusion
Fargate hides the servers, but it does not hide the network. Every task is a real participant in your VPC, with its own interface, its own IP and its own security groups. When something fails, that model gives you a clear path: did the ENI get created in the right subnet, can that subnet route where the task needs to go, do the security groups allow the traffic, and is the app actually listening on the right address and port?
Thinking this way also improves your designs. You start placing load balancers and tasks in different subnets on purpose, you choose between NAT and VPC endpoints based on what the app really calls, and you write security group rules that describe relationships between services instead of open ports.
Key takeaways
- Fargate requires
awsvpc: each task gets its own ENI and a private IP from your subnet. - The ENI and its security groups exist before your container starts, so many "app" failures are network failures.
- Put the ALB in public subnets and tasks in private subnets, and allow the app port only from the ALB's security group.
- Private tasks need an outbound path: VPC endpoints for AWS services, a NAT Gateway for the public internet.
- Use target type
ip, and make your app listen on0.0.0.0.
Check More blogs at https://khanalsamir.com/blogs

Top comments (0)