如何减少 REST API/Lambda 上的调用次数?
我有一个 REST API 端点,最多可能每隔几天调用几次。然而,当监控我的 API 网关和 Lambda 时,它显示有数千个 API 调用。这是预期的吗?如果不是,我该如何防止这种情况发生?
以下是我的 API 网关和 Lambda 函数的图形用法(我的 API 网关连接到我的 Lambda):
这是我的 Lambda 代码(从 DynamoDB 读取数据):
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const getData = async() => {
const scanResult = await docClient.scan({ "TableName": "redirects" }).promise();
return scanResult;
};
exports.handler = async () => {
var iOS = 0;
var android = 0;
var other = 0;
const data = await getData();
data["Items"].forEach((item) => {
switch (item.operating_system) {
case 'iOS':
iOS += 1;
break;
case 'Android':
android += 1;
break;
default:
other += 1;
}
});
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify([
{
os: "iOS",
count: iOS
},
{
os: "Android",
count: android
},
{
os: "Other",
count: other
}
])
};
return response;
};
I have a REST API endpoint which is probably called few times every couple of days at most. Yet, when monitoring my API Gateway and Lambda, it shows there's been thousands of API Calls. Is this expected? If not, how can I prevent this?
Here are my graph usages for API Gateway and Lambda function (my API Gateway is connected to my Lambda):
And here is the code for my Lambda (reads data from DynamoDB):
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const getData = async() => {
const scanResult = await docClient.scan({ "TableName": "redirects" }).promise();
return scanResult;
};
exports.handler = async () => {
var iOS = 0;
var android = 0;
var other = 0;
const data = await getData();
data["Items"].forEach((item) => {
switch (item.operating_system) {
case 'iOS':
iOS += 1;
break;
case 'Android':
android += 1;
break;
default:
other += 1;
}
});
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify([
{
os: "iOS",
count: iOS
},
{
os: "Android",
count: android
},
{
os: "Other",
count: other
}
])
};
return response;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望这可以帮助 更好地限制 API 请求吞吐量。
您可以使用 API Gateway 服务为您的 API/帐户配置限制和配额设置
或者您可以考虑使用 Cognito 等其他服务来授权访问您的 API(如果不是公开的)或 WAF ,限制某些CIDR、Region的访问,防止DDoS攻击等。
I hope this can help Throttle API requests for better throughput.
You can configure throttle and quota settings for your API/Account with the API Gateway Service
Or you can think to use other services like Cognito to authorize the access to your APIs (if aren't public) or WAF, restricting access to some CIDR, Region, prevent DDoS attack, and so on.
如果您有权访问调用 API 的前端代码,则可以检查它是否内置了任何意外的无限循环,导致应用程序运行时不断调用 API。
If you have access to the frontend code that calls your API, you can check that it doesn't have any accidental infinite loops built in that keep calling the API when the app is running.
一般来说,减少数量。 API调用的数量自然会帮助服务器处理更多的调用。您始终可以尝试尽可能多地缓存数据,这样也可以减少发送的数据量。例如数据库中的查询缓存、反向代理服务器、html 缓存或使用 CDN 等。
Generally speaking reducing the no. of API calls It will naturally help the server to handle more calls. You can always try and cache the data as much as possible in that way too you could reduce the volume of data being send. Like query cache at database, reverse proxy server, cache at html or use CDNs etc.