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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠