Does anybody know how to do Throttling/rate-limiti...
# sst
a
Does anybody know how to do Throttling/rate-limiting on API GW v2 APIs? maybe with the
Api
construct? Found that documentation shows this example, but HOW we do it with CDK?
Copy code
aws apigatewayv2 update-stage \
    --api-id a1b2c3d4 \
    --stage-name dev \
    --route-settings '{"GET /pets":{"ThrottlingBurstLimit":100,"ThrottlingRateLimit":2000}}'
Found this on CDK, but this is for GW v1 😩 . https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.ThrottleSettings.html
f
Hey @AdriƔn Mouly, give this a try
Copy code
const api = new sst.Api(..);
const cfnStage = api.defaultStage.node.defaultChild;
cfnStage.routeSettings = { ... };
a
Thanks sir, going to check it.
Maybe this is something that the API construct can have?
😩
Found this…
Copy code
const cfnStage = api.httpApi.defaultStage?.node.defaultChild;
This works on Types, but not sure if is going to work, hahah.
Copy code
const cfnStage = api.httpApi.defaultStage?.node.defaultChild as CfnStage;
    cfnStage.routeSettings = {
      throttlingBurstLimit: 10,
      throttlingRateLimit: 100,
    };
@Frank thoughts?
This is the diff, looks promising.
Didn’t work šŸ˜ž
Copy code
Property validation failure: [Value of property {/RouteSettings/throttlingBurstLimit} does not match type {Object}, Value of property {/RouteSettings/throttlingRateLimit} does not match type {Object}]
f
@AdriĆ”n Mouly, I’m reading through the CFN docs, it seems
cfnStage
has
defaultRouteSettings
and
routeSettings
a
Mmm.
f
This would apply to all routes
Copy code
cfnStage.defaultRouteSettings = {
      throttlingBurstLimit: 10,
      throttlingRateLimit: 100,
    };
a
So will be like…
Copy code
const cfnStage = api.httpApi.defaultStage?.node.defaultChild as CfnStage;
    cfnStage.defaultRouteSettings = {
      throttlingBurstLimit: 100,
      throttlingRateLimit: 50,
    };
If I don’t put the
as
the type is not recognized.
f
yeah
as
is required for TS
a
Ok going to try.
Can we add this to the SST construct?
This is the diff, going to try it.
Looks like it worked!!
No errors on CF so far.
f
Will add this in the next release!
a
šŸ‘ šŸ˜
Is going to look nicer?
f
Just added this in v0.40.5, it looks like this:
Copy code
new Api(this, "Api", {
  defaultThrottlingRateLimit: 2000,
  defaultThrottlingBurstLimit: 100,
  routes: {
    "GET  /notes": "list.main",
    "POST /notes": "create.main",
  },
});