Review WAF protection for an API Gateway REST API

Apply web request filtering that fits the API’s exposure and requirements.

Description

Associate an AWS WAF web ACL with the API Gateway REST API stage that needs filtering. Choose managed, custom or rate-based rules for the API’s exposure and data.

Potential impact

Unwanted traffic can increase backend costs or delay legitimate requests.

Remediation

Use a REGIONAL web ACL in the same Region as the stage. Test the rules against legitimate traffic before blocking. Monitor WAF metrics and review request logs if logging is enabled on the web ACL. Retain API authentication, authorization and input validation.

Examples

The first task deploys my_api.yml to production. The second requires the API, stage and string03 web ACL to exist.

Before

yaml
- name: Setup AWS API Gateway
  community.aws.aws_api_gateway:
    swagger_file: my_api.yml
    stage: production
    endpoint_type: EDGE
    state: present

After

yaml
- name: Associate API Gateway stage with WAF
  community.aws.wafv2_resources:
    name: string03
    scope: REGIONAL
    state: present
    arn: "arn:aws:apigateway:region::/restapis/api-id/stages/production"

Set arn in wafv2_resources to the stage ARN using the actual REST API ID, Region and stage name. Even with an EDGE endpoint, this association targets the API stage in its Region.

References