简体   繁体   中英

Attaching IAM user to IAM Group using Boto3

I am trying to create an IAM policy, to an IAM group using Boto3. So far, I am not able to add IAM user to an IAM group. For "response = iam.add_user_to_group( ", I am getting an error, "Exception has occurred: AttributeError 'iam.ServiceResource' object has no attribute 'add_user_to_group'"

import boto3
iam = boto3.resource('iam') #using resource representing IAM
created_user = iam.create_user(
    UserName='some_random_user'
)
print(created_user)

create_group_response = iam.create_group(GroupName = 'Tester')

response = iam.add_user_to_group(
UserName = 'some_random_user', #Name of user
GroupName = 'Tester'
)

response = iam.attach_user_policy(
UserName = 'some_random_user', #Name of user
PolicyArn = 'arn:aws:iam::196687784845:policy/boto-test' 
# Policy ARN which you want to asign to user
)

Not sure what seems to be the problem, I am very new to python and boto so might be a very small thing.

According to the doc , add_user_to_group is an action for IAM client (not resource). Use this action on Group resource instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM