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.
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,};}}