Bimlesh Sharma
Bimlesh Sharma

Reputation: 306

Not able to attach policy to a role using Boto3

Using BOTO3 script,Created a Role and a Policy and trying to attached policy to that role. I am getting error while attaching but if i do attach manually then working fine.

Using BOTO3 i am doing followings: Created a AWS role say "TEST" Created a policy called "POL" Both have been created and we can see on AWS console. Now attaching policy to Role with below command

response = client.attach_role_policy(
    RoleName='TEST',
    PolicyArn='arn:aws:iam::6929051012:policy/POL'
)

getting below error.

raise error_class(parsed_response, operation_name)
botocore.errorfactory.NoSuchEntityException: An error occurred (NoSuchEntity) when calling the AttachRolePolicy operation: Policy arn:aws:iam::6929051012:policy/POL does not exist or is not attachable.

Manually i can attached this policy to Role. Your Help is highly appreciated. Thanks

Upvotes: 0

Views: 2732

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270184

To reproduce your situation, I did the following:

  • Created an IAM Role (stack-role) via the management console
  • Created an IAM Policy via the management console (arn:aws:iam::123456789012:policy/stack-policy)

I then ran:

import boto3

iam_client = boto3.client('iam')

response = iam_client.attach_role_policy(
    RoleName='stack-role',
    PolicyArn='arn:aws:iam::123456789012:policy/stack-policy'
)

print (response)

The call returned successfully. I then looked at the Role in the IAM management console and the stack-policy was attached.

So, seems to work fine!

Upvotes: 0

Related Questions