Cognito with Lambda doesn't work at the first attempt, requires more

0

i have a function, which read a json with some users informations
i need to create a cognito user for everyone in the json:

const AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
AWS.config.update({
  region: "eu-west-2"
});
const usersJSON = require("./users.json");
let saved = {
  Users: []
};

for (let o in usersJSON.Users) {
  let { name, surname, userClass } = usersJSON.Users[o];
  let emailToSave = `${nome}${cognome}${classe}@spalla.it`;
  let email = emailToSave.toLowerCase();
  let password = `Pwd${Math.floor(10000 + Math.random() * 90000)}`;

  let user = {
    nome: name,
    cognome: surname,
    classe: userClass,
    email: email,
    password: password,
    time: Date()
  };

  saved.Users.push(user);

  let params = {
    UserPoolId: cognito.userPoolId,
    Username: email,
    DesiredDeliveryMediums: [
      "EMAIL",
    ],
    MessageAction: "SUPPRESS",
    TemporaryPassword: password,
    UserAttributes: [{
        Name: 'nickname',
        Value: name
      },
      {
        Name: 'email',
        Value: email
      },
      {
        Name: 'custom:authCode',
        Value: "000000"
      },
      {
        Name: 'email_verified',
        Value: "True"
      },
      {
        Name: 'custom:userClass',
        Value: userClass
      },
      {
        Name: 'custom:role',
        Value: "user"
      }
    ]
  };

  cognitoidentityserviceprovider.adminCreateUser(params, function(err, data) {
    console.log("user created");
  });
}

the first time i run this function it does literally nothing, then the second time i run it it creates the two users successfully. ( note: this is only the specific part of the entire code, it was working perfectly some weeks ago.. but now it doesn't. it also includes at the end a function to send an email (nodemailer) with the users created and, as the creation, it works only at the third - fourth time. )

i'm using aws-serverless-express
what could be the problem? thanks in advance!

asked 5 years ago180 views
2 Answers
0

It kinda solved alone.. basically as i said, it's an API so it wasn't used for that cognito function only. If i use the API for something before using the function after uploading the code to Lambda, it works as expected.
So.. maybe cognito first need to to load the whole code to work as expected? maybe it's a problem of aws-serverless-express? maybe it's a problem in the code somewhere? i don't know.

Edited by: danieleservadei on Jan 24, 2019 3:16 PM

answered 5 years ago
0

Hello Daniele,
I'm having almost the same symptoms while trying to create a Cognito user with adminCreateUser - no error, no user created. I'm using npm.
Can you please be more specific about your findings?

Thanks
Davide

fugaz
answered 5 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.

Guidelines for Answering Questions