Amazon API Gateway supports defining default limits for an API to prevent it from being overwhelmed by too many requests. This uses a token bucket algorithm, where a token counts for a single request.
AWS SAM templates
For SAM REST APIs, this rule requires a wildcard MethodSettings entry (HttpMethod: "*", ResourcePath: "/*") with both ThrottlingBurstLimit and ThrottlingRateLimit; throttling declared only for a specific route does not count as a default. SAM HTTP APIs use DefaultRouteSettings with both limits. For AWS::Serverless::Api and AWS::Serverless::HttpApi resources, this rule checks the effective stage settings directly, including values inherited from Globals.Api or Globals.HttpApi. Function Api events without RestApiId and HttpApi events without ApiId create implicit APIs, which are also checked using the applicable global settings; each implicit API finding is reported once per owning function, so functions sharing an implicit API can each receive a finding.
import{RestApi}from'@aws-cdk/aws-apigateway';exportclassMyStackextendscdk.Stack{constructor(scope:cdk.Construct,id:string,props?:cdk.StackProps){super(scope,id,props);constmyApi=newRestApi(scope,'MyApi',{deployOptions:{// Throttling for default methodsmethodOptions:{'*/*':{throttlingBurstLimit:1000,throttlingRateLimit:10,}}},});}}
1 2 3 4 5 6 7 8 910111213141516171819
{"Resources":{"MyApi":{"Type":"AWS::Serverless::Api","Properties":{"DefinitionUri":"openapi.yaml","StageName":"prod",// Throttling for default methods by setting HttpMethod to '*' and// ResourcePath to '/*'"MethodSettings":[{"HttpMethod":"*","ResourcePath":"/*","ThrottlingRateLimit":10,"ThrottlingBurstLimit":1000}]}}}}
1 2 3 4 5 6 7 8 91011121314
Resources:MyApi:Type:AWS::Serverless::ApiProperties:DefinitionUri:openapi.yamlStageName:prod# Throttling for default methods by setting HttpMethod to '*' and# ResourcePath to '/*'MethodSettings:-HttpMethod:"*"ResourcePath:"/*"ThrottlingRateLimit:10ThrottlingBurstLimit:1000
1 2 3 4 5 6 7 8 9101112131415
resources:Resources:MyApi:Type:AWS::Serverless::ApiProperties:DefinitionUri:openapi.yamlStageName:prod# Throttling for default methods by setting HttpMethod to '*' and# ResourcePath to '/*'MethodSettings:-HttpMethod:"*"ResourcePath:"/*"ThrottlingRateLimit:10ThrottlingBurstLimit:1000
resource"aws_api_gateway_stage""this"{body=file("openapi.yaml")}resource"aws_api_gateway_deployment""this"{rest_api_id=aws_api_gateway_rest_api.this.idtriggers={redeployment=sha1(jsonencode(aws_api_gateway_rest_api.this.body))}lifecycle{create_before_destroy=true}}resource"aws_api_gateway_stage""this"{deployment_id=aws_api_gateway_deployment.this.idrest_api_id=aws_api_gateway_rest_api.this.idstage_name="prod"}# Throttling for default methods by setting method_path to '*/*'resource"aws_api_gateway_method_settings""this"{rest_api_id=aws_api_gateway_rest_api.this.idstage_name=aws_api_gateway_stage.this.stage_namemethod_path="*/*"settings{throttling_burst_limit=1000throttling_rate_limit=10}}
Remark: this is currently not supported in AWS CDK as an L2 construct at the moment.
1 2 3 4 5 6 7 8 9101112131415161718192021
import{CfnStage,HttpApi}from'@aws-cdk/aws-apigatewayv2';exportclassMyStackextendscdk.Stack{constructor(scope:cdk.Construct,id:string,props?:cdk.StackProps){super(scope,id,props);constmyApi=newHttpApi(scope,'MyApi');// Throttling for default methods by setting method_path to '*/* using escape hatch.// See https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html#cfn_layer_resource// for more information.constdefaultStage=myApi.defaultStage.node.defaultChildasCfnStage;defaultStage.defaultRouteSettings={throttlingBurstLimit=1000,throttlingRateLimit=10,};}}