Search code examples
aws-api-gatewayaws-sdkaws-sdk-nodejs

How to create usage plan with aws-sdk-apigateway?


I am trying to create a usage plan with aws-sdk apigateway, but getting an error response.

Here is the function I am using:

const params = {
    name: 'My usage plan',
    description: 'My plan for checking the usage',
    apiStages: [
      {
        apiId: 'b3mzhze48g',
        stage: 'dev',
        throttle: {
          string: {
            burstLimit: 4,
            rateLimit: 8,
          },
        },
      },
    ],
    throttle: {
      burstLimit: 5,
      rateLimit: 10,
    },
    quota: {
      limit: 100,
      offset: 1,
      period: 'WEEK',
    },
  };
  try {
    const result = await apigateway.createUsagePlan(params).promise();
    res.send(result);
  } catch (err) {
    res.send(err);
  }

here is an image of the id's I have for my API:

api-ids

and this is the error message I get:

{
    "message": "Invalid method {resourcePath: string,method: } specified",
    "code": "BadRequestException",
    "time": "2021-01-31T10:58:28.117Z",
    "requestId": "6d175534-e555-4a0d-99ef-1f3fc628e502",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 80.40030962554764
}

How should I modify the apiStages parameters to be able to set the usage plan for a specific resource in my API?


Solution

  • Issue is with apiStages -> throttle, it is used to throttle at resource & method level, It is optional, But if it is set, key should be resource + method name , i.e. for GET /staff/list method , /staff/list/GET should be key

    apiStages: [
        {
          apiId: "abc111i6h3",
          stage: "dev",
          throttle: {
            "/staff/list/GET": {
              burstLimit: 4,
              rateLimit: 8,
            },
          },
        },
      ]
    

    Example from aws documentation that gave that hint: enter image description here