Limit AWS Session Manager Access to ECS tasks based on cluster tag

0

I have two main roles that users use that we will call UserRoleA and UserRoleB. What I want to be able to do is to either allow or deny session manager access to the tasks within a given cluster to UserRoleB based on if the cluster has a tag like AllowAccessToRoleB: False

Is there some condition that I can put in a policy that I can attach to UserRoleB that will allow me to allow/deny access to a task based on a tag that exists in the cluster that the task is running within?

1 Answer
0

Yes, you can achieve this by using AWS Identity and Access Management (IAM) policies with condition keys based on tags. In your case, you want to allow or deny access to tasks within an Amazon ECS cluster based on the cluster's tag. This can be done by using a combination of aws:ResourceTag condition key and IAM policies.

Step 1: Tag Your ECS Clusters Ensure your ECS clusters are tagged appropriately. For example, you might tag your clusters as follows:

Key: AllowAccessToRoleB Value: False (or True) Step 2: Create or Modify the IAM Policy for UserRoleB Create a policy that allows or denies access based on the cluster's tag. Below is an example of such a policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "ecs:DescribeTasks",
                "ecs:DescribeTaskDefinition",
                "ecs:ListTasks",
                "ecs:StartTask",
                "ecs:StopTask"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/AllowAccessToRoleB": "False"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecs:DescribeTasks",
                "ecs:DescribeTaskDefinition",
                "ecs:ListTasks",
                "ecs:StartTask",
                "ecs:StopTask"
            ],
            "Resource": "*"
        }
    ]
}

Step 3: Attach the Policy to UserRoleB Attach the created policy to the IAM role UserRoleB.

Explanation The first statement in the policy explicitly denies actions like DescribeTasks, DescribeTaskDefinition, ListTasks, StartTask, and StopTask on ECS tasks if the ECS cluster has the tag AllowAccessToRoleB with the value False. The second statement allows these actions without any condition. This allows access unless explicitly denied by the first statement.

if you need even more fine-grained control, you can specify the exact ARNs of the ECS clusters and tasks within the policy. Here's an example of specifying the resource ARNs:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "ecs:DescribeTasks",
                "ecs:DescribeTaskDefinition",
                "ecs:ListTasks",
                "ecs:StartTask",
                "ecs:StopTask"
            ],
            "Resource": [
                "arn:aws:ecs:region:account-id:cluster/your-cluster-name",
                "arn:aws:ecs:region:account-id:task/your-cluster-name/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/AllowAccessToRoleB": "False"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecs:DescribeTasks",
                "ecs:DescribeTaskDefinition",
                "ecs:ListTasks",
                "ecs:StartTask",
                "ecs:StopTask"
            ],
            "Resource": [
                "arn:aws:ecs:region:account-id:cluster/your-cluster-name",
                "arn:aws:ecs:region:account-id:task/your-cluster-name/*"
            ]
        }
    ]
}

This example specifies the ECS cluster and its tasks explicitly, which might be necessary for your use case.

By setting up the policy as described, you can control access based on the tags of the ECS clusters, thereby allowing or denying access to UserRoleB as needed.

profile picture
EXPERT
answered 10 months ago
  • This seems to work, and is much less complex than I expected it to be! Thank you

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions