Lesson 4 of 6 Amazon ECS, EKS and Fargate

Networking Containers for Security, Load Balancing and Discovery

This lesson explains how the way tasks connect to the network decides how they are secured, load balanced and discovered by other services.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose a task network mode.
  • Describe service discovery and private access for tasks.
  • Identify the VPC endpoints that private tasks need.

Network modes

An ECS task definition's network mode decides how its containers connect to the network. In awsvpc mode, which Fargate requires, each task receives its own elastic network interface, private IP address and security group, exactly like an instance. Security can therefore be set per task, and a load balancer registers tasks by IP address.

In bridge mode, available on EC2, containers share the host's network through Docker's virtual network, and dynamic port mapping assigns each container a different host port, so many copies of a task can run on one instance behind an ALB. In host mode, containers use the host's network directly, which is fast, but only one task can use a given port on each host. The following table summarises the modes.

ModeBehaviour
awsvpc (required for Fargate)Each task gets its own ENI, private IP and security group; the ALB uses IP targets
bridgeContainers share the host's network through Docker; dynamic port mapping lets many tasks share an EC2 host behind an ALB
hostContainers use the host's network directly; one task per port per host

With awsvpc, every task consumes an IP address, so subnets must be sized for the maximum task count.

Service discovery and private access

Containerised services must find one another as tasks start and stop with changing addresses. ECS Service Connect lets services call each other by name, and adds load balancing, retries and traffic metrics between services.

Tasks in private subnets need a path to pull images: through a NAT gateway, or more securely and cheaply through VPC endpoints for Amazon ECR and an S3 gateway endpoint, because image layers are stored in S3. In awsvpc mode, each task has its own security group, which allows tier-to-tier rules, such as permitting only the front-end service to reach the API service.

The following diagram shows the private path for image pulls.

How tasks in private subnets pull images without a NAT gateway. Interface VPC endpoints for the Amazon ECR API and Docker registry carry the registry calls, and an S3 gateway endpoint carries the image layers, which ECR stores in S3. Tasks that write logs also need a CloudWatch Logs endpoint.
Without the S3 gateway endpoint, the registry calls succeed but the layer downloads fail.
True or false

Container networking

Exam skills 2.1.4 · 1.2.1

Check how containers find each other and reach AWS privately.

Log in to try this activity Interactive activities are free with an account.
Worked example

Private tasks that cannot pull images

ECS tasks on Fargate in private subnets with no NAT gateway fail to start because they cannot pull images from ECR.

EndpointWhy it is needed
ECR API interface endpointAuthentication and image metadata
ECR Docker interface endpointImage manifest requests
S3 gateway endpointImage layers are stored in S3
CloudWatch Logs interface endpointThe awslogs driver

With these endpoints, tasks run with no internet access at all, and each task's security group controls exactly what it may reach.

Check yourself

Which network mode gives each ECS task its own elastic network interface and security groups?

Show answer

The awsvpc network mode, which Fargate requires.

Exam Tips

Remember for the exam

  • To give each task its own security group, use awsvpc networking.
  • When private tasks cannot pull images, add ECR api and dkr interface endpoints and an S3 gateway endpoint.
  • For service-to-service discovery by name, use ECS Service Connect.
  • With awsvpc networking, size subnets for one IP address per task.