Amazon Elastic Container Service (ECS) empowers users to deploy and manage Docker containers on Amazon Elastic Compute Cloud (EC2) or Fargate instances. However, accessing AWS resources in a containerized environment can prove challenging, which is where the Task Role and Task Execution Role come in.

What is Amazon ECS?

ECS serves as a fully managed container orchestration service that provides users with the ability to run and manage Docker containers on a cluster of EC2 or Fargate instances. With its auto-scaling, load balancing, and fully managed capabilities, ECS offers a comprehensive solution for managing containerized applications.

The task role and task execution role are both defined on an ECS task definition level.

Task Role 🔑

The Task Role is an IAM role that attaches to a specific task definition, granting container permissions to access AWS resources like S3 buckets, DynamoDB tables, or other services. The role is assumed by the containers running in the task, you can compare it to attach it directly to an EC2 instance.

By providing granular permissions for each container in your task, the task role allows you to control access scope. For example:

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action": "s3:ListAllMyBuckets",
         "Resource":"*"
      },
      {
         "Effect":"Allow",
         "Action":[
         	"s3:ListBucket",
         ],
         "Resource":"arn:aws:s3:::<bucketName>"
      },
      {
         "Effect":"Allow",
         "Action":[
            "s3:PutObject",
            "s3:GetObject",
         ],
         "Resource":"arn:aws:s3:::<bucketName>/*"
      }
   ]
}

Please remember that this policy could be empty, based on the application you deploy. This is often the case if you deploy cloud-agnostic applications.

Enjoy this article? Subscribe to receive the latest news about cloud security here 📫

Task Execution Role 🔐

The Task Execution Role is an IAM role attached to the task definition responsible for running the task. You define the permissions the task definition needs to perform like connecting to ECR or reading secrets manager keys for the environment variables. This is one level higher than the task role.

This role enables the task definition to access AWS APIs on the task's behalf. With task execution roles, you can assign permissions for a group of tasks running on the same EC2/Fargate instances, making it simpler to grant permissions to your containerized applications. An example of a policy could look like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:GetDownloadUrlForLayer",
                "ecr:DescribeRepositories",
                "ecr:ListImages",
                "ecr:DescribeImages",
                "ecr:BatchGetImage",
                "ecr:ListTagsForResource",
                [...]
            ],
            "Resource": "*"
        }
    ]
}

or if you are using environment variables connecting to the AWS Secrets Manager:

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

Check out my complete guide to using AWS Secrets Manager with Amazon ECS:

How To Manage Secrets In AWS ECS Using AWS Secret Manager: A Comprehensive Guide
Managing secrets in AWS ECS (Elastic Container Service) can be challenging, as you want to ensure that sensitive information like passwords, API keys, and database credentials are secure and only accessible to the applications that need them. AWS Secret Manager is a service that helps you manage sec…

Conclusion 🤝

Both Task Role and Task Execution Role play crucial roles in securing your containerized applications on Amazon ECS. These roles help you implement fine-grained access control, which reduces the risk of unauthorized access and enhances the security of your application. It is critical to follow best practices and use least privilege permissions when configuring these roles to ensure that you only grant necessary access.

🚨
Dont assign the same role to the task and task execution roles to make your life easier 
Share this post