Rate limiting rules
This page provides an example of creating a rate limiting rule in a zone using Terraform.
For more information on rate limiting rules, refer to Rate limiting 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.
This example creates a rate limiting rule in zone with ID <ZONE_ID>
blocking traffic that exceeds the configured rate:
To create another rate limiting rule, add a new rules
object to the same cloudflare_ruleset
resource.
Account-level example configuration
This example defines a custom ruleset with a single rate limiting rule in account with ID <ACCOUNT_ID>
that blocks traffic for the /api/
path exceeding the configured rate. The second cloudflare_ruleset
resource defines an execute
rule that deploys the custom ruleset for traffic addressed at example.com
.
This example creates a rate limiting rule in zone with ID <ZONE_ID>
with:
- A custom counting expression that includes a response field (
http.response.code
). - A custom JSON response for rate limited requests.
To create another rate limiting rule, add a new rules
object to the same cloudflare_ruleset
resource.