Home

Awesome

serverless-apigateway-route-settings

serverless CircleCI npm contributions welcome

About

A <a href="https://serverless.com/" target="_blank">Serverless Framework</a> Plugin which helps you configure route specific variables, such as throttling rate limits, detailed metrics etc (see <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigatewayv2-stage-routesettings.html" target="_blank">CloudFormation RouteSettings</a>) for Api Gateway v2 (HTTP). Also allows defaults to be set in the custom attribute of your serverless.yml.

Supported RouteSettings

ApiGateway v2 seems to only accept the following RouteSettings for Api Gateway v2 (HTTP):

Get Started

npm install serverless-apigateway-route-settings

or

yarn add serverless-apigateway-route-settings

Edit your serverless.yml to use this plugin:

plugins:
  - serverless-apigateway-route-settings

Next, edit your serverless.yml for <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html#cfn-apigatewayv2-stage-defaultroutesettings" target="_blank">DefaultRouteSettings</a>. What you enter here will be the default for each route in the stage.

custom:
  routeSettings:
    burstLimit: 200
    rateLimit: 400
    detailedMetricsEnabled: true

You can override the default route settings/account defaults by configuring at the route level. for example:

functions:
  hello:
    handler: src/throttle_me.handler
    events:
      - httpApi:
          path: /hello
          method: GET
          routeSettings:
            rateLimit: 10
            burstLimit: 5
            detailedMetricsEnabled: false

Caveats

Example serverless.yml

service: example

frameworkVersion: '2'

plugins:
  - serverless-apigateway-route-settings

custom: 
  routeSettings:
    detailedMetricsEnabled: true
    rateLimit: 200
    burstLimit: 30

provider:
  name: aws
  runtime: nodejs12.x

functions:
  # Inherits the default route settings.
  hello:
    handler: src/helloWorld.handler
    events:
      - httpApi:
          path: /hello
          method: GET

  # Overrides the default throttle rate limits.
  lowerRateLimit:
    handler: src/lowerRateLimit.handler
    events:
      - httpApi:
          path: /throttle
          method: GET
          routeSettings:
            rateLimit: 10
            burstLimit: 3

What will be added to your CloudFormation template?

Issues

If you encounter any bugs, please let me know here, and I will aim to fix them soon :slightly_smiling_face:. Contributions are welcome, feel free to make a pull request into the develop branch.