AWS ECS service role permissions need review

Grant only the permissions ECS needs to manage the load balancer.

Description

ECS uses iam_role to manage the service’s load balancer. Review the application’s task role and the task execution role used for image pulls separately.

Potential impact

Excess permissions can let the service role change resources beyond its load-balancing duties.

Remediation

With awsvpc, omit iam_role and use the ECS service-linked role. Where an explicit role is allowed, check its trust policy and allow only the actions and resources it needs. Verify target registration and service health after deployment.

Examples

The excerpts assume a load-balanced service using a mode other than awsvpc. Supply the cluster, task definition, load_balancer block and role policies.

Before

hcl
resource "aws_ecs_service" "ecs_service" {
  name            = "mongodb"
  cluster         = aws_ecs_cluster.foo.id
  task_definition = aws_ecs_task_definition.mongo.arn
  desired_count   = 3
  iam_role        = "admin"
}

After

hcl
resource "aws_ecs_service" "ecs_service" {
  name            = "mongodb"
  cluster         = aws_ecs_cluster.foo.id
  task_definition = aws_ecs_task_definition.mongo.arn
  desired_count   = 3
  iam_role        = aws_iam_role.foo.arn
}

The role reference changes from admin to aws_iam_role.foo.arn. Compare their policies to see whether the permissions change. Changing the role replaces the service, so inspect the Terraform plan. You can narrow the existing role’s policy in place. To run its replacement alongside mongodb, use a different service name.

References