Serverless Apigateway Route Settings

Configure Route Settings for HTTP API Gateway v2 (Throttling & Detailed Metrics)

serverless-apigateway-route-settings

serverless CircleCI npm contributions welcome

About

A Serverless Framework Plugin which helps you configure route specific variables, such as throttling rate limits, detailed metrics etc (see CloudFormation RouteSettings) 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):

  • ThrottlingBurstLimit
  • ThrottlingRateLimit
  • DetailedMetricsEnabled

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 DefaultRouteSettings. 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

  • Currently, if we are to deploy with default route settings specified, then remove them, they will persist, you MUST specify new default route settings. I aim to fix this in an update, will default to account levels.
  • Doesn't work with pre existing API Gateways, eg if they are existing and we simply add routes in the serverless.yml. It is possible to add this plugin to an existing api gateway which is handled by serverless however.

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?

  • DefaultRouteSettings will be added to your Stage.
  • RouteSettings will be added to your Stage.
  • The DependsOn attribute is edited for your Stage. We simply add any Route's with configured RouteSettings, to ensure the creation of the Routes before the Stage. Otherwise CloudFormation will error (as it tries to edit the RouteSettings for a Route that doesn't exist yet).

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.