在发布确认触发器中禁用用户

时间:2020-09-02 00:05:17

标签: python aws-lambda amazon-cognito amazon-cognito-triggers

我的用户群具有不同的类型(user_type属性将定义类型)。我想在确认后禁用某些类型的用户。即流程:用户注册->用户收到带有代码的确认电子邮件->用户输入代码->调用发布确认触发器。

这是我的帖子确认触发器lambda:

import logging
import boto3

logger = logging.getLogger()
logger.setLevel(logging.INFO)

cognito_client = boto3.client('cognito-idp')

def lambda_handler(event, context):
    user_type =  event['request']['userAttributes'].get('user_type', '')
    logger.info(event)
    if user_type == 'TYPE1':
        response = cognito_client.admin_disable_user(
            UserPoolId=event['userPoolId'],
            Username=event['userName']
        )
        logger.info(response)
    return event

这将返回以下错误:

botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the AdminDisableUser operation: User: arn:aws:sts::<accound_id>:assumed-role/CognitoPostConfirmation-role-xxxxx/CognitoPostConfirmation is not authorized to perform: cognito-idp:AdminDisableUser on resource: arn:aws:cognito-idp:us-east-1:xxxxxx:userpool/us-east-1_xxxxxx

有没有一种方法可以禁止某些类型的用户使用触发器?我也尝试过使用预注册,但是这里的问题是所有其他类型的用户都需要自动确认,这不是我想要的。我需要普通用户接收确认电子邮件,并且某种类型的用户需要接收确认,然后被禁用或不被确认开始。

在此方面,我将不胜感激

谢谢

1 个答案:

答案 0 :(得分:1)

每个Lambda函数都具有执行角色,为了获得允许它进行所需的所有AWS API调用的权限,该角色假定为执行角色。

从错误消息中,您的后确认触发器的Lambda函数似乎具有执行角色CognitoPostConfirmation

该错误消息告诉您它没有运行用来禁用某些用户的cognito-idp:AdminDisableUser方法的正确权限。

因此,您应该转到IAM并将策略添加到CognitoPostConfirmation角色中,以允许您的lambda函数使用该API方法:

{
    "Effect": "Allow",
    "Action": "cognito-idp:AdminDisableUser",
    "Resource": "arn:aws:cognito-idp:<your-aws-region>:<your-aws-account>:userpool/<your-userpool-id>"
}