Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Terraform の例

最終更新 Markdown で表示Agent セットアップ

次の例は、Terraform でゾーンに 1 件の Cache Rule を定義します。このルールは複数のキャッシュ設定を行い、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 を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。

次の例は Vary を設定します。AcceptAccept-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 を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。

Cloudflare で Terraform を使う方法の詳細は、Terraform を参照してください。

役に立ちましたか?