Terraform example (2)
Last updated Aug 25, 2026
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.
<details>
<summary>
Terraform <code>cloudflare_ruleset</code> resource
</summary>
# Cache rule configuring cache settings and defining custom cache keys
resource "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_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 = {
all = true
}
}
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 <code>ref</code> 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 <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.
</details>
The following example configures Vary. It normalizes Accept and Accept-Language, and bypasses cache for any other header in the origin Vary response.
<details>
<summary>
Terraform example: Cache expected Vary responses
</summary>
variable "zone_id" {
type = string
}
resource "cloudflare_ruleset" "cache_vary_example" {
zone_id = var.zone_id
name = "Set cache settings"
description = "Set cache settings for incoming requests"
kind = "zone"
phase = "http_request_cache_settings"
rules = [
{
ref = "cache_vary"
description = "Cache expected Vary responses"
expression = "(http.host eq \"example.com\")"
action = "set_cache_settings"
action_parameters = {
cache = true
vary = {
default = {
action = "bypass"
}
headers = {
"accept" = {
action = "normalize"
media_types = [
"text/html",
"application/json"
]
}
"accept-language" = {
action = "normalize"
languages = [
"en",
"fr",
"de"
]
}
}
}
}
}
]
}
Use the <code>ref</code> 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 <a href="https://developers.cloudflare.com/terraform/troubleshooting/rule-id-changes/#how-to-keep-the-same-rule-id-between-modifications">Troubleshooting</a> in the Terraform documentation.
</details>
For additional guidance on using Terraform with Cloudflare, refer to Terraform.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/cache/how-to/cache-rules/terraform-example/#page","headline":"Terraform example","description":"Create Cache Rules using Terraform.","url":"https://developers.cloudflare.com/cache/how-to/cache-rules/terraform-example/","inLanguage":"en","image":"https://developers.cloudflare.com/cache/how-to/cache-rules/terraform-example/og.png?v=2e1998f2e9c40d4f","dateModified":"2026-08-25","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Terraform"]}
description: Cache responses based on origin HTTP status codes. title: Cache by status code image: https://developers.cloudflare.com/cache/how-to/configure-cache-status-code/og.png?v=8b3328c399f252db
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cache/llms.txt
Use this file to discover all available pages before exploring further.
Docs