Add Throttle Limits to API Gateway

Throttling helps in preventing your backend services from being overwhelmed by too many requests at once. This is particularly important if your backend services have limited capacity and can only handle a certain number of requests per second.

Without throttling, a single client could potentially consume all available resources, leaving others with degraded service or no service at all.

What we'll be doing

  1. Review our existing module
  2. Configure throttle limits

Review our existing module

The first step is clone the Nitric repository and examine how the Nitric Terrraform provider provisions an API Gateway.

git clone https://github.com/nitrictech/nitric
cd nitric

The AWS API module in the default Terraform provider performs the following tasks:

  1. Defines an HTTP API Gateway with specified name, protocol, and API specification.
  2. Sets up a "$default" deployment stage with automatic deployment enabled.
  3. Allows the API Gateway to invoke specified Lambda functions.
  4. Looks up existing SSL certificates for specified domains.
  5. Configures custom domain names for the API Gateway using the retrieved SSL certificates.

To begin our customization, we will start adding configuration to this module.

Configure throttle limits

Update the `aws_apigatewayv2_stage.stage" in aws/deploytf/.nitric/modules/bucket/main.tf to add default_route_settings:

resource "aws_apigatewayv2_stage" "stage" {
  api_id      = aws_apigatewayv2_api.api_gateway.id
  name        = "$default"
  auto_deploy = true

  default_route_settings {
    throttling_burst_limit = 1000
    throttling_rate_limit  = 500
  }
}

Note: Full documentation can be found on the Terraform registry.

Building and using your updated provider

The Nitric project includes a make file that will build and install your provider as nitric/awstf@0.0.1 by default.

Run make install from the provider's root directory - nitric/cloud/aws

The provider can then be used directly in your project's stack file as follows.

# The nitric provider to use
provider: nitric/awstf@0.0.1

# The target aws region to deploy to
region: us-east-2

Note: If you don't have a stack file use the nitric stack new to create one.

You'll also need to enable beta-providers in your Nitric project by adding the following to your project's nitric.yaml file:

preview:
  - beta-providers

You can generate the Terraform code by running the following command:

nitric up

To deploy the application using Terraform, you can navigate into your Terraform stack directory and use the standard Terraform commands:

terraform init
terraform plan
terraform apply

You can examine your configuration by logging into the AWS console.

aws console api gateway throttle settings