Review the ECS task network mode

Choose an ECS task network mode that fits its connectivity needs.

Description

Fargate requires awsvpc. On EC2, choose it when you need a dedicated interface per task so that you can apply security groups directly to tasks.

Potential impact

With host or bridge, you cannot attach a security group directly to a task. With none, the task has no external network connection.

Remediation

For network_mode = "awsvpc", configure the service subnets and security groups and check the required ports and routes. Keep none on Linux EC2 tasks if the task needs no external connectivity.

Examples

The EC2 excerpts change only the network mode. Supply the omitted container definitions before deploying.

Before

hcl
resource "aws_ecs_task_definition" "example" {
  family       = "service"
  network_mode = "none"

  volume {
    name      = "service-storage"
    host_path = "/ecs/service-storage"
  }
}

After

hcl
resource "aws_ecs_task_definition" "example" {
  family       = "service"
  network_mode = "awsvpc"

  volume {
    name      = "service-storage"
    host_path = "/ecs/service-storage"
  }
}

References