I don’t have much knowledge in AWS
I have following setup,
const express = require("express");
const serverless = require("serverless-http");
const app = express();
app.use(cors());
app.use((req, res, next) => {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Keep-Alive', 'timeout=30');
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(`api-end-point/user`, userRoute);
....
if (process.env.NODE_ENV !== "lambda") {
PORT = process.env.PORT || 7000;
const server = app.listen(PORT, () => {
console.log(`node-express server running in ${process.env.NODE_ENV} mode on ${PORT}`);
});
server.timeout = 0;
}else {
module.exports.handler = serverless(app);
}
user.controller.js
const AWS = require("aws-sdk");
const CryptoJS = require("crypto-js");
const Base64 = require("crypto-js/enc-base64");
const config = require("../core/config/config.json");
...
...
async getUser(token, cb) {
var params = {
AccessToken: token /* required */
};
try {
this.cognitoIdentity.getUser(params, (err, data) => {
if (err) cb(err); // an error occurred
else cb(null, data); // successful response
});
} catch (e) {
console.log(error);
return false;
}
}
And of course, I have cognito user pool from where I get the requested user information using above getUser
by passing Authorization token (after login).
Local Environment
When I run this and all other APIs, believe me I haven’t received any error single time and all APIs work just fine without any problem.
API Gateway + Lambda Enviornment
Problem happens when this code goes to Lambda function.
API gateway routes are configure as /ANY, /{proxy+}
Also, CORS setting is as below,
And the entire code goes to LAMBDA function and lambda is associated with API gateway.
When I hit `API gateway + Lambda (+ congnito)` to get **user information**. It works fine. Most of the time, it works but there are instances where it fails and returns (all of sudden)
503 Service Unavailable
Now, I really have no idea what is going wrong.
TIMEOUT
API gateway timeout is set to default which is 30 seconds.
Lambda timeout is set to 10 MINUTS. (I just increased lambda timeout to try something)
-
BUT THIS ISSUE KEEPS OCCURING
-
Sometime (NOT Every time) I get CORS issue (out of no where and all of sudden)
-
Is there any way I can increase
API gateway
timeout
What should I real do to make work every time ?
NOTE: getUser function is very straight forward. I don’t think it is taking so much time to get user details from cognito user pool. something is wrong which I’m not able to figure out.
Please please help
2
Answers
You should try increasing the timeout value for the API Gateway by going to the API Gateway settings in the AWS Management Console and editing the timeout value for the resource and method that corresponds to the Lambda function you are invoking. The default timeout for an API Gateway is under a minute, which might not be enough time for the Lambda function to complete and return a response.
Answer to question 1:
Seeing your code, I suspect that you are using
serverless
framework. If your code is running normal when youinvoke
locally, I would suggest you to try and reserve your concurrency for this function (or ask AWS to increase your account limit).You can find it in the
Concurrency
tabs and I would suggest you to reserve some concurrency resource for your function. Even though your account has a 1000 concurrency limit, if you are logging to your AWS client account using IAM or deploy on a shared resources, the capacity pools might be shared. I would suggest to check on that as well.Since Lambda usually return
503
when:or when
You can read it more about it here
But if the worst case scenario happen, which means your account concurrency limit is hit, you would need to ask AWS support for raising your total allowed concurrency. Which involves going the Service Quotas dashboard.For a more detailed guide, you can refer to this
Answer to question 2:
Would be hard to answer without knowing more on what path got
CORS
or all of your part just gotCORS
. If it’s all part gotCORS
it could be due to yourOPTIONS
requests not having any response due to your server did not run (Because it hits concurrency limit or something). If concurrency is really the problem you have, this should resolve itself out.Answer to question 3:
You can’t raise APIGateway timeout limit unfortunately, it is fixed at 29 seconds and AWS does not allow you to change it
You can read more details here