Home > Net >  How can I reduce the number of invocations on my REST API/Lambda?
How can I reduce the number of invocations on my REST API/Lambda?

Time:02-25

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): API Gateway AWS 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;
};

CodePudding user response:

I hope this can help Usage Plan tab

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related