Review AWS WAF protection for API Gateway

Apply appropriate WAF rules to REST API stages that need request filtering.

Description

To filter a REST API with AWS WAF, associate a web ACL with the API Gateway stage you want to protect.

Potential impact

Without the filters or rate limits required for that stage, unwanted traffic can reach the backend.

Remediation

Use a REGIONAL WAFv2 web ACL in the same Region. Choose managed or custom rules and exceptions, then use Count to check their effect on legitimate traffic before enabling blocking. Review allowed and blocked requests. Keep API authentication, authorization and security fixes in place.

Examples

Supply the REST API, deployment, web ACL and logging configuration separately.

Before

hcl
resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "example"
}

After

hcl
resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "example"
}

resource "aws_wafv2_web_acl_association" "association" {
  resource_arn = aws_api_gateway_stage.example.arn
  web_acl_arn  = aws_wafv2_web_acl.foo.arn
}

The second excerpt sets the example stage ARN and the web ACL ARN on aws_wafv2_web_acl_association. After deployment, verify which ACL is attached to that stage.

References