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!

已提問 5 年前檢視次數 186 次
2 個答案
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

已回答 5 年前
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
已回答 5 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南