Skip to content

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

Terraform で DDoS マネージドルールセットを設定する

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

このページでは、Terraform を使ってゾーンまたはアカウントの DDoS マネージドルールセット を設定する例を示します。次のシナリオを扱います。

DDoS マネージドルールセットは常に有効です。利用中の Cloudflare サービスによっては、動作を調整できます。

Cloudflare API を使う場合は、次のリソースを参照してください。

Rulesets API を使ったマネージドルールセットのデプロイと設定については、Ruleset Engine ドキュメントの マネージドルールセットを扱う を参照してください。

始める前に

必要なアカウント ID、ゾーン ID、マネージドルールセット ID を取得する

このページの Terraform 設定には、マネージドルールセットをデプロイするゾーンまたはアカウントのゾーン ID(またはアカウント ID)が必要です。

  • アクセスできるアカウントの一覧と ID を取得するには、List accounts オペレーションを使います。
  • アクセスできるゾーンの一覧と ID を取得するには、List zones オペレーションを使います。

Terraform でマネージドルールセットをデプロイするには、ルールセット ID が必要です。マネージドルールセットの ID を調べるには、List account rulesets オペレーションを使います。レスポンスには、既存のマネージドルールセットの説明と ID が含まれます。

(任意)既存のルールセットを削除してゼロから始める

Terraform は、アカウントとゾーンのルールセットを完全に管理していると想定します。アカウントまたはゾーンにルールセットが既にある場合は、次のいずれかを行ってください。

  • cf-terraforming ツールを使い、既存のルールセットを Terraform へインポート します。最近のバージョンでは、既存ルールセットのリソース定義を生成し、その設定を Terraform の状態へインポートできます。
  • 既存のルールセットを削除 してから(アカウントルールセットは "kind": "root"、ゾーンルールセットは "kind": "zone")、Terraform でルールセットの設定を定義し、最初から始めます。

例: HTTP DDoS Attack Protection を設定する

この例では、Terraform を使ってゾーンの HTTP DDoS Attack Protection マネージドルールセットを設定します。

必要な API トークン権限

次の トークン権限 のうち、少なくとも 1 つが必要です。

  • HTTP DDoS Managed Ruleset Write

cloudflare_ruleset リソースを設定します。

resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
  zone_id     = var.cloudflare_zone_id
  name        = "HTTP DDoS Attack Protection entry point ruleset"
  description = ""
  kind        = "zone"
  phase       = "ddos_l7"

  rules = [{
    action = "execute"
    action_parameters = {
      # Cloudflare L7 DDoS Attack Protection Ruleset
      id = "4d21379b4f9f4bb088e0729962c8b3cf"
      overrides = {
        action            = "block"
        sensitivity_level = "default"
        rules = [
          {
            # Adaptive DDoS Protection based on Locations (Available only to Enterprise zones with Advanced DDoS service)
            id                = "a8c6333711ff4b0a81371d1c444be2c3"
            sensitivity_level = "default"
            action            = "managed_challenge"
          },
          {
            # Adaptive DDoS Protection based on User-Agents (Available only to Enterprise zones with Advanced DDoS service)
            id                = "7709d496081e458899c1e3a6e4fe8e55"
            sensitivity_level = "default"
            action            = "managed_challenge"
          },
          {
            # HTTP requests causing a high number of origin errors.
            id                = "dd42da7baabe4e518eaf11c393596a9d"
            sensitivity_level = "default"
            action            = "managed_challenge"
          },
        ]
      }
    }
    expression  = "true"
    description = "Zone-wide HTTP DDoS Override"
    enabled     = true
  }]
}
resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
  zone_id     = "<ZONE_ID>"
  name        = "HTTP DDoS Attack Protection entry point ruleset"
  description = ""
  kind        = "zone"
  phase       = "ddos_l7"

  rules {
    action = "execute"
    action_parameters {
      # Cloudflare L7 DDoS Attack Protection Ruleset
      id = "4d21379b4f9f4bb088e0729962c8b3cf"
      overrides {
        action = "block"
        sensitivity_level = "default"
        rules {
          # Adaptive DDoS Protection based on Locations (Available only to Enterprise zones with Advanced DDoS service)
          id = "a8c6333711ff4b0a81371d1c444be2c3"
          sensitivity_level = "default"
          action = "managed_challenge"
        }
        rules {
          # Adaptive DDoS Protection based on User-Agents (Available only to Enterprise zones with Advanced DDoS service)
          id = "7709d496081e458899c1e3a6e4fe8e55"
          sensitivity_level = "default"
          action = "managed_challenge"
        }
        rules {
          # HTTP requests causing a high number of origin errors.
          id = "dd42da7baabe4e518eaf11c393596a9d"
          sensitivity_level = "default"
          action = "managed_challenge"
        }
      }
    }
    expression = "true"
    description = "Zone-wide HTTP DDoS Override"
    enabled = true
  }
}

HTTP DDoS Attack Protection の詳細は、HTTP DDoS Attack Protection マネージドルールセット を参照してください。

例: Network-layer DDoS Attack Protection を設定する

この例では、Terraform を使ってアカウントの Network-layer DDoS Attack Protection マネージドルールセットを設定し、オーバーライドで ID が のルールの感度レベルを low に変更します。

必要な API トークン権限

次の トークン権限 のうち、少なくとも 1 つが必要です。

  • L4 DDoS Managed Ruleset Write

cloudflare_ruleset リソースを設定します。

resource "cloudflare_ruleset" "account_level_network_ddos_config" {
  account_id  = var.cloudflare_account_id
  name        = "Network-layer DDoS Attack Protection entry point ruleset"
  description = ""
  kind        = "root"
  phase       = "ddos_l4"

  rules = [{
    ref         = "override_l7_ddos_ruleset_dst_ip"
    description = "Override the HTTP DDoS Attack Protection managed ruleset"
    expression  = "ip.dst in { 192.0.2.0/24 }"
    action      = "execute"
    action_parameters = {
      # Cloudflare L3/4 DDoS Attack Protection Ruleset
      id = "3b64149bfa6e4220bbbc2bd6db589552"
      overrides = {
        rules = [{
          # Rule: Generic high-volume UDP traffic flows.
          id                = "599dab0942ff4898ac1b7797e954e98b"
          sensitivity_level = "low"
        }]
      }
    }
  }]
}
resource "cloudflare_ruleset" "account_level_network_ddos_config" {
  account_id  = "<ACCOUNT_ID>"
  name        = "Network-layer DDoS Attack Protection entry point ruleset"
  description = ""
  kind        = "root"
  phase       = "ddos_l4"

  rules {
    ref         = "override_l7_ddos_ruleset_dst_ip"
    description = "Override the HTTP DDoS Attack Protection managed ruleset"
    expression  = "ip.dst in { 192.0.2.0/24 }"
    action      = "execute"
    action_parameters {
      # Cloudflare L3/4 DDoS Attack Protection Ruleset
      id = "3b64149bfa6e4220bbbc2bd6db589552"
      overrides {
        rules {
          # Rule: Generic high-volume UDP traffic flows.
          id                = "599dab0942ff4898ac1b7797e954e98b"
          sensitivity_level = "low"
        }
      }
    }
  }
}

Network-layer DDoS Attack Protection の詳細は、Network-layer DDoS Attack Protection マネージドルールセット を参照してください。


ユースケース: 大規模な HTTP DDoS 攻撃を緩和し、フラグされたトラフィックを監視する

次の例では、誤検知を心配しつつ、大規模な HTTP DDoS 攻撃からは守りたい、というお客様を想定します。HTTP DDoS 保護 の設定にある 2 つのルール(それぞれオーバーライドを 2 つ含む)は、次のように動作します。

  1. 感度レベル Low、アクション Block のルールを設定し、大規模な HTTP DDoS 攻撃を緩和します。
  2. 既定の感度レベル(High)、アクション Log のルールを設定し、DDoS 保護システムがフラグしたトラフィックを監視します。

ルールの順序は重要です。感度レベルが最も高いルールは、感度レベルが最も低いルールの後に置く必要があります。そうしないと評価されません。

必要な API トークン権限

次の トークン権限 のうち、少なくとも 1 つが必要です。

  • HTTP DDoS Managed Ruleset Write

cloudflare_ruleset リソースを設定します。

resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
  zone_id     = var.cloudflare_zone_id
  name        = "HTTP DDoS - Terraform managed"
  description = ""
  kind        = "zone"
  phase       = "ddos_l7"

  # The resource configuration contains two rules:
  #  1. The first rule has the lowest sensitivity level (highest threshold)
  #     and it will block attacks.
  #  2. The second rule has a higher sensitivity level (lower threshold) and
  #     will only apply a Log action.
  #
  # In practice, evaluation stops whenever a rule matches both the expression
  # and the threshold, so the rule order is important:
  #   - When the traffic rate is below the (low) threshold of the default
  #     sensitivity level ('High'), no rules match (no action is applied).
  #   - When the traffic rate is between the thresholds of the 'Low' and
  #     default ('High') sensitivity levels, the first rule does not match,
  #     but the second rule does (traffic gets logged).
  #   - When the traffic rate goes above the (high) threshold of the 'Low'
  #     sensitivity level, the first rule matches (traffic gets blocked).
  #
  # The DDoS protection systems will still apply mitigation actions to incoming
  # traffic when rates exceed the threshold of the _Essentially Off_ sensitivity
  # level.

  rules = [
    {
      ref         = "l7_ddos_block_traffic_low_threshold"
      description = "At the low sensitivity threshold, block the traffic"
      expression  = "true"
      action      = "execute"
      action_parameters = {
        # Cloudflare L7 DDoS Attack Protection Ruleset
        id = "4d21379b4f9f4bb088e0729962c8b3cf"
        overrides = {
          rules = [
            {
              # Rule: HTTP requests from known botnet (signature #4).
              id                = "29d170ba2f004cc787b1ac272c9e04e7"
              sensitivity_level = "low"
              action            = "block"
            },
            {
              # Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
              id                = "60a48054bbcf4014ac63c44f1712a123"
              sensitivity_level = "low"
              action            = "block"
            },
          ]
        }
      }
    },
    {
      ref         = "l7_ddos_log_default_threshold"
      description = "At the default sensitivity threshold, log to see if any legitimate traffic gets caught"
      expression  = "true"
      action      = "execute"
      action_parameters = {
        # Cloudflare L7 DDoS Attack Protection Ruleset
        id = "4d21379b4f9f4bb088e0729962c8b3cf"
        overrides = {
          rules = [
            {
              # Rule: HTTP requests from known botnet (signature #4).
              id                = "29d170ba2f004cc787b1ac272c9e04e7"
              sensitivity_level = "default"
              action            = "log"
            },
            {
              # Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
              id                = "60a48054bbcf4014ac63c44f1712a123"
              sensitivity_level = "default"
              action            = "log"
            },
          ]
        }
      }
    },
  ]
}
variable "zone_id" {
  default = "<ZONE_ID>"
}

resource "cloudflare_ruleset" "zone_level_http_ddos_config" {
  zone_id     = var.zone_id
  name        = "HTTP DDoS - Terraform managed"
  description = ""
  kind        = "zone"
  phase       = "ddos_l7"

  # The resource configuration contains two rules:
  #  1. The first rule has the lowest sensitivity level (highest threshold)
  #     and it will block attacks.
  #  2. The second rule has a higher sensitivity level (lower threshold) and
  #     will only apply a Log action.
  #
  # In practice, evaluation stops whenever a rule matches both the expression
  # and the threshold, so the rule order is important:
  #   - When the traffic rate is below the (low) threshold of the default
  #     sensitivity level ('High'), no rules match (no action is applied).
  #   - When the traffic rate is between the thresholds of the 'Low' and
  #     default ('High') sensitivity levels, the first rule does not match,
  #     but the second rule does (traffic gets logged).
  #   - When the traffic rate goes above the (high) threshold of the 'Low'
  #     sensitivity level, the first rule matches (traffic gets blocked).
  #
  # The DDoS protection systems will still apply mitigation actions to incoming
  # traffic when rates exceed the threshold of the _Essentially Off_ sensitivity
  # level.

  rules {
    ref         = "l7_ddos_block_traffic_low_threshold"
    description = "At the low sensitivity threshold, block the traffic"
    expression  = "true"
    action      = "execute"
    action_parameters {
      # Cloudflare L7 DDoS Attack Protection Ruleset
      id = "4d21379b4f9f4bb088e0729962c8b3cf"
      overrides {
        rules {
          # Rule: HTTP requests from known botnet (signature #4).
          id                = "29d170ba2f004cc787b1ac272c9e04e7"
          sensitivity_level = "low"
          action            = "block"
        }
        rules {
          # Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
          id                = "60a48054bbcf4014ac63c44f1712a123"
          sensitivity_level = "low"
          action            = "block"
        }
      }
    }
  }

  rules {
    ref         = "l7_ddos_log_default_threshold"
    description = "At the default sensitivity threshold, log to see if any legitimate traffic gets caught"
    expression  = "true"
    action      = "execute"
    action_parameters {
      # Cloudflare L7 DDoS Attack Protection Ruleset
      id = "4d21379b4f9f4bb088e0729962c8b3cf"
      overrides {
        rules {
          # Rule: HTTP requests from known botnet (signature #4).
          id                = "29d170ba2f004cc787b1ac272c9e04e7"
          sensitivity_level = "default"
          action            = "log"
        }
        rules {
          # Rule: HTTP requests with unusual HTTP headers or URI path (signature #16).
          id                = "60a48054bbcf4014ac63c44f1712a123"
          sensitivity_level = "default"
          action            = "log"
        }
      }
    }
  }
}

役に立ちましたか?