Terraform 예시
Terraform 예시 (Terraform example)
다음 예시는 Terraform을 사용해 한 존에 대해 단일 Cache Response Rule을 정의해요. 이 규칙은 JavaScript 파일 응답에서 Set-Cookie와 ETag 헤더를 캐싱 전에 제거해요.
출처: 문서
본문
Terraform cloudflare_ruleset 리소스
cloudflare_ruleset 리소스# Cache Response Rule to strip headers from JS responses
resource "cloudflare_ruleset" "cache_response_rules_example" {
zone_id = "<ZONE_ID>"
name = "Cache Response Rules"
description = "Configure cache settings for origin responses"
kind = "zone"
phase = "http_response_cache_settings"
rules {
ref = "strip_js_headers"
description = "Strip caching headers from JS file responses"
expression = "http.request.uri.path.extension eq \"js\""
action = "set_cache_settings"
action_parameters {
strip_etags = true
strip_set_cookie = true
}
}
rules {
ref = "tag_api_responses"
description = "Tag API responses for targeted purging"
expression = "starts_with(http.request.uri.path, \"/api/\")"
action = "set_cache_tags"
action_parameters {
operation = "set"
values = ["api-response", "dynamic-content"]
}
}
rules {
ref = "cache_control_200"
description = "Set cache-control for successful responses"
expression = "http.response.code eq 200"
action = "set_cache_control"
action_parameters {
s_maxage {
operation = "set"
value = 86400
cloudflare_only = true
}
must_revalidate {
operation = "set"
}
}
}
}
Terraform을 사용할 때 ref 필드를 사용하면 업데이트 전반에 걸쳐 안정적인 규칙 ID를 얻을 수 있어요. 이 필드를 추가하면 변경 시 Terraform이 규칙을 다시 만들지 않게 돼요. 자세한 내용은 Terraform 문서의 Troubleshooting을 참고해요.
Cloudflare에서 Terraform을 사용하는 추가 지침은 Terraform을 참고해요.
더 알아보기 (Learn more)
Terraform의 cloudflare_ruleset 리소스로 Cache Response Rule을 선언해 JS 응답의 캐싱 헤더를 제거하고, API 응답에 캐시 태그를 달고, 성공 응답의 cache-control을 설정할 수 있어요. ref 필드로 규칙 ID를 안정적으로 유지하는 게 좋아요.