-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_get_all_users.py
More file actions
55 lines (48 loc) · 1.55 KB
/
Copy pathlambda_get_all_users.py
File metadata and controls
55 lines (48 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import boto3
import botocore.exceptions
import json
USER_POOL_ID = ""
def lambda_handler(event, context):
# It sets the user pool autoConfirmUser flag after validating the email domain
client = boto3.client('cognito-idp')
try:
response = client.list_users(
UserPoolId=USER_POOL_ID,
AttributesToGet=[
"email",
'custom:tag',
],
)
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({"error": True,
"message": str(e),
})}
if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
return {
'statusCode': 500,
'body': json.dumps({"error": True,
"message": "Unable to retrieve users",
})}
res=[]
for user in response["Users"]:
attributes = user["Attributes"]
email=None
tags=[]
for attribute in attributes:
if attribute["Name"] == "email":
email = attribute["Value"]
elif attribute["Name"] == "custom:tag":
tags.extend(attribute["Value"].split(";"))
res.append({"username":user["Username"],"email":email,"tag":tags})
# Return to Amazon Cognito
return {
'statusCode': 200,
'body': json.dumps({
"success": True,
"data": res
})
}
if __name__=="__main__":
print(lambda_handler({},None))