3

My coworker is trying to attach IAM roles to EC2 instances and doesn’t have permissions. I’m trying to work out which permissions to give him.

My question is: What is the AWS user permission that allows attaching and detaching IAM Roles to instances?

enter image description here

3 Answers 3

5

From Granting a User Permissions to Pass a Role to an AWS Service:

To pass a role (and its permissions) to an AWS service, a user must have permissions to pass the role to the service. This helps administrators ensure that only approved users can configure a service with a role that grants permissions. To allow a user to pass a role to an AWS service, you must grant the PassRole permission to the user's IAM user, role, or group.

When a user passes a role ARN as a parameter to any API that uses the role to assign permissions to the service, the service checks whether that user has the iam:PassRole permission. To limit the user to passing only approved roles, you can filter the iam:PassRole permission with the Resources element of the IAM policy statement.

Is this what you're looking for?

An example from the above-metioned page:

Example 1

Imagine that you want to grant a user the ability to pass any of an approved set of roles to the Amazon EC2 service upon launching an instance. You need three elements:

  • An IAM permissions policy attached to the role that determines what the role can do. Scope permissions to only the actions that the role needs to perform, and to only the resources that the role needs for those actions. You can use AWS managed or customer-created IAM permissions policy.

    {
        "Version": "2012-10-17",
        "Statement": {
            "Effect": "Allow",
            "Action": [ "A list of the permissions the role is allowed to use" ],
            "Resource": [ "A list of the resources the role is allowed to access" ]
        }
    } 
    
  • A trust policy for the role that allows the service to assume the role. For example, you could attach the following trust policy to the role with the UpdateAssumeRolePolicy action. This trust policy allows Amazon EC2 to use the role and the permissions attached to the role.

    {
        "Version": "2012-10-17",
        "Statement": {
            "Sid": "TrustPolicyStatementThatAllowsEC2ServiceToAssumeTheAttachedRole",
            "Effect": "Allow",
            "Principal": { "Service": "ec2.amazonaws.com" },
           "Action": "sts:AssumeRole"
        }
    }       
    
  • An IAM permissions policy attached to the IAM user that allows the user to pass only those policies that are approved. iam:PassRole usually is accompanied by iam:GetRole so that the user can get the details of the role to be passed. In this example, the user can pass only roles that exist in the specified account with names that begin with EC2-roles-for-XYZ-:

    {
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Action": [
                "iam:GetRole",
                "iam:PassRole"
            ],
            "Resource": "arn:aws:iam::<account-id>:role/EC2-roles-for-XYZ-*"
        }]
    }
    

Now the user can start an Amazon EC2 instance with an assigned role. Applications running on the instance can access temporary credentials for the role through the instance profile metadata. The permission policies attached to the role determine what the instance can do.

The procedure(s) to attach a policy to a user/role (inlining it might work as well) are described in Attaching and Detaching IAM Policies:

To attach a managed policy (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, choose Policies.

  3. In the list of policies, select the check box next to the name of the policy to attach. You can use the Filter menu and the search box to filter the list of policies.

  4. Choose Policy actions, and then choose Attach.

  5. Select one or more identities to attach the policy to. You can use the Filter menu and the search box to filter the list of principal entities. After selecting the identities, choose Attach policy.

...

To embed an inline policy for a user or role (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, choose Users or Roles.

  3. In the list, choose the name of the user or role to embed a policy in.

  4. Choose the Permissions tab.

  5. Scroll to the bottom of the page and choose Add inline policy.

    Note

    You cannot embed an inline policy in a service-linked role in IAM. Because the linked service defines whether you can modify the permissions of the role, you might be able to add additional policies from the service console, API, or AWS CLI. To view the service-linked role documentation for a service, see AWS Services That Work with IAM and choose Yes in the Service-Linked Role column for your service.

  6. Choose from the following methods to view the steps required to create your policy:

    • Import an Existing Managed Policy – You can import a managed policy within your account and then edit the policy to customize it to your specific requirements. A managed policy can be an AWS managed policy or a customer managed policy that you created previously.

    • Create a Policy with the Visual Editor – You can construct a new policy from scratch in the visual editor. If you use the visual editor, you do not have to understand JSON syntax.

    • Create a Policy on the JSON Tab – In the JSON tab, you can use JSON syntax to create a policy. You can type a new JSON policy document or paste an example policy.

  7. After you create an inline policy, it is automatically embedded in your user or role.

3
  • Could you expand on the procedure to set this?
    – hawkeye
    Commented Jan 17, 2018 at 23:45
  • This is nice - but you haven’t said what the actual policy I need is.
    – hawkeye
    Commented Jan 25, 2018 at 4:41
  • I don't know of a managed one with iam:PassRole, you may need to create one (or more) - you need 3 according to the quotes. Commented Jan 26, 2018 at 1:39
1

To create (and assign) IAM roles and permissions that user needs to have Administrator level rights within the AWS Account. You can search admin in Attach existing policies directly window in the Add permissions to << user name >>` of the IAM Module accessible via the Console.

From there look at the different levels of Administrator accounts offered by default and you can further look at the JSON provided examples to better understand the specific access rights you could begin to assign to lock down the user to only specific functions.

Edit 2018-01-26 From the AWS Console Page: - Click Services, type in IAM - From the IAM Console Click Users - Select the user then via the Permissions Tab, Click Add Permissions - Locate "AdministratorAccess"

4
  • Could you specify the policy?
    – hawkeye
    Commented Jan 25, 2018 at 4:41
  • Please reference my edit for instructions on setting AdminstratorAccess. Commented Jan 26, 2018 at 18:51
  • Are you wanting your coworker to simply attach a role to an EC2 (for example) or are you wanting him/her to create roles to attach? Simply attaching possibly the other answer would be more appropriate. Creation of roles is another matter. Commented Jan 26, 2018 at 18:53
  • Just attaching - I’d prefer something more specific than administrator access.
    – hawkeye
    Commented Jan 26, 2018 at 22:48
0

I hope you've figured it out yourself. In case you (or some other people) have that issue:

The user that needs to attach / detach the instance profile to an instance needs:

  • EC2 actions to perform that. That includes:
{
    "Sid": "InstanceProfileAccess",
    "Effect": "Allow",
    "Action": [
        "iam:GetInstanceProfile",
        "iam:ListInstanceProfiles",
        "iam:ListInstanceProfilesForRole",
        "iam:AddRoleToInstanceProfile",
        "iam:RemoveRoleFromInstanceProfile"
    ],
    "Resource": "*"
}
  • The passRole action to pass the role to the specific resource.
{
    "Sid": "PassDefaultInstanceProfileToEC2",
    "Effect":"Allow",
    "Action":"iam:PassRole",
    "Resource":"arn:aws:iam::<ID>:role/<ROLE_NAME>"
}

That user can gain access via policies or roles or group. I hope this helps.

You can read more here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.