WAF custom rules
This page provides examples of creating WAF custom rules in a zone or account using Terraform. The examples cover the following scenarios:
-
Zone-level configurations:
-
Account-level configurations:
For more information on custom rules, refer to Custom rules in the Cloudflare WAF documentation.
The Terraform configurations provided in this page need the zone ID (or account ID) of the zone/account where you will deploy rulesets.
- To retrieve the list of accounts you have access to, including their IDs, use the List accounts operation.
- To retrieve the list of zones you have access to, including their IDs, use the List zones operation.
Terraform assumes that it has complete control over account and zone rulesets. If you already have rulesets configured in your account or zone, do one of the following:
- Import existing rulesets to Terraform using the
cf-terraforming
tool. Recent versions of the tool can generate resource definitions for existing rulesets and import their configuration to Terraform state. - Start from scratch by deleting existing rulesets (account and zone rulesets with
"kind": "root"
and"kind": "zone"
, respectively) and then defining your rulesets configuration in Terraform.
The following example configures a custom rule in the zone entry point ruleset for the http_request_firewall_custom
phase for zone with ID <ZONE_ID>
. The rule will block all traffic on non-standard HTTP(S) ports:
To create another custom rule, add a new rules
object to the same cloudflare_ruleset
resource.
The following example creates a custom ruleset in the account with ID <ACCOUNT_ID>
containing a single custom rule. This custom ruleset is then deployed using a separate cloudflare_ruleset
Terraform resource. If you do not deploy a custom ruleset, it will not execute.
The following configuration creates the custom ruleset with a single rule:
To create another custom rule in the custom ruleset, add a new rules
object to the same cloudflare_ruleset
resource.
The following configuration deploys the custom ruleset at the account level. It defines a dependency on the account_firewall_custom_ruleset
resource and uses the ID of the created custom ruleset in action_parameters
:
For more information on configuring and deploying custom rulesets, refer to Work with custom rulesets in the Ruleset Engine documentation.
The following configuration creates a custom ruleset with a single rule that checks for exposed credentials.
To create another rule, add a new rules
object to the same cloudflare_ruleset
resource.
The following configuration deploys the custom ruleset. It defines a dependency on the account_firewall_custom_ruleset_exposed_creds
resource and obtains the ID of the created custom ruleset: