2

I am trying to add a user to cognito and assign it a group. Adding to group is successful, but I cannot add to which group a user belongs.

import boto3

email = '[email protected]'
poolId = 'ap-northeast-1_xxxxxx'
group = 'xxxx'
password = 'String1234'
aws_access_key_id = 'Axxxxxxxx'
aws_secret_access_key = '6xxxxxxxxx'

session = boto3.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
client = session.client('cognito-idp')

response = client.admin_create_user(
                UserPoolId=poolId,
                Username=email,
                TemporaryPassword= password,
                UserAttributes=[

                    {
                        "Name": "email",
                        "Value": email
                    },
                    {
                        "Name": "email_verified",
                        "Value": "true"
                    }
                ]
            )

response = client.admin_add_user_to_group(
                UserPoolId=poolId,
                Username=email,
                GroupName=group
            )

The user is added successfully but a group is not added:

Traceback (most recent call last):   File "test.py", line 24, in <module>
response = client.admin_add_user_to_group
( AttributeError: 'CognitoIdentityProvider' object has no attribute 'admin_add_user_to_group'

In my requirements.txt for creating virtual enviorment for python I use

boto3==1.4.5

Using this boto3 docs for reference.

1 Answer 1

6

I have made it work with boto3 (1.4.6). The example you have there I hope assumes that you have created the group that you are trying to add the user to. Also the following code sample assumes that you have exported you access, secret keys as environment variables.

import boto3
a='XXXXXXXXXXXXXXXXXXXX'
s='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
r='us-west-2'

session = boto3.Session(aws_access_key_id=a, aws_secret_access_key=s, region_name=r)
client = boto3.client('cognito-idp', region_name=r)

email = '[email protected]'
poolId = 'us-west-2_xxxxxxxxx'
group = 'foo'
password = 'String1234'

response = client.admin_create_user(
                UserPoolId=poolId,
                Username=email,
                TemporaryPassword= password,
                UserAttributes=[{"Name": "email","Value": email}, { "Name": "email_verified", "Value": "true" }]
            )

reply = client.create_group(UserPoolId=poolId,  GroupName=group)
reply = client.admin_add_user_to_group( UserPoolId=poolId, Username=email, GroupName=group )

Your Answer

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

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