Amazon ECS task execution IAM role - Amazon Elastic Container Service

Amazon ECS task execution IAM role

The task execution role grants the Amazon ECS container and Fargate agents permission to make AWS API calls on your behalf. The task execution IAM role is required depending on the requirements of your task. You can have multiple task execution roles for different purposes and services associated with your account. For the IAM permissions that your application needs to run, see Amazon ECS task IAM role.

The following are common use cases for a task execution IAM role:

  • Your task is hosted on AWS Fargate or on an external instance and:

    • pulls a container image from an Amazon ECR private repository.

    • pulls a container image from an Amazon ECR private repository in a different account from the account that runs the task.

    • sends container logs to CloudWatch Logs using the awslogs log driver. For more information, see Send Amazon ECS logs to CloudWatch .

  • Your tasks are hosted on either AWS Fargate or Amazon EC2 instances and:

Note

The task execution role is supported by Amazon ECS container agent version 1.16.0 and later.

Amazon ECS provides the managed policy named AmazonECSTaskExecutionRolePolicy which contains the permissions the common use cases described above require. For more information, see AmazonECSTaskExecutionRolePolicy in the AWS Managed Policy Reference Guide. It might be necessary to add inline policies to your task execution role for special use cases

The Amazon ECS console creates a task execution role. You can manually attach the managed IAM policy for tasks to allow Amazon ECS to add permissions for future features and enhancements as they are introduced. You can use IAM console search to search for ecsTaskExecutionRole and see if your account already has the task execution role. For more information, see IAM console search in the IAM user guide.

If you pull images as an authenticated user, you're less likely to be impacted by the changes that occurred to Docker Hub's pull rate limits. For more information see, Private registry authentication for container instances.

By using Amazon ECR and Amazon ECR Public, you can avoid the limits imposed by Docker. If you pull images from Amazon ECR, this also helps shorten network pull times and reduces data transfer changes when traffic leaves your VPC.

When you use Fargate, you must authenticate to a private image registry using repositoryCredentials. It's not possible to set the Amazon ECS container agent environment variables ECS_ENGINE_AUTH_TYPE or ECS_ENGINE_AUTH_DATA or modify the ecs.config file for tasks hosted on Fargate. For more information, see Private registry authentication for tasks.

Creating the task execution role

If your account doesn't already have a task execution role, use the following steps to create the role.

AWS Management Console
To create the service role for Elastic Container Service (IAM console)
  1. Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.

  2. In the navigation pane of the IAM console, choose Roles, and then choose Create role.

  3. For Trusted entity type, choose AWS service.

  4. For Service or use case, choose Elastic Container Service, and then choose the Elastic Container Service Task use case.

  5. Choose Next.

  6. In the Add permissions section, search for AmazonECSTaskExecutionRolePolicy, then select the policy.

  7. Choose Next.

  8. For Role name, enter ecsTaskExecutionRole.

  9. Review the role, and then choose Create role.

AWS CLI

Replace all user input with your own information.

  1. Create a file named ecs-tasks-trust-policy.json that contains the trust policy to use for the IAM role. The file should contain the following:

    { "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "ecs-tasks.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
  2. Create an IAM role named ecsTaskExecutionRole using the trust policy created in the previous step.

    aws iam create-role \ --role-name ecsTaskExecutionRole \ --assume-role-policy-document file://ecs-tasks-trust-policy.json
  3. Attach the AWS managed AmazonECSTaskExecutionRolePolicy policy to the ecsTaskExecutionRole role.

    aws iam attach-role-policy \ --role-name ecsTaskExecutionRole \ --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy

After you create the role, add additional permissions to the role for the following features.

Feature Additional permissions

Use Secrets Manager credentials to access your container image private repository

Private registry authentication permissions

Pass sensitive data with Systems Manager or Secrets Manager

Secrets Manager or Systems Manager permissions

Have Fargate tasks pull Amazon ECR images over interface endpoints

Fargate tasks pulling Amazon ECR images over interface endpoints permissions

Host configuration files in an Amazon S3 bucket

Amazon S3 file storage permissions

Configure Container Insights to view Amazon ECS lifecycle events

Permissions required to configure Container Insights to view Amazon ECS lifecycle events

View Amazon ECS lifecycle events in Container Insights

Permissions required to view Amazon ECS lifecycle events in Container Insights

Private registry authentication permissions

To provide access to the secrets that you create, add the following permissions as an inline policy to the task execution role. For more information, see Adding and Removing IAM Policies.

  • secretsmanager:GetSecretValue

  • kms:Decrypt—Required only if your key uses a custom KMS key and not the default key. The Amazon Resource Name (ARN) for your custom key must be added as a resource.

The following is an example inline policy that adds the permissions.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Decrypt", "secretsmanager:GetSecretValue" ], "Resource": [ "arn:aws:secretsmanager:<region>:<aws_account_id>:secret:secret_name", "arn:aws:kms:<region>:<aws_account_id>:key/key_id" ] } ] }

Secrets Manager or Systems Manager permissions

The permission to allow the container agent to pull the necessary AWS Systems Manager or Secrets Manager resources. For more information, see Pass sensitive data to an Amazon ECS container.

Using Secrets Manager

To provide access to the Secrets Manager secrets that you create, manually add the following permission to the task execution role. For information about how to manage permissions, see Adding and Removing IAM identity permissions in the IAM User Guide.

  • secretsmanager:GetSecretValue– Required if you are referencing a Secrets Manager secret. Adds the permission to retrieve the secret from Secrets Manager.

The following example policy adds the required permissions.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": [ "arn:aws:secretsmanager:region:aws_account_id:secret:secret_name" ] } ] }

Using Systems Manager

Important

For tasks that use the EC2 launch type, you must use the ECS agent configuration variable ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE=true to use this feature. You can add it to the ./etc/ecs/ecs.config file during container instance creation or you can add it to an existing instance and then restart the ECS agent. For more information, see Amazon ECS container agent configuration.

To provide access to the Systems Manager Parameter Store parameters that you create, manually add the following permissions as a policy to the task execution role. For information about how to manage permissions, see Adding and Removing IAM identity permissions in the IAM User Guide.

  • ssm:GetParameters — Required if you are referencing a Systems Manager Parameter Store parameter in a task definition. Adds the permission to retrieve Systems Manager parameters.

  • secretsmanager:GetSecretValue — Required if you are referencing a Secrets Manager secret either directly or if your Systems Manager Parameter Store parameter is referencing a Secrets Manager secret in a task definition. Adds the permission to retrieve the secret from Secrets Manager.

  • kms:Decrypt — Required only if your secret uses a customer managed key and not the default key. The ARN for your custom key should be added as a resource. Adds the permission to decrypt the customer managed key .

The following example policy adds the required permissions:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ssm:GetParameters", "secretsmanager:GetSecretValue", "kms:Decrypt" ], "Resource": [ "arn:aws:ssm:region:aws_account_id:parameter/parameter_name", "arn:aws:secretsmanager:region:aws_account_id:secret:secret_name", "arn:aws:kms:region:aws_account_id:key/key_id" ] } ] }

Fargate tasks pulling Amazon ECR images over interface endpoints permissions

When launching tasks that use the Fargate launch type that pull images from Amazon ECR when Amazon ECR is configured to use an interface VPC endpoint, you can restrict the tasks access to a specific VPC or VPC endpoint. Do this by creating a task execution role for the tasks to use that use IAM condition keys.

Use the following IAM global condition keys to restrict access to a specific VPC or VPC endpoint. For more information, see AWS Global Condition Context Keys.

  • aws:SourceVpc—Restricts access to a specific VPC. You can restrict the VPC to the VPC that hosts the task and endpoint.

  • aws:SourceVpce—Restricts access to a specific VPC endpoint.

The following task execution role policy provides an example for adding condition keys:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecr:GetAuthorizationToken", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ecr:BatchCheckLayerAvailability", "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage" ], "Resource": "*", "Condition": { "StringEquals": { "aws:sourceVpce": "vpce-xxxxxx", "aws:sourceVpc": "vpc-xxxxx" } } } ] }

Amazon S3 file storage permissions

When you specify a configuration file that's hosted in Amazon S3, the task execution role must include the s3:GetObject permission for the configuration file and the s3:GetBucketLocation permission on the Amazon S3 bucket that the file is in. For more information, see Policy actions for Amazon S3 in the Amazon Simple Storage Service User Guide.

The following example policy adds the required permissions for retrieving a file from Amazon S3. Specify the name of your Amazon S3 bucket and configuration file name.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-bucket/folder_name/config_file_name" ] }, { "Effect": "Allow", "Action": [ "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::amzn-s3-demo-bucket" ] } ] }

Permissions required to configure Container Insights to view Amazon ECS lifecycle events

The following permissions are required in the task role to configure the lifecycle events:

  • events:PutRule

  • events:PutTargets

  • logs:CreateLogGroup

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "events:PutRule", "events:PutTargets", "logs:CreateLogGroup" ], "Resource": "*" } ] }

Permissions required to view Amazon ECS lifecycle events in Container Insights

The following permissions are required to view the lifecycle events. Add the following permissions as an inline policy to the task execution role. For more information, see Adding and Removing IAM Policies.

  • events:DescribeRule

  • events:ListTargetsByRule

  • logs:DescribeLogGroups

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "events:DescribeRule", "events:ListTargetsByRule", "logs:DescribeLogGroups" ], "Resource": "*" } ] }