Review IAM roles for an ECS task

Assign the ECS execution role and application task role according to their purposes.

Description

Use ExecutionRoleArn for ECS agent operations that need permissions, such as pulling an image or sending logs. Use TaskRoleArn when the application in the container calls AWS APIs.

Potential impact

Missing execution permissions can prevent startup. Missing task permissions can cause the application’s AWS calls to fail.

Remediation

Have each required role trust ecs-tasks.amazonaws.com and grant only the actions it needs. Check task startup and the application’s own API calls without embedding long-lived keys.

Examples

Supply role ARNs as parameters for the two !Ref values shown. For IAM roles declared in the template, use !GetAtt to retrieve their ARNs. Supply service subnets for awsvpc and the resource settings required by your launch type.

Before

yaml
Resources:
  ECSService:
    Type: AWS::ECS::Service
    Properties:
      TaskDefinition: !Ref ECSTaskDefinition

  ECSTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: app
      NetworkMode: awsvpc
      ContainerDefinitions:
        - Name: app
          Image: example/app:latest

After

yaml
Resources:
  ECSService:
    Type: AWS::ECS::Service
    Properties:
      TaskDefinition: !Ref ECSTaskDefinition

  ECSTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: app
      NetworkMode: awsvpc
      ExecutionRoleArn: !Ref TaskExecutionRole
      TaskRoleArn: !Ref AppTaskRole
      ContainerDefinitions:
        - Name: app
          Image: example/app:latest

References