Sub topic 7
Networking tasks with awsvpc
This chapter assumes you know VPC basics (subnets, route tables, security groups) and have read the chapter on task definitions. It covers how Fargate tasks get network access and the errors that follow from getting it wrong.
One network interface per task
Fargate supports only the awsvpc network mode. Each task gets its own elastic network interface (ENI) in a subnet you choose. The task has its own private IP address and its own security groups, just like an EC2 instance would.
Two consequences follow:
- Containers in the same task share one network namespace. They reach each other on
localhost. - Different tasks, even on the same underlying host, talk to each other over their ENIs. Security groups control that traffic.
Where the network settings live
The task definition only sets "networkMode": "awsvpc". The subnets, security groups, and public IP setting belong to the service (or to run-task), as you saw in the create-service command:
--network-configuration "awsvpcConfiguration={subnets=[subnet-aaa111,subnet-bbb222],securityGroups=[sg-0123456789abcdef0],assignPublicIp=DISABLED}"Choose subnets in at least two Availability Zones so the service survives the loss of one zone.
Security groups
Reference security groups instead of IP ranges. A common layout uses two groups:
| Security group | Inbound rule | Source |
|---|---|---|
| `alb-sg` | TCP 443 | `0.0.0.0/0` |
| `task-sg` | TCP 8080 | `alb-sg` |
The task group accepts traffic only from the load balancer. Tasks scale in and out, and their IPs change, but the rule keeps working. The port is the container port from portMappings.
Public or private subnets
Place tasks in private subnets and set assignPublicIp=DISABLED. The load balancer sits in public subnets and is the only internet-facing component.
A task still needs outbound access at startup. Fargate must pull the image from ECR, fetch secrets, and send logs. You have two options:
- A NAT gateway in a public subnet, with a route from the private subnets. It is simple but has an hourly and per-GB charge.
- VPC endpoints for ECR (
ecr.apiandecr.dkr), an S3 gateway endpoint (image layers are stored in S3), CloudWatch Logs, and Secrets Manager if you use it. Traffic stays inside AWS.
If you use public subnets with assignPublicIp=ENABLED, the task gets a public IP and routes through an internet gateway. That works for experiments, but it exposes the task to the internet unless its security group blocks it.
Subnet capacity
Each running task uses one IP address from its subnet. During a rolling deployment old and new tasks run together, so the subnet must have spare addresses. A very small subnet such as a /28 can run out during scale-out and block deployments.
Exercise
A service in private subnets has no NAT gateway. Tasks fail with a timeout while pulling the image from ECR. Name two kinds of VPC endpoint you must add, and say which one carries image layers.
Show answer
Add the ECR interface endpoints (ecr.api and ecr.dkr) and an S3 gateway endpoint. Image layers are stored in S3, so the S3 gateway endpoint carries them. If tasks also use CloudWatch Logs or Secrets Manager, add endpoints for those as well.