본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Terraform 예시

원문 보기 위키 갱신

이 페이지에서는 Terraform을 사용해 Cloudflare 캐시 규칙(Cache Rule)을 만드는 방법을 코드 예시로 알려드려요. 존에 대한 캐시 규칙 하나를 정의하고, 여러 캐시 설정과 커스텀 캐시 키를 구성하는 방법을 다룬답니다.

출처: 문서

본문

다음 예시는 Terraform을 사용해 존(zone)에 대한 캐시 규칙 하나를 정의해요. 이 규칙은 example.net으로 향하는 들어오는 요청에 대해 여러 캐시 설정을 구성하고 커스텀 캐시 키를 설정해요.

Terraform cloudflare_ruleset 리소스

# 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
      }
    }
  ]
}

Terraform을 사용할 때 ref 필드를 사용하면 업데이트를 거쳐도 안정적인 규칙 ID를 얻을 수 있어요. 이 필드를 추가하면 변경 시 Terraform이 규칙을 다시 생성하지 않아요. 자세한 내용은 Terraform 문서의 문제 해결을 참고하세요.

다음 예시는 Vary를 구성해요. Accept와 Accept-Language를 정규화하고, 오리진 Vary 응답의 다른 모든 헤더에 대해서는 캐시를 우회해요.

Terraform 예시: 예상 Vary 응답 캐시하기

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"
              ]
            }
          }
        }
      }
    }
  ]
}

Terraform을 사용할 때 ref 필드를 사용하면 업데이트를 거쳐도 안정적인 규칙 ID를 얻을 수 있어요. 이 필드를 추가하면 변경 시 Terraform이 규칙을 다시 생성하지 않아요. 자세한 내용은 Terraform 문서의 문제 해결을 참고하세요.

Cloudflare와 함께 Terraform을 사용하는 추가 지침은 Terraform 문서를 참고하세요.

더 알아보기 (Learn more)

Terraform을 이용한 Cloudflare 규칙 관리의 자세한 내용은 Cloudflare의 Terraform 문서를 함께 참고하면 좋아요.