Невозможно отправить почтовый запрос от AWS лямбда-функции на внешнюю конечную точку - PullRequest
0 голосов
/ 29 мая 2020

Я не могу отправить почтовый запрос от AWS лямбда-функции на внешнюю конечную точку.

Вот мой код в лямбда-функции, пожалуйста, сообщите, где я могу ошибиться.

var request     = require('request');

exports.handler = async (event) => {
    let secretLoginCode;
    if (!event.request.session || !event.request.session.length) {
        // This is a new auth session
        // Generate a new secret login code and mail it to the user
        secretLoginCode = crypto_secure_random_digit_1.randomDigits(6).join('');

        callServerEndpoint(event.request.userAttributes.phone_number, secretLoginCode)
    }
    else {
        // There's an existing session. Don't generate new digits but
        // re-use the code from the current session. This allows the user to
        // make a mistake when keying in the code and to then retry, rather
        // then needing to e-mail the user an all new code again.    
        const previousChallenge = event.request.session.slice(-1)[0];
        secretLoginCode = previousChallenge.challengeMetadata.match(/CODE-(\d*)/)[1];
    }
    // This is sent back to the client app
    event.response.publicChallengeParameters = { email: event.request.userAttributes.email };
    // Add the secret login code to the private challenge parameters
    // so it can be verified by the "Verify Auth Challenge Response" trigger
    event.response.privateChallengeParameters = { secretLoginCode }
    event.response.challengeMetadata = `CODE-${secretLoginCode}`;
    return event;
};


function callServerEndpoint(mobileNumber, secretLoginCode){

  var message                                     = {};
    message["mobileNumberWithCountryCode"]          = mobileNumber;
    message["secretLoginCode"]                      = secretLoginCode;


  var options = 
    { 
        method  : 'POST',
        url     : 'https://example.com/v1/sendLoginPin',
        headers : { 
                    'cache-control' : 'no-cache',
                    'content-type'  : 'application/json' 
                  },
        body    : message,
        json    : true 
    };

  request(options, function (error, response, body) {
    if (error){
      console.log("Error: "+JSON.stringify(error, null, 4));
    } else{
      console.log("success"+JSON.stringify(body, null, 4));
    }
  });
}

1 Ответ

0 голосов
/ 29 мая 2020

Не могли бы вы предоставить код обработчика с помощью этой функции callServerEndpoint?

Проблема также может заключаться в тайм-ауте лямбда-функции. Это происходит, когда сообщение длиннее таймаута, установленного для лямбда-функции. Кстати, облачные часы должны отслеживать это.

PS: Пакет запроса устарел, см. ссылку . Было бы интересно посмотреть на ax ios, superagent или другие пакеты http-клиента

...