CDK 中使用 AWS API Gateway 进行细粒度(方法级)限制

问题描述 投票:0回答:1

如果不在 AWS UI 控制台上手动调整,我就无法在 AWS Gateway 上实现方法级 API 限制以使 REST API 正常工作。

我通常通过舞台设置中的“覆盖”来设置它。

有没有办法在 AWS CDK 中完成此操作?

aws-cdk
1个回答
0
投票

我注意到

apigateway.RestApi
中有两种方法级节流选项。

  1. 您可以在创建网关时添加

    methodOptions
    ,但这实际上不起作用,因为更改永远不会同步到 AWS UI 控制台(作为阶段内方法的“覆盖”)。

  2. 您还可以更改

    throttling
    下的使用计划来指定细粒度方法,但您还必须激活粗粒度(整个 API)限制。

请参阅 https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.UsagePlanPerApiStage.html

      const usagePlan = new apigateway.UsagePlan(context, `UsagePlan`, {
        name: `UsagePlan`,
        description: `Usage Plan for API`,
        throttle: {
          rateLimit: 10000,
          burstLimit: 10000
        },
        apiStages: [
          {
            api: gateway,
            stage: gateway.deploymentStage,
            throttle: [
              {
                method: postMethod,
                throttle: {
                  rateLimit: 20,
                  burstLimit: 100
                }
              }
            ]
          }
        ]
      })
© www.soinside.com 2019 - 2024. All rights reserved.