AWS SDK Gateway getUsagePlans not working

85 Views Asked by At

Im trying to get a list of all the API usage plans on my account, running the cli command returns my desired result, however I cant get the JS SDK version to work in Lambda. What's going wrong? I see in he sdk its paginated but it doesn't return data after I include that kind of info either.

CLI:

aws apigateway get-usage-plans

Output:

{
"items": [
    {
        "id": "3hhulv",
        "name": "testplan",
        "apiStages": [
            {
                "apiId": "dp6ounv3jd",
                "stage": "default"
            }
        ],
        "throttle": {
            "burstLimit": 10,
            "rateLimit": 10.0
        },
        "quota": {
            "limit": 10000,
            "offset": 0,
            "period": "MONTH"
        }
    }
]
}

In node:

const AWS = require('aws-sdk');

exports.handler = async (event) => {

var apigateway = new AWS.APIGateway();

var params = {};
var usageplans = apigateway.getUsagePlans(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data); // successful response
});

const response = {
    statusCode: 200,
    things : usageplans.data
    
};
return response;
};

output:

{
"statusCode": 200
}
1

There are 1 best solutions below

0
ALargeRubberDuck On

I resolved the issue by removing the callback function and adding a .promise() onto it.

const AWS = require('aws-sdk');

exports.handler = async (event) => {

var apigateway = new AWS.APIGateway();

var params = {};
var usageplans = await apigateway.getUsagePlans(params).promise();

const response = {
    things : usageplans.position,
    statusCode: 200
    
};

return response;
};