Home > Software engineering >  Cognito throwing Username should be an email
Cognito throwing Username should be an email

Time:05-15

I am attempting to build a lambda function connected to an API gateway POST method that will allow users to create and register cognito users within a given pool:

def lambda_handler(event, context):
  username = json.dumps(event["body"])

  try:
    response = client.admin_create_user(
      UserPoolId=user_pool,
      Username=username,
      TemporaryPassword="TemporaryPassword1234",
    )

    return response

  except ClientError as e:

        return {
            "statusCode": 400,
            "headers": misc.response_parameters_standard,
            "body": e.response["Error"]["Message"],
        }

The username in email format is given in the event body.

I have confirmed via logging statements that the username variable is being successfully extracted as a string in format "[email protected]"

I have confirmed that the lambda function has appropriate permissions to access cognito as admin.

On running this lambda, I am met with the ClientError of:

Username should be an email.

I have sent the username in the same format directly from the CLI and the userpool accepts an identical username without error.

CodePudding user response:

json.dumps converts object -> JSON.
json.loads converts JSON -> object.

If the event is JSON, you want to convert JSON -> object so try username = json.loads(event["body"]) instead.

If it isn't and you're just passing the username as a string, try username = event[“body”].

  • Related