Description
ECS uses its service role to register load balancer targets. Containers use the task role for AWS API calls, and ECS uses the task execution role for actions such as image pulls.
Potential impact
Excess permissions can affect unrelated resources. Missing permissions can prevent target registration and block deployment.
Remediation
For an awsvpc service, omit Role and use the ECS service-linked role. Where an explicit role is allowed, review its trust policy and limit it to required load-balancer actions and resources. Verify target registration and health checks after deployment.
Examples
The EC2 examples require real role and target group ARNs, cluster capacity and networking.
Before
AWSTemplateFormatVersion: 2010-09-09
Description: Creating ECS service
Parameters:
TargetGroupArn:
Type: String
Resources:
cluster:
Type: AWS::ECS::Cluster
taskdefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: simple-app
Image: amazon/amazon-ecs-sample
Memory: 128
PortMappings:
- ContainerPort: 80
Volumes:
- Host:
SourcePath: /var/lib/docker/vfs/dir/
Name: my-vol
service:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref cluster
Role: arn:aws:iam::123456789012:role/Admin
TaskDefinition: !Ref taskdefinition
LoadBalancers:
- TargetGroupArn: !Ref TargetGroupArn
ContainerName: simple-app
ContainerPort: 80
After
AWSTemplateFormatVersion: 2010-09-09
Description: Creating ECS service
Parameters:
TargetGroupArn:
Type: String
Resources:
cluster:
Type: AWS::ECS::Cluster
taskdefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: simple-app
Image: amazon/amazon-ecs-sample
Memory: 128
PortMappings:
- ContainerPort: 80
Volumes:
- Host:
SourcePath: /var/lib/docker/vfs/dir/
Name: my-vol
service:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref cluster
Role: arn:aws:iam::123456789012:role/EcsServiceRole
TaskDefinition: !Ref taskdefinition
LoadBalancers:
- TargetGroupArn: !Ref TargetGroupArn
ContainerName: simple-app
ContainerPort: 80
The role changes from Admin to EcsServiceRole. The policies are omitted, so compare the actual permissions on each role. Changing Role replaces the service. Inspect the change set first, or narrow the existing role’s policy in place.