在python中使用Lambda触发器将用户迁移到Cognito [英] User Migration to Cognito using Lambda trigger in python

查看:89
本文介绍了在python中使用Lambda触发器将用户迁移到Cognito的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Python中创建了Lambda函数,以将用户从RDS迁移到AWS Cognito。我面临的问题是我的函数的返回类型,以便Cognito创建用户。最初,我返回JSON:

I've created a Lambda function in Python to migrate users from RDS to AWS Cognito. The problem I am facing is the return type for my function in order for Cognito to create the user. At first I was returning JSON:

return {
        "response": {
            "userAttributes": {
                "email": event["userName"],
            },
            "finalUserStatus": "CONFIRMED",
            "messageAction": "SUPPRESS",
            "desiredDeliveryMediums": "EMAIL",
            "forceAliasCreation": "false"
        }
    }

这导致了异常:

Which resulted in an exception:

我也尝试遵循他们只介绍了有关迁移的代码示例(第109页)通过Lambda的用户:

I also tried to follow the only code Sample (Page 109) they presented about migrating users via Lambda:

exports.handler = function (event, context) {
if (event.triggerSource == "UserMigration_Authentication") {
    // authenticate the user with your existing user directory service
    var user = authenticateUser(event.userName, event.request.password);
    if (user) {
        event.response.userAttributes = {
            "email": user.emailAddress,
            "email_verified": "true"
        };
        event.response.finalUserStatus = "CONFIRMED";
        event.response.messageAction = "SUPPRESS";
        context.succeed(event);
    } else {
        context.fail("Bad password");
    }
} else if (event.triggerSource == "UserMigration_ForgotPassword") {
    // Lookup the user in your existing user directory service
    var user = lookupUser(event.userName);
    if (user) {
        event.response.userAttributes = {
            "email": user.emailAddress,

            // required to enable password-reset code to be sent to user
            "email_verified": "true"
        };
        event.response.messageAction = "SUPPRESS";
        context.succeed(event);
    } else {
        context.fail("Bad password");
    }
} else {
    context.fail("Bad triggerSource " + event.triggerSource);
}
};

在此示例中,我假设我应该在向其中添加新值后返回事件对象它,下面是我的Python代码:

In this example, I assumed that I should be returning the "event" object after adding new values to it, here's my Python code below:

event["response"] = {
            "userAttributes": {
                "email": event["userName"],
                "email_verified": "true"
            },
            "finalUserStatus": "CONFIRMED",
            "messageAction": "SUPPRESS",
            "desiredDeliveryMediums": "EMAIL",
            "forceAliasCreation": "false"
        }

    return event

但这也没有用,并引发了同样的异常。在Cognito中创建新用户的正确返回类型是什么?

But that also didn't work and raised the same exception. What is the correct return type to create a new user in Cognito?

推荐答案

问题出在事件对象中,这就是我的目的已经更改为使其正常工作:
删除了行

The issue was in the event object, here's what I've changed to get this to work: removed the line

"desiredDeliveryMediums": "EMAIL",

因为这与参数- messageAction: SUPPRESS冲突。

because that's conflic with the parameter - "messageAction": "SUPPRESS".

"forceAliasCreation": "false" 

是不必要的,因为默认值为false。还应该进行测试以检查用户池中是否不存在用户名/电子邮件。

is not necessary, as false is the default value. There should also be a test to check if username/email don't already exist in the user pool.

我在用户池中使用以下代码进行了测试,它可以正常工作。

I tested with following code in my User Pool, it works.

def lambda_handler(event, context):
    ## print("migrateUserLambda Python")

    event["response"] = {
            "userAttributes": {
                "email": event["userName"],
                "email_verified": "true"
            },
            "finalUserStatus": "CONFIRMED",
            "messageAction": "SUPPRESS"
        }

    return event  

这篇关于在python中使用Lambda触发器将用户迁移到Cognito的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆