Sub topic 8
Networking tasks with awsvpc
This chapter covers how Fargate tasks get network access through the awsvpc network mode. It assumes you know VPCs, subnets, route tables, and security groups at a basic level. It also assumes you have read the earlier chapters on task definitions and Fargate. By the end you should be able to place tasks in the right subnets, write security groups that match how traffic really flows, and diagnose the most common connectivity failures.
What awsvpc means
In awsvpc mode, every task gets its own elastic network interface (ENI). The ENI has a private IPv4 address from the subnet you choose, and it carries the security groups you attach. The task behaves like a small EC2 instance in your VPC. Containers inside the same task share that ENI, so they reach each other on localhost, and two containers in one task cannot bind the same port.
Fargate supports only awsvpc. You set it in the task definition, and you supply the subnets and security groups later, when you create the service or run the task:
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc"
}Because each task has its own IP address and security group, you can control traffic per task. You do not need to map host ports or manage dynamic port ranges, because the container port is the port the task listens on.
Supplying the network configuration
The network configuration belongs to the service (or to a one-off run-task call), not to the task definition. It has three parts:
subnets: where the ENIs are created. List subnets in at least two Availability Zones for a service, so ECS can spread tasks and survive a zone failure.securityGroups: up to five security groups attached to each task ENI.assignPublicIp:ENABLEDorDISABLED. It controls whether the ENI gets a public IPv4 address.
aws ecs create-service \
--cluster demo \
--service-name web \
--task-definition web-app \
--desired-count 2 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-aaa111,subnet-bbb222],securityGroups=[sg-0123456789abcdef0],assignPublicIp=DISABLED}"Each running task uses one IP address from its subnet. A subnet that is too small runs out of addresses during scale-out or during a deployment, when old and new tasks run side by side. Size subnets for your peak task count plus the extra tasks a rolling update adds.
Public versus private subnets
A subnet is public when its route table sends 0.0.0.0/0 to an internet gateway. It is private when it does not. A task's reachability depends on both the subnet type and the assignPublicIp setting.
| Placement | assignPublicIp | Route for outbound traffic | Typical use |
|---|---|---|---|
| Public subnet | ENABLED | Internet gateway, using the task's public IP | Simple demos, tasks with no NAT |
| Public subnet | DISABLED | No working internet path | Rarely useful |
| Private subnet | DISABLED | NAT gateway or VPC endpoints | Production services behind a load balancer |
For production web services, the usual design puts the Application Load Balancer in public subnets and the tasks in private subnets. Internet clients reach only the load balancer, and the tasks accept traffic only from it.
How a task reaches what it needs
A task must reach several AWS services before and while it runs. Fargate itself uses the task execution role to pull the image from Amazon ECR, fetch secrets, and send logs to CloudWatch Logs. These calls use the task's ENI, so the task needs a network path to each service. Your application code may also call AWS APIs, such as S3, using the task role.
In a private subnet you have two ways to provide that path:
- NAT gateway. One route (
0.0.0.0/0to the NAT gateway) gives access to every endpoint, including the internet. It is simple, but you pay per hour and per gigabyte processed. - VPC endpoints. Private connections to specific services. They avoid NAT data charges for that traffic and keep it off the internet.
For an all-private setup that pulls from ECR, you generally need these endpoints:
com.amazonaws.<region>.ecr.api(interface endpoint)com.amazonaws.<region>.ecr.dkr(interface endpoint)com.amazonaws.<region>.s3(gateway endpoint), because ECR stores image layers in S3com.amazonaws.<region>.logs(interface endpoint), if you use theawslogsdrivercom.amazonaws.<region>.secretsmanager(interface endpoint), if you inject secrets
Interface endpoints have their own security groups. The endpoint's security group must allow inbound HTTPS (port 443) from the task's security group, or the pull will time out.
The diagram below shows the typical production layout.
Security groups: chaining instead of CIDR ranges
Because each task has its own security group, you can reference one security group from another instead of using IP ranges. Task IP addresses change every time a task is replaced, so CIDR rules for tasks go stale quickly. Group references do not.
A common pair looks like this:
- ALB security group: inbound 80 and 443 from
0.0.0.0/0. Outbound is open by default. - Task security group: inbound 8080 from the ALB security group only. Outbound is open by default, or restricted to what the app needs.
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 8080 \
--source-group sg-0aaaabbbbccccddddHere sg-0123456789abcdef0 is the task group and sg-0aaaabbbbccccdddd is the ALB group. The same pattern works between services: if service A calls service B on port 9000, allow 9000 on B's group from A's group.
Security groups are stateful. If inbound traffic is allowed, the response is allowed automatically. You only need an outbound rule for connections the task starts.
Edge cases and limits
- Changing networking needs a new deployment. You can change a service's subnets and security groups with
update-service --network-configuration, which starts a new deployment. Running tasks keep their old ENI settings until they are replaced. - Startup is slower than you may expect. Fargate creates and attaches the ENI before the containers start, which is part of why a task spends time in
PROVISIONING. - IPv6. Tasks can be dual-stack in subnets that have IPv6 CIDR blocks, if your account and region have the dual-stack setting enabled. Check the current ECS documentation before relying on it.
- Service-to-service discovery. Task IPs change, so do not hard-code them. Use a load balancer, Service Connect, or AWS Cloud Map to give callers a stable name.
- ENI visibility. Fargate ENIs appear in your account's EC2 console with a description like
arn:aws:ecs:.... You cannot detach or modify them yourself, since ECS manages them.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Task stops with `CannotPullContainerError` and a timeout | No route to ECR: private subnet with no NAT or endpoints, or `assignPublicIp` disabled in a public subnet | Add a NAT gateway or the ECR and S3 endpoints, or enable a public IP in a public subnet |
| Pull works but task fails to start with a secrets or logs error | Missing `secretsmanager` or `logs` endpoint, or endpoint security group blocks 443 | Add the endpoint and allow 443 from the task group |
| Targets show unhealthy in the target group | Task security group does not allow the container port from the ALB group | Add an inbound rule on the container port from the ALB group |
| Service cannot place tasks, with an error about no available IP addresses | Subnet is exhausted | Add larger or additional subnets |
| App cannot call the internet but ECR pulls succeed | Endpoints exist but there is no NAT route | Add a NAT gateway route for general outbound traffic |
Exercise
Your service runs in two private subnets with no NAT gateway. Tasks stop with a CannotPullContainerError that mentions a timeout while pulling from ECR. Logs go to CloudWatch Logs using awslogs. Which VPC endpoints do you create, and what must their security groups allow?
Show answer
Create interface endpoints for ecr.api, ecr.dkr, and logs, plus a gateway endpoint for S3, because ECR serves image layers from S3. Update the private subnets' route tables to include the S3 gateway endpoint. Each interface endpoint's security group must allow inbound TCP 443 from the tasks' security group. If you also inject secrets from Secrets Manager, add a secretsmanager interface endpoint.
Exercise
A service's targets stay unhealthy, even though the container responds on port 8080 when you test it from inside the task. The task security group allows inbound 8080 from 0.0.0.0/0... but the ALB is in the same VPC and the target group health check uses port 8081. What is wrong, and how do you fix it?
Show answer
The health check port (8081) is not allowed by the task security group, or the container is not listening on it. The security group must allow the health check port from the ALB's security group. Either change the target group health check to use the traffic port (8080), or open 8081 from the ALB group and make sure the container listens there. Restricting the source to the ALB security group instead of 0.0.0.0/0 is also better practice.