2

I would like to migrate users from userPool 1 to userPool 2 with the migration user lambda in AWS Console function. In order to do it, I have used the script provided by AWS but I can't find how I can use authenticateUser for instance. It is not defined when executed.

The migration lambda is executed. authenticateUser is not defined

I have also tried to create a layer, imported succesfully and set the layer in my lambda function but cannot make it work too.

exports.handler = (event, context, callback) => {

    var user;

    if ( event.triggerSource == "UserMigration_Authentication" ) {

        // authenticate the user with your existing user directory service
        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 {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else if ( event.triggerSource == "UserMigration_ForgotPassword" ) {

        // Lookup the user in your existing user directory service
        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 {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else { 
        // Return error to Amazon Cognito
        callback("Bad triggerSource " + event.triggerSource);
    }
};

authenticateUser is not defined

My question is : how do we import this function ?

Thanks a lot.

jkeys
  • 3,803
  • 11
  • 39
  • 63
youpi
  • 205
  • 1
  • 2
  • 6

1 Answers1

0

That sample code is for migrating a user from a legacy database, and the authenticateUser, lookupUser functions are just abstractions for your business logic (which AWS can't write for you). For instance if you have to migrate from a legacy database (not a user pool), then you would lookup their user in your table, grab their salt, hash the password passed in to the migration trigger using the same logic you did in your legacy authentication method, compare it against the stored hashed password in your legacy database, etc. (It gets a little simpler if you were storing passwords in plaintext, but let's not consider that.)

Here's a snippet that should do most of the migration for you. Someone asked a similar question on Github and referenced this StackOverflow issue.

const AWS = require('aws-sdk');
const cognitoIdentity = new AWS.CognitoIdentityServiceProvider({ region: '<your-region-here>' });

const UserPoolId = process.env.deprecatedUserPoolId;

exports.handler = async (event) => {
    const { userName } = event;

    const getUserParams = {
        Username: userName,
        UserPoolId
    };

    try {
        const user = await cognitoIdentity.adminGetUser(getUserParams).promise();
        //TODO: if you have custom attributes, grab them from the user variable and store them in the response below
        event.response = { finalUserStatus: "CONFIRMED" }
        return event;
    } catch (e) {
        throw e; //no user to migrate, give them an error in the client 
    }
};
jkeys
  • 3,803
  • 11
  • 39
  • 63
  • 1
    Thanks a lot, I understand better now and could make work this lambda function ! :) – youpi Oct 23 '19 at 08:09
  • Sorry complete noob chucked in the deepend here. After the lambda is created how do we set up the trigger configuration. If you choose cognito you have to supply an identity pool and not a user pool. – cham May 28 '20 at 21:26
  • 1
    Hi @cham, if you're using the AWS Cognito console UI, you would choose the "user migration" on the Triggers page and set it to the lambda corresponding to this post. The last bit of your comment I'm not sure I understand. Let me know if you're still stuck on this – jkeys Jul 23 '20 at 03:43