Skip to content

Migration Lambda fails to add user to user pool

0

Hi, I've been following the migration lambda developer guide to migrate users over from a different user pool/account. I'm able to successfully retrieve the original user information and the lambda itself seems to be passing but the user is never actually added to the new user pool.

Here is what I currently have, in case anybody can point out something I'm missing: NOTE: the migrationFunc here is just a helper that retrieves the user info

export const handler: UserMigrationTriggerHandler = async (event) => {
  try {
      const response = await migrationFunc(event.userName, event.request.password);
   if (!response) {
      throw new Error("User Not Found");
  }
      event.response.userAttributes = {
        ["custom:metadata"]: JSON.stringify({
          studioId: response.userId,
        }),
        email: event.userName,
        email_verified: "true",
        family_name: response.lastName,
        given_name: response.firstName,
      };
      event.response.finalUserStatus = "CONFIRMED";
      event.response.messageAction = "SUPPRESS";
    }

    console.log("UserMigration event: ", event);
    return event;
  } catch (error) {
    console.error("Migration error: ", error);
    return event;
  }
};
1 Answer
0

Based on your description and the code provided, there are a few potential reasons why the user is not being added to the new user pool despite the Lambda function executing successfully:

  1. Required Attributes: Ensure that all required attributes for the new user pool are properly mapped in the Lambda response. Double-check that the attributes you're setting (email, family_name, given_name) match the required attributes in your new user pool configuration.

  2. Error Handling: Your current error handling might be masking issues. Instead of returning the event object when an error occurs, you should throw an error or return a specific error response. This will help Cognito understand that the migration failed and prevent it from assuming success.

  3. Username Handling: Make sure the 'userName' in the event matches the email address you're using to log in, as it appears you're using email as the username.

  4. Custom Attributes: Verify that the custom attribute "custom:metadata" is properly defined in your new Cognito user pool before attempting to migrate users with this attribute.

  5. Response Format: Ensure that all attribute values are strings. For example, 'email_verified' should be "true" as a string, not a boolean value.

To improve your function, consider these modifications:

  1. Add more detailed logging to track the flow of the function.
  2. Implement proper error handling that communicates failures to Cognito.
  3. Verify that all required attributes are being set correctly.
  4. Double-check that the Lambda function has the necessary permissions to interact with Cognito.

If the problem persists after making these changes, you may need to enable detailed CloudWatch logs for both the Lambda function and Cognito to get more insight into what's happening during the migration process. Also, consider temporarily removing any non-essential attributes from the migration process to isolate the issue.
Sources
Cognito Migration Trigger returns an event, but Cognito fails to create user | AWS re:Post
Migrate user Lambda trigger - Amazon Cognito

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.