I have added a lambda function to my api gateway. It is a get request and it returns an array of data. I am using mongodb for my database
Domain: domain.com/api/list/
Sometime if I request the domain without a slash it works fine, sometimes it does not work and returns 502. If I request it in the normal browser it returns 502 but if i request the same url in the tor browser then it always works fine.
I suspect it might be something related to caching but why would caching result in 502?
module.exports.getAll = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
await database.connect();
const x = await model.find({});
callback(null, {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(x)
})
};
I have set caching for this api gateway to 0 on cloudfront. I have increased the timeout to 10 seconds but cloudwatch says Task timed out after 6.01 seconds
x-amzn-errortype: InternalServerErrorException
x-amzn-requestid: 578f0c43-9798-4949-813c-52259d57375b
x-cache: Error from cloudfront
I have also made a npm package which contains my mongodb schemas and creates the connection to the mongoclient. Something I realised from looking at the cloudwatch is that I get the errors sometimes when 'using existing database connection' is printed out.
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const url = ``
let isConnected;
module.exports = connectToDatabase = () => {
if (isConnected) {
console.log('=> using existing database connection');
return Promise.resolve();
}
console.log('=> using new database connection');
return mongoose.connect(url).then(db => {
isConnected = db.connections[0].readyState;
});
};