Jhayes2118
Jhayes2118

Reputation: 842

Nodejs Cognito cognitoUser.authenticateUser() getting 502 bad gateway on lambda

I have an api call that is supposed to use the Cognito Javascript SDK to authenticate the user. It is not working when deployed to lambda, but works locally. Why isn't this working?

router.post('/login', function (req, res, next) {

  var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
    Username: req.body.username,
    Password: req.body.password
  });
  var userData = {
    Username: req.body.username,
    Pool: userPool
  };
   try {

     var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
     cognitoUser.authenticateUser(authenticationDetails, {
       onSuccess: function (result) {
         res.status(200);
         res.send({ token: `${result.getAccessToken().getJwtToken()}` });
       },
       onFailure: function (err) {
         res.status(401);
         res.send({ "Message": "Login failed, please try again" });
       }
     });
   }
   catch (ex) {
     res.status(500);
     res.send({
       "message": "error logging in",
       "error": ex
     });
   }
});

is returning

---502 bad gateway
{
    "message": "Internal server error"
}

Upvotes: 0

Views: 732

Answers (1)

Jhayes2118
Jhayes2118

Reputation: 842

The real issue was nothing to do with lambda or the deployment or permissions as I thought it would be. It was really a timeout issue.

I increased my lambda memory and that sped it up a bit and made the call come back successfully.

Options 1) increase ram 2) increase the timeout limit

Upvotes: 2

Related Questions