ECS service assigned public IP addresses

Do not assign public IP addresses to ECS tasks that do not need direct public access.

Description

Check whether Fargate tasks need direct internet connections. A public IPv4 address allows inbound traffic only when the subnet route and security groups permit it.

Potential impact

A direct connection can reach a task without going through the service’s load balancer.

Remediation

For private tasks, set assign_public_ip = false and use private subnets. Route public requests through an ALB or NLB. Provide NAT or supported VPC endpoints for outbound traffic such as image pulls.

Examples

Both excerpts change address assignment on the same service. Use actual subnet and security group IDs and check connectivity on the newly deployed tasks.

Before

hcl
resource "aws_ecs_service" "example" {
  name            = "example_service_dev"
  cluster         = aws_ecs_cluster.example_cluster.id
  task_definition = aws_ecs_task_definition.example_task.arn
  desired_count   = 2
  launch_type     = "FARGATE"

  network_configuration {
    assign_public_ip = true
    subnets          = ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"]
    security_groups  = ["sg-0123456789abcdef0"]
  }
}

After

hcl
resource "aws_ecs_service" "example" {
  name            = "example_service_dev"
  cluster         = aws_ecs_cluster.example_cluster.id
  task_definition = aws_ecs_task_definition.example_task.arn
  desired_count   = 2
  launch_type     = "FARGATE"

  network_configuration {
    assign_public_ip = false
    subnets          = ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"]
    security_groups  = ["sg-0123456789abcdef0"]
  }
}

References