// terraform-aws-waf

AWS WAFv2 Web ACLs with managed rules, rate limiting, geoblocking, Bot Control, IP sets, custom rules, and logging.

Terraform ≥ 1.0 AWS ≥ 4.0 MIT License

Executive Summary

This module creates and manages AWS WAFv2 Web ACLs with comprehensive protection capabilities. It supports AWS Managed Rule Groups (Common Rule Set, SQL injection, known bad inputs, admin protection, IP reputation, and more), AWS Bot Control with COMMON or TARGETED inspection, rate-based rules with IP aggregation and scope-down statements, IP set reference rules for allow/block lists, geo match rules for country-based blocking, and fully custom rules with byte match, geo match, and size constraint statements. Logging is supported to Kinesis Firehose, CloudWatch Logs, or S3 with field redaction. Web ACL associations attach to ALB, API Gateway, AppSync, and Cognito resources. Both REGIONAL and CLOUDFRONT scopes are supported.

Overview

6

Rule Types

Managed, Bot Control, Rate Limit, IP Set, Geo Match, Custom

4

Resources

Web ACL, Logging Config, CloudWatch Log Group, Web ACL Association

3

Log Destinations

Kinesis Firehose, CloudWatch Logs, S3 Bucket

10+

Managed Rule Groups

CRS, SQLi, Linux, Windows, PHP, WordPress, IP Reputation, Anonymous IP, Bot Control, ATP, ACFP

Architecture Diagram

Client RequestHTTP/S traffic
CloudFront / ALB / API GWProtected resource
WAFv2 Web ACLRule evaluation pipeline
Managed RulesCRS, SQLi, Bad Inputs
Bot ControlCOMMON / TARGETED
Rate LimitingIP-based throttling
IP Set RulesAllow / Block lists
Geo MatchCountry blocking
Custom RulesByte / Size match
ALLOWForward to app
BLOCK403 Forbidden
COUNTLog only
CloudWatch LogsAuto-created
Kinesis FirehoseStreaming
S3 BucketArchival

Component Breakdown

ComponentResourcePurpose
Web ACLaws_wafv2_web_aclCentral rule evaluation engine with configurable default action
Managed Rulesmanaged_rule_group_statementAWS-curated rules: CRS, SQLi, Known Bad Inputs, Admin Protection, IP Reputation
Bot ControlAWSManagedRulesBotControlRuleSetBot detection with COMMON (general) or TARGETED (advanced) inspection
Rate Limitingrate_based_statementIP-based request throttling with optional geo scope-down
IP Set Rulesip_set_reference_statementAllow/block based on IP set ARN references
Geo Matchgeo_match_statementCountry-code-based blocking/allowing
Custom Rulesbyte_match / size_constraint / geoCustom inspection of URI, query string, headers, body
Loggingaws_wafv2_web_acl_logging_configurationLog to Firehose, CloudWatch, or S3 with field redaction
Associationaws_wafv2_web_acl_associationAttach Web ACL to ALB, API Gateway, AppSync, Cognito

Data Flow

Client
ALB / CloudFront
WAF Rules (priority order)
Application

Rules are evaluated in priority order (lowest number first). The first matching rule determines the action (ALLOW, BLOCK, or COUNT). If no rule matches, the default action applies. All evaluations are logged to the configured destination with CloudWatch metrics enabled per-rule for visibility.

Security Controls

OWASP Top 10 Coverage

AWSManagedRulesCommonRuleSet provides protection against XSS, SQLi, SSRF, and other OWASP Top 10 threats

Log4j / Known Exploits

AWSManagedRulesKnownBadInputsRuleSet blocks Log4Shell and other known vulnerability patterns

Bot Mitigation

Bot Control with COMMON or TARGETED inspection levels for automated traffic management

DDoS Protection

Rate-based rules throttle abusive IPs, geo-blocking restricts traffic by country

Log Redaction

Sensitive fields (authorization headers, query strings) can be redacted from WAF logs

Per-Rule Metrics

CloudWatch metrics and sampled requests enabled on every rule for full observability

Industry Adaptation

IndustryConfiguration
E-CommerceCRS + SQLi rules, Bot Control (TARGETED), rate limiting on checkout, geo-blocking
Financial ServicesCRS + Admin Protection + ATP for account takeover, IP allowlists for partner APIs
SaaS / APIRate limiting per endpoint, Known Bad Inputs, custom rules for API abuse patterns
Media / ContentBot Control for scraping prevention, geo restrictions for licensing, ACFP for fake accounts
HealthcareCRS + Admin Protection, IP allowlists, log redaction for PHI fields

Production Readiness Checklist

Configuration Reference

Required Variables

VariableTypeDescription
namestringName of the WAFv2 Web ACL

Key Optional Variables

VariableDefaultDescription
scope"REGIONAL"REGIONAL or CLOUDFRONT
default_action"allow"Default action (allow or block)
managed_rule_groups[]List of AWS managed rule groups
enable_bot_controlfalseEnable Bot Control
bot_control_inspection_level"COMMON"COMMON or TARGETED
rate_limit_rules[]Rate-based rules
ip_set_rules[]IP set reference rules
geo_match_rules[]Geo match / country blocking rules
custom_rules[]Custom byte/size/geo rules
enable_loggingtrueEnable WAF logging
resource_arns[]ARNs of resources to associate

AWS Managed Rule Groups Reference

CategoryRule Group Name
BaselineAWSManagedRulesCommonRuleSet, AWSManagedRulesAdminProtectionRuleSet, AWSManagedRulesKnownBadInputsRuleSet
SQLi / LFIAWSManagedRulesSQLiRuleSet, AWSManagedRulesLinuxRuleSet, AWSManagedRulesUnixRuleSet
PlatformAWSManagedRulesWindowsRuleSet, AWSManagedRulesPHPRuleSet, AWSManagedRulesWordPressRuleSet
IP ReputationAWSManagedRulesAmazonIpReputationList, AWSManagedRulesAnonymousIpList
Bot / FraudAWSManagedRulesBotControlRuleSet, AWSManagedRulesATPRuleSet, AWSManagedRulesACFPRuleSet

Deployment

module "waf" {
  source = "github.com/kogunlowo123/terraform-aws-waf"

  name  = "my-web-acl"
  scope = "REGIONAL"

  managed_rule_groups = [
    {
      name            = "AWSManagedRulesCommonRuleSet"
      priority        = 10
      override_action = "none"
      excluded_rules  = []
    },
    {
      name            = "AWSManagedRulesKnownBadInputsRuleSet"
      priority        = 20
      override_action = "none"
      excluded_rules  = []
    }
  ]

  rate_limit_rules = [
    {
      name     = "global-rate-limit"
      priority = 100
      rate     = 2000
      action   = "block"
    }
  ]

  resource_arns = [aws_lb.main.arn]

  tags = { Environment = "production" }
}

Links

AWS WAFv2 Documentation
AWS Managed Rule Groups
AWS Bot Control
Terraform aws_wafv2_web_acl
OWASP Top 10