What a Fargate service needs from its network on AWS, from public and private subnets to the NAT gateway, route tables and security groups, what the Terraform for it looks like, and how to get the same layout from an app declared in code.

A Fargate service has no instances to place, but every task still gets a network interface in a subnet of your VPC, and that subnet decides whether it can pull its image, reach a database and be reached by a load balancer.
The network is easy to get wrong in both directions, with tasks that have no route to ECR or tasks in public subnets behind a loose security group. This guide lists what it needs and then gets it from code.
Private subnets have no route to the internet gateway, so the NAT gateway docs describe it as the path for outbound traffic from tasks, and the Fargate networking docs explain that every task needs a route to reach ECR for its image. The NAT gateway is billed at $0.045 per hour plus $0.045 per GB processed in the example on the VPC pricing page, checked November 26, 2026, which is about $33 a month for one gateway before traffic.
This is the network only, and it still runs to dozens of lines before the cluster, the task definition, the service, the load balancer and the IAM roles are added:
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
}
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index + 10)
availability_zone = data.aws_availability_zones.available.names[count.index]
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}
resource "aws_eip" "nat" {
domain = "vpc"
}
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id
depends_on = [aws_internet_gateway.main]
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}
}
resource "aws_route_table_association" "public" {
count = 2
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private" {
count = 2
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private.id
}
resource "aws_security_group" "alb" {
name = "alb"
vpc_id = aws_vpc.main.id
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "tasks" {
name = "tasks"
vpc_id = aws_vpc.main.id
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
The single NAT gateway keeps the cost down and is also a single point of failure for outbound traffic from the other zone, and one NAT gateway per zone doubles the bill. Databases add a third subnet tier, a subnet group and another security group that only accepts the task security group, and a module such as terraform-aws-vpc packages the same pieces behind variables.
The console wizard for creating a VPC with public and private subnets and a NAT gateway produces the same resources in a few clicks, and the AWS CLI can script it. Both leave you with a network that exists only in an account, so a second environment means repeating the steps and a security group edit made in the console is invisible to code review. Terraform, OpenTofu and CloudFormation fix that by writing the network down, at the price of keeping it in step with the services that run in it.
Encore brings infrastructure from code, so the network is derived from the services and databases an application declares instead of being written separately. Per the AWS infrastructure docs, Encore creates an isolated VPC for each environment across two Availability Zones, with a public subnet holding the Application Load Balancer, an internet gateway and a NAT gateway, a compute subnet where Fargate tasks run and accept traffic only from the load balancer, and a storage subnet for databases and caches that is provisioned when the application declares one.

Run in your terminal to get started locally.
A service with a database is enough to produce all three subnet tiers. The todos folder also needs an encore.service.ts that exports new Service("todos"):
// todos/todos.ts
import { api, APIError } from "encore.dev/api";
import { SQLDatabase } from "encore.dev/storage/sqldb";
// Declaring a database is what adds the storage subnet tier on AWS.
const db = new SQLDatabase("todos", { migrations: "./migrations" });
export const add = api(
{ expose: true, method: "POST", path: "/todos" },
async ({ title }: { title: string }): Promise<{ id: number }> => {
if (!title.trim()) throw APIError.invalidArgument("title is required");
const row = await db.queryRow<{ id: number }>`
INSERT INTO todos (title) VALUES (${title.trim()}) RETURNING id`;
return { id: row!.id };
},
);
export const list = api(
{ expose: true, method: "GET", path: "/todos" },
async (): Promise<{ todos: { id: number; title: string }[] }> => {
const rows = await db.query<{ id: number; title: string }>`
SELECT id, title FROM todos ORDER BY id DESC LIMIT 100`;
const todos: { id: number; title: string }[] = [];
for await (const r of rows) todos.push(r);
return { todos };
},
);
-- todos/migrations/1_create_todos.up.sql
CREATE TABLE todos (
id BIGSERIAL PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
encore run starts Postgres locally, so the service is tested before any network exists. To deploy, connect your AWS account as described in Connect your cloud account, create an environment and push, and Encore builds the image, stores it in ECR and runs the service on Fargate in the compute subnet with the database in the storage subnet. By default Encore reserves a random /16 block in a private range, and if the network has to be peered with an existing one, the network configuration docs show how to choose the IP range when you create the environment. An AI coding agent that adds a second service or a cache only changes code, and the network follows the same declarations without an edit to route tables or security groups.
Best fit: Teams that want a VPC with public, compute and storage tiers for Fargate in their own AWS account, derived from the services and databases in the code.
Tasks in private subnets need one, or VPC endpoints, to pull images from ECR and reach the internet, because private subnets have no route to an internet gateway. A NAT gateway costs $0.045 an hour plus $0.045 per GB processed in the US East example on the AWS pricing page, which is roughly $33 a month before any traffic.
Private subnets for anything with a database or secrets, with the load balancer in the public subnets as the only entry point. Public subnets with public IPs avoid the NAT cost but expose the tasks directly, so each security group has to be exactly right.
At least two, because an Application Load Balancer requires subnets in two Availability Zones and a service spread across both keeps running if one zone fails. Each zone then needs its own public and private subnet.
With Encore you connect your AWS account and deploy the application, and Encore creates a VPC for each environment that spans two Availability Zones, with public, compute and storage subnets, an internet gateway, a NAT gateway and an Application Load Balancer. The network is derived from the services and databases your code declares.
Deploy to your cloud on AWS and Google Cloud without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.