[英]AWS HTTP API Gateway + Lambda ( Node/express) 503 Service unavailable
I don't have much knowledge in AWS我对 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用户.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).当然,我有cognito 用户池,通过传递授权令牌(登录后)使用上面的
getUser
获取请求的用户信息。
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 和所有其他 API 时,请相信我,我一次都没有收到任何错误,所有 API 都可以正常工作,没有任何问题。
API Gateway + Lambda Enviornment API 网关 + Lambda 环境
Problem happens when this code goes to Lambda function.当此代码转到 Lambda function 时会出现问题。
API gateway routes are configure as /ANY, /{proxy+}
API 网关路由配置为
/ANY, /{proxy+}
Also, CORS setting is as below,另外,CORS 设置如下,
And the entire code goes to LAMBDA function and lambda is associated with API gateway .并且整个代码转到LAMBDA function 和lambda与API 网关相关联。
503 Service Unavailable
503服务不可用
Now, I really have no idea what is going wrong .现在,我真的不知道出了什么问题。
TIMEOUT暂停
API gateway timeout is set to default which is 30 seconds . API 网关超时设置为默认值30 秒。
Lambda timeout is set to 10 MINUTS . Lambda 超时设置为10 分钟。 (I just increased lambda timeout to try something)
(我只是增加了 lambda 超时来尝试一下)
BUT THIS ISSUE KEEPS OCCURING但是这个问题不断发生
Sometime (NOT Every time) I get CORS issue (out of no where and all of sudden)有时(不是每次)我收到 CORS 问题(不知从何而来,突然之间)
Is there any way I can increase API gateway
timeout有什么办法可以增加
API gateway
超时
What should I real do to make work every time?我真的应该做什么才能每次都工作?
NOTE: getUser function is very straight forward.注意: getUser function 非常简单。 I don't think it is taking so much time to get user details from cognito user pool.
我认为从 Cognito 用户池中获取用户详细信息不会花费太多时间。 something is wrong which I'm not able to figure out.
出了点问题,我无法弄清楚。
Please please help请帮忙
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.您应该尝试增加 API 网关的超时值,方法是转到 AWS 管理控制台中的 API 网关设置并编辑与您正在调用的 Lambda function 相对应的资源和方法的超时值。 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.
API 网关的默认超时时间不到一分钟,这可能不足以让 Lambda function 完成并返回响应。
Answer to question 1:问题1的答案:
Seeing your code, I suspect that you are using serverless
framework.看到你的代码,我怀疑你正在使用
serverless
框架。 If your code is running normal when you invoke
locally, I would suggest you to try and reserve your concurrency for this function (or ask AWS to increase your account limit).如果你在本地
invoke
时代码运行正常,我建议你尝试为这个 function 保留你的并发(或者让 AWS 提高你的账户限制)。
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.您可以在“
Concurrency
”选项卡中找到它,我建议您为 function 保留一些并发资源。即使您的账户有 1000 个并发限制,如果您使用 IAM 登录到您的 AWS 客户账户或部署在共享资源上,容量池可能是共享的。 I would suggest to check on that as well.我建议也检查一下。
Since Lambda usually return 503
when:由于 Lambda 通常在以下情况下返回
503
:
The number of function executions exceeded one of the quotas (formerly known as limits) that Lambda sets to throttle executions in an AWS Region (concurrent executions or invocation frequency).
function 的执行次数超过了 Lambda 设置的一个配额(以前称为限制),以限制 AWS 区域中的执行(并发执行或调用频率)。
or when或者什么时候
The function exceeded the Lambda function timeout quota.
function 超出了 Lambda function 超时配额。
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.但是,如果发生最坏的情况,即达到您的账户并发限制,您将需要请求 AWS 支持提高您的总允许并发数。 Which involves going the Service Quotas dashboard .For a more detailed guide, you can refer to this
这涉及到Service Quotas 仪表板。有关更详细的指南,您可以参考此
Answer to question 2:问题2的答案:
Would be hard to answer without knowing more on what path got CORS
or all of your part just got CORS
.如果不知道更多关于什么路径得到
CORS
或你的全部部分刚刚得到CORS
,将很难回答。 If it's all part got CORS
it could be due to your OPTIONS
requests not having any response due to your server did not run (Because it hits concurrency limit or something).如果所有部分都得到
CORS
,则可能是由于您的OPTIONS
请求没有任何响应,因为您的服务器未运行(因为它达到并发限制或其他原因)。 If concurrency is really the problem you have, this should resolve itself out.如果并发确实是您遇到的问题,那么这应该会自行解决。
Answer to question 3:问题3的答案:
You can't raise APIGateway timeout limit unfortunately, it is fixed at 29 seconds and AWS does not allow you to change it不幸的是,您不能提高 APIGateway 超时限制,它固定为 29 秒并且 AWS 不允许您更改它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.