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!

preguntada hace 5 años186 visualizaciones
2 Respuestas
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

respondido hace 5 años
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
respondido hace 5 años

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas