umotif-public
terraform-aws-waf-webaclv2
HCL

Terraform module to configure WAF V2 Web ACL with managed rules for Application Load Balancer

Last updated Mar 18, 2026
146
Stars
124
Forks
24
Issues
0
Stars/day
Attention Score
41
Language breakdown
HCL 95.9%
Go 4.0%
Makefile 0.1%
Files click to expand
README

GitHub release (latest by date)

terraform-aws-waf-webaclv2

Terraform module to configure WAF Web ACL V2 for Application Load Balancer or Cloudfront distribution.

Supported WAF v2 components:

  • The module supports all AWS-managed rules defined in this AWS documentation.
  • Associating WAFv2 ACL with one or more Application Load Balancers (ALB)
  • Blocking IP Sets
  • Rate limiting IPs (and optional scope down statements)
  • Byte Match statements
  • Geo set statements
  • Logical Statements (AND, OR, NOT)
  • Size constraint statements
  • Label Match statements
  • Regex Match statements
  • Regex Pattern Match statements
  • Custom responses
  • Attach Custom Rule Groups

Terraform versions

Terraform 0.13+ Pin module version to ~> 4.0. Submit pull requests to the main branch.

Usage

Please pin down the version of this module to the exact version.

If referring directly to the code instead of a pinned version, take note that from release 4.0.0 all future changes will only be made to the main branch.

module "waf" {
  source = "umotif-public/waf-webaclv2/aws"
  version = "~> 4.0.0"

name_prefix = "test-waf-setup" alb_arn = module.alb.arn

scope = "REGIONAL"

createalbassociation = true

allowdefaultaction = true # set to allow if not specified

visibility_config = { metric_name = "test-waf-setup-waf-main-metrics" }

rules = [ { name = "AWSManagedRulesCommonRuleSet-rule-1" priority = "1"

override_action = "none"

visibility_config = { metric_name = "AWSManagedRulesCommonRuleSet-metric" }

managedrulegroup_statement = { name = "AWSManagedRulesCommonRuleSet" vendor_name = "AWS" ruleactionoverrides = [ { actiontouse = { count = {} }

name = "SizeRestrictions_QUERYSTRING" }, { actiontouse = { count = {} }

name = "SizeRestrictions_BODY" }, { actiontouse = { count = {} }

name = "GenericRFI_QUERYARGUMENTS" } ] } }, { name = "AWSManagedRulesKnownBadInputsRuleSet-rule-2" priority = "2"

override_action = "count"

visibility_config = { metric_name = "AWSManagedRulesKnownBadInputsRuleSet-metric" }

managedrulegroup_statement = { name = "AWSManagedRulesKnownBadInputsRuleSet" vendor_name = "AWS" } }, { name = "AWSManagedRulesPHPRuleSet-rule-3" priority = "3"

override_action = "none"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "AWSManagedRulesPHPRuleSet-metric" sampledrequestsenabled = false }

managedrulegroup_statement = { name = "AWSManagedRulesPHPRuleSet" vendor_name = "AWS" } }, ### Byte Match Rule example # Refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2webacl#byte-match-statement # for all of the options available. # Additional examples available in the examples directory { name = "ByteMatchRule-4" priority = "4"

action = "count"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "ByteMatchRule-metric" sampledrequestsenabled = false }

bytematchstatement = { fieldtomatch = { uri_path = "{}" } positionalconstraint = "STARTSWITH" search_string = "/path/to/match" priority = 0 type = "NONE" } }, ### Geo Match Rule example { name = "GeoMatchRule-5" priority = "5"

action = "allow"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "GeoMatchRule-metric" sampledrequestsenabled = false }

geomatchstatement = { country_codes = ["NL", "GB", "US"] } }, ### IP Set Rule example { name = "IpSetRule-6" priority = "6"

action = "allow"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "IpSetRule-metric" sampledrequestsenabled = false }

ipsetreference_statement = { arn = "arn:aws:wafv2:eu-west-1:111122223333:regional/ipset/ip-set-test/a1bcdef2-1234-123a-abc0-1234a5bc67d8" } }, ### IP Rate Based Rule example { name = "IpRateBasedRule-7" priority = "7"

action = "block"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "IpRateBasedRule-metric" sampledrequestsenabled = false }

ratebasedstatement = { limit = 100 aggregatekeytype = "IP" # Optional scopedownstatement to refine what gets rate limited scopedownstatement = { not_statement = { bytematchstatement = { fieldtomatch = { uri_path = "{}" } positionalconstraint = "STARTSWITH" search_string = "/path/to/match" priority = 0 type = "NONE" } } } } }, ### NOT rule example (can be applied to bytematch, geomatch, and ip_set rules) { name = "NotByteMatchRule-8" priority = "8"

action = "count"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "NotByteMatchRule-metric" sampledrequestsenabled = false }

not_statement = { bytematchstatement = { fieldtomatch = { uri_path = "{}" } positionalconstraint = "STARTSWITH" search_string = "/path/to/match" priority = 0 type = "NONE" } } }, ### Regex Match Rule example { name = "RegexMatchRule-9" priority = "9"

action = "allow"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "RegexMatchRule-metric" sampledrequestsenabled = false }

bytematchstatement = { fieldtomatch = { uri_path = "{}" } regex_string = "/foo/" priority = 0 type = "NONE" } ### Attach Custom Rule Group example { name = "CustomRuleGroup-1" priority = "9"

override_action = "none"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "CustomRuleGroup-metric" sampledrequestsenabled = false }

rulegroupreference_statement = { arn = "arn:aws:wafv2:eu-west-1:111122223333:regional/rulegroup/rulegroup-test/a1bcdef2-1234-123a-abc0-1234a5bc67d8" } ### Regex Match Rule example { name = "RegexMatchRule-9" priority = "9"

action = "allow"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "RegexMatchRule-metric" sampledrequestsenabled = false }

bytematchstatement = { fieldtomatch = { uri_path = "{}" } regex_string = "/foo/" priority = 0 type = "NONE" } ### Attach Custom Rule Group example { name = "CustomRuleGroup-1" priority = "9"

override_action = "none"

visibility_config = { cloudwatchmetricsenabled = false metric_name = "CustomRuleGroup-metric" sampledrequestsenabled = false }

rulegroupreference_statement = { arn = "arn:aws:wafv2:eu-west-1:111122223333:regional/rulegroup/rulegroup-test/a1bcdef2-1234-123a-abc0-1234a5bc67d8" } }, ### Size constraint Rule example # Refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2webacl#size-constraint-statement # for all of the options available. # Additional examples available in the examples directory { name = "BodySizeConstraint" priority = 0 sizeconstraintstatement = { fieldtomatch = { body = "{}" } comparison_operator = "GT" size = 8192 priority = 0 type = "NONE" }

action = "count"

visibility_config = { cloudwatchmetricsenabled = true metric_name = "BodySizeConstraint" sampledrequestsenabled = true } }, ### Regex Pattern Set Reference Rule example # Refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2webacl#regex-pattern-set-reference-statement # for all of the options available. # Additional examples available in the examples directory { name = "MatchRegexRule-1" priority = "1"

action = "none"

visibility_config = { cloudwatchmetricsenabled = true metric_name = "RegexBadBotsUserAgent-metric" sampledrequestsenabled = false }

# You need to previously create you regex pattern # Refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2regexpattern_set # for all of the options available. regexpatternsetreferencestatement = { arn = awswafv2regexpatternset.example.arn fieldtomatch = { single_header = { name = "user-agent" } } priority = 0 type = "LOWERCASE" # The text transformation type } } ]

tags = { "Name" = "test-waf-setup" "Env" = "test" } }

Cloudfront configuration

provider "aws" {
  alias = "us-east"

version = ">= 4.44.0" region = "us-east-1" }

module "waf" { providers = { aws = aws.us-east }

source = "umotif-public/waf-webaclv2/aws" version = "~> 4.0.0"

name_prefix = "test-waf-setup-cloudfront" scope = "CLOUDFRONT" createalbassociation = false ... }

Logging configuration

When you enable logging configuration for WAFv2. Remember to follow the naming convention defined in .

Importantly, make sure that Amazon Kinesis Data Firehose is using a name starting with the prefix aws-waf-logs-.

Examples

Authors

Module managed by:

Requirements

| Name | Version | |------|---------| | terraform | >= 1.0.2 | | aws | >= 4.52.0 |

Providers

| Name | Version | |------|---------| | aws | >= 4.52.0 |

Modules

No modules.

Resources

| Name | Type | |------|------| | awswafv2webacl.main | resource | | awswafv2webaclassociation.alb_list | resource | | awswafv2webaclassociation.main | resource | | awswafv2webaclloggingconfiguration.main | resource |

Inputs

| Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | alb\_arn | Application Load Balancer ARN | string | "" | no | | alb\arn\list | Application Load Balancer ARN list | list(string) | [] | no | | allow\default\action | Set to true for WAF to allow requests by default. Set to false for WAF to block requests by default. | bool | true | no | | create\alb\association | Whether to create alb association with WAF web acl | bool | true | no | | create\logging\configuration | Whether to create logging configuration in order start logging from a WAFv2 Web ACL to Amazon Kinesis Data Firehose. | bool | false | no | | custom\response\bodies | Custom response bodies to be referenced on a per rule basis. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2webacl#custom-response-body |

list(object({
key = string
content = string
content_type = string
}))
| [] | no | | description | A friendly description of the WebACL | string | null | no | | enabled | Whether to create the resources. Set to false to prevent the module from creating any resources | bool | true | no | | log\destination\configs | The Amazon Kinesis Data Firehose Amazon Resource Name (ARNs) that you want to associate with the web ACL. Currently, only 1 ARN is supported. | list(string) | [] | no | | logging\_filter | A configuration block that specifies which web requests are kept in the logs and which are dropped. You can filter on the rule action and on the web request labels that were applied by matching rules during web ACL evaluation. | any | {} | no | | name\_prefix | Name prefix used to create resources. | string | n/a | yes | | redacted\fields | The parts of the request that you want to keep out of the logs. Up to 100 redactedfields blocks are supported. | any | [] | no | | rules | List of WAF rules. | any | [] | no | | scope | Specifies whether this is for an AWS CloudFront distribution or for a regional application. Valid values are CLOUDFRONT or REGIONAL. To work with CloudFront, you must also specify the region us-east-1 (N. Virginia) on the AWS provider. | string | "REGIONAL" | no | | tags | A map of tags (key-value pairs) passed to resources. | map(string) | {} | no | | visibility\config | Visibility config for WAFv2 web acl. https://www.terraform.io/docs/providers/aws/r/wafv2web_acl.html#visibility-configuration | map(string) | {} | no |

Outputs

| Name | Description | |------|-------------| | web\acl\arn | The ARN of the WAFv2 WebACL. | | web\acl\assoc\acl\arn | The ARN of the Web ACL attached to the Web ACL Association | | web\acl\assoc\alb\list\acl\arn | The ARN of the Web ACL attached to the Web ACL Association for the alb\_list resource | | web\acl\assoc\alb\list\id | The ID of the Web ACL Association for the alb\list resource | | web\acl\assoc\alb\list\resource\arn | The ARN of the ALB attached to the Web ACL Association for the alb\_list resource | | web\acl\assoc\_id | The ID of the Web ACL Association | | web\acl\assoc\resource\arn | The ARN of the ALB attached to the Web ACL Association | | web\acl\capacity | The web ACL capacity units (WCUs) currently being used by this web ACL. | | web\acl\id | The ID of the WAFv2 WebACL. | | web\acl\logging\configuration\id | The ID of the Web ACL logging configuration resource | | web\acl\name | The name of the WAFv2 WebACL. | | web\acl\rule\_names | List of created rule names | | web\acl\visibility\config\name | The web ACL visibility config name |

License

See LICENSE for full details.

Pre-commit hooks & Golang for Terratest

Install dependencies

Terratest

We are using Terratest to run tests on this module.

brew install go

Change to the test directory

cd test

Get dependencies

go mod download

Run tests

go test -v -timeout 30m

MacOS

brew install pre-commit terraform-docs tflint

brew tap git-chglog/git-chglog brew install git-chglog

🔗 More in this category

© 2026 GitRepoTrend · umotif-public/terraform-aws-waf-webaclv2 · Updated daily from GitHub