User Migration to Cognito using Lambda trigger in python

家住魔仙堡 提交于 2019-12-07 08:25:39

问题


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:

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);
}
};

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

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


回答1:


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

"desiredDeliveryMediums": "EMAIL",

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

Also

"forceAliasCreation": "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  


来源:https://stackoverflow.com/questions/49745219/user-migration-to-cognito-using-lambda-trigger-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!