Bootstrap FreeKB - Amazon Web Services (AWS) - Attach IAM Permission Policy to a Role using the AWS CLI
Amazon Web Services (AWS) - Attach IAM Permission Policy to a Role using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

The aws iam list-roles command can be used to list the IAM (Identity and Access Management) Roles. Something like this should be returned.

~]$ aws iam list-roles
{
    "Roles": [
        {
            "Path": "/",
            "RoleName": "my-role",
            "RoleId": "ARABCDITL71234TJARYZV",
            "Arn": "arn:aws:iam::123456789012:role/my-rolee",
            "CreateDate": "2023-07-18T06:08:58+00:00",
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {
                            "AWS": "arn:aws:iam::123456789012:root"
                        },
                        "Action": "sts:AssumeRole"
                    }
                ]
            },
            "MaxSessionDuration": 3600
        }
    ]
}

 

  • The aws iam attach-user-policy command can be used to attach a policy to a user using the ARN (Amazon Resource Number) of the policy.
  • The aws iam attach-role-policy command (this article) can be used to attach a policy to a role using the ARN (Amazon Resource Number) of the policy.

In this example, the ReadOnlyAccess policy is attached to my-role.

aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/ReadOnlyAccess --role-name my-role

 

In this example, the ReadOnlyAccess policy is attached to my-role.

~]$ aws iam list-attached-role-policies --role-name my-role
{
    "AttachedPolicies": [
        {
            "PolicyName": "ReadOnlyAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/ReadOnlyAccess"
        }
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 671ae1 in the box below so that we can be sure you are a human.