Terraform example
The following example defines a single cache rule for a zone using Terraform. The rule configures several cache settings and sets a custom cache key for incoming requests addressed at example.net
.
Terraform cloudflare_ruleset
resource
cloudflare_ruleset
resource# Cache rule configuring cache settings and defining custom cache keysresource "cloudflare_ruleset" "cache_rules_example" { zone_id = "<ZONE_ID>" name = "Set cache settings" description = "Set cache settings for incoming requests" kind = "zone" phase = "http_request_cache_settings"
rules { ref = "cache_settings_custom_cache_key" description = "Set cache settings and custom cache key for example.net" expression = "(http.host eq \"example.net\")" action = "set_cache_settings" action_parameters { edge_ttl { mode = "override_origin" default = 60 status_code_ttl { status_code = 200 value = 50 } status_code_ttl { status_code_range { from = 201 to = 300 } value = 30 } } browser_ttl { mode = "respect_origin" } serve_stale { disable_stale_while_updating = true } respect_strong_etags = true cache_key { ignore_query_strings_order = false cache_deception_armor = true custom_key { query_string { exclude = ["*"] } header { include = ["habc", "hdef"] check_presence = ["habc_t", "hdef_t"] exclude_origin = true } cookie { include = ["cabc", "cdef"] check_presence = ["cabc_t", "cdef_t"] } user { device_type = true geo = false } host { resolved = true } } } origin_error_page_passthru = false } }}
Use the ref
field to get stable rule IDs across updates when using Terraform. Adding this field prevents Terraform from recreating the rule on changes. For more information, refer to Troubleshooting in the Terraform documentation.
For additional guidance on using Terraform with Cloudflare, refer to Terraform.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark