用户在python中使用Lambda触发器迁移到Cognito

时间:2018-04-10 04:15:32

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

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

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

导致例外情况: enter image description here

我还尝试按照他们介绍的有关通过Lambda迁移用户的唯一code Sample(页109):

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代码:

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

    return event

但这也没有奏效并提出同样的例外。在Cognito中创建新用户的正确返回类型是什么?

1 个答案:

答案 0 :(得分:3)

问题出现在事件对象中,这是我为改变它而改变的方式: 删除了行

"desiredDeliveryMediums": "EMAIL",

因为它与参数 - “messageAction”:“SUPPRESS”冲突。

另外

"forceAliasCreation": "false" 

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

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

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

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

    return event