Skip to content

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

Terraform でレート制限ルールを設定する

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

このページでは、Terraform を使ってゾーンまたはアカウントに レート制限ルール を作成する例を紹介します。

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

始める前に

必要なアカウント ID またはゾーン ID を取得する

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

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

既存のルールセットをインポートまたは削除する

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

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

ゾーンレベルでレート制限ルールを作成する

この例では、ID が <ZONE_ID> のゾーンにレート制限ルールを作成し、設定したレートを超えるトラフィックをブロックします。

必要な API トークン権限

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

  • Zone WAF Write

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

resource "cloudflare_ruleset" "zone_rl" {
  zone_id     = var.cloudflare_zone_id
  name        = "Rate limiting for my zone"
  description = ""
  kind        = "zone"
  phase       = "http_ratelimit"

  rules = [{
    ref         = "rate_limit_api_requests_ip"
    description = "Rate limit API requests by IP"
    expression  = "(http.request.uri.path matches \"^/api/\")"
    action      = "block"
    ratelimit = {
      characteristics     = ["cf.colo.id", "ip.src"]
      period              = 60
      requests_per_period = 100
      mitigation_timeout  = 600
    }
  }]
}
resource "cloudflare_ruleset" "zone_rl" {
  zone_id     = "<ZONE_ID>"
  name        = "Rate limiting for my zone"
  description = ""
  kind        = "zone"
  phase       = "http_ratelimit"

  rules {
    ref         = "rate_limit_api_requests_ip"
    description = "Rate limit API requests by IP"
    expression  = "(http.request.uri.path matches \"^/api/\")"
    action      = "block"
    ratelimit {
      characteristics = ["cf.colo.id", "ip.src"]
      period = 60
      requests_per_period = 100
      mitigation_timeout = 600
    }
  }
}

別の rate limiting rule を作成するには、同じ cloudflare_ruleset リソースに新しい rules オブジェクトを追加します。


アカウントレベルでレート制限ルールを作成する

この例では、ID が <ACCOUNT_ID> のアカウントに、/api/ パスで設定レートを超えるトラフィックをブロックするレート制限ルールを 1 つ含む カスタムルールセット を定義します。2 つ目の cloudflare_ruleset リソースは、example.com 向けトラフィックにこのカスタムルールセットをデプロイする execute ルールを定義します。

必要な API トークン権限

次の トークン権限 がすべて必要です。

  • Account WAF Write
  • Account Rulesets Write

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

resource "cloudflare_ruleset" "account_rl" {
  account_id  = var.cloudflare_account_id
  name        = "Rate limiting rules for APIs"
  description = ""
  kind        = "custom"
  phase       = "http_ratelimit"

  rules = [{
    ref         = "rate_limit_api_ip"
    description = "Rate limit API requests by IP"
    expression  = "http.request.uri.path contains \"/api/\""
    action      = "block"
    ratelimit = {
      characteristics     = ["cf.colo.id", "ip.src"]
      period              = 60
      requests_per_period = 100
      mitigation_timeout  = 600
    }
  }]
}

# Account-level entry point ruleset for the 'http_ratelimit' phase
resource "cloudflare_ruleset" "account_rl_entrypoint" {
  account_id  = var.cloudflare_account_id
  name        = "Account-level rate limiting"
  description = ""
  kind        = "root"
  phase       = "http_ratelimit"

  depends_on = [cloudflare_ruleset.account_rl]

  rules = [{
    # Deploy the previously defined custom ruleset containing a rate limiting rule
    ref         = "deploy_rate_limit_example_com"
    description = "Deploy custom ruleset with RL rule"
    expression  = "cf.zone.name eq \"example.com\" and cf.zone.plan eq \"ENT\""
    action      = "execute"
    action_parameters = {
      id = cloudflare_ruleset.account_rl.id
    }
  }]
}
resource "cloudflare_ruleset" "account_rl" {
  account_id  = "<ACCOUNT_ID>"
  name        = "Rate limiting rules for APIs"
  description = ""
  kind        = "custom"
  phase       = "http_ratelimit"

  rules {
    ref         = "rate_limit_api_ip"
    description = "Rate limit API requests by IP"
    expression  = "http.request.uri.path contains \"/api/\""
    action      = "block"
    ratelimit {
      characteristics     = ["cf.colo.id", "ip.src"]
      period              = 60
      requests_per_period = 100
      mitigation_timeout  = 600
    }
  }
}

# Account-level entry point ruleset for the 'http_ratelimit' phase
resource "cloudflare_ruleset" "account_rl_entrypoint" {
  account_id  = "<ACCOUNT_ID>"
  name        = "Account-level rate limiting"
  description = ""
  kind        = "root"
  phase       = "http_ratelimit"

  depends_on = [cloudflare_ruleset.account_rl]

  rules {
    # Deploy the previously defined custom ruleset containing a rate limiting rule
    ref         = "deploy_rate_limit_example_com"
    description = "Deploy custom ruleset with RL rule"
    expression  = "cf.zone.name eq \"example.com\" and cf.zone.plan eq \"ENT\""
    action      = "execute"
    action_parameters {
      id = cloudflare_ruleset.account_rl.id
    }
  }
}

別の rate limiting rule を作成するには、同じ cloudflare_ruleset リソースに新しい rules オブジェクトを追加します。


高度なレート制限ルールを作成する

この例では、ID が <ZONE_ID> のゾーンに、次の内容のレート制限ルールを作成します。

  • レスポンスフィールド(http.response.code)を含むカスタムカウント式
  • レート制限されたリクエスト向けのカスタム JSON レスポンス

必要な API トークン権限

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

  • Zone WAF Write

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

resource "cloudflare_ruleset" "zone_rl_custom_response" {
  zone_id     = var.cloudflare_zone_id
  name        = "Advanced rate limiting rule for my zone"
  description = ""
  kind        = "zone"
  phase       = "http_ratelimit"

  rules = [{
    ref         = "rate_limit_example_com_status_404"
    description = "Rate limit requests to www.example.com when exceeding the threshold of 404 responses on /status/"
    expression  = "http.host eq \"www.example.com\" and (http.request.uri.path matches \"^/status/\")"
    action      = "block"
    action_parameters = {
      response = {
        status_code  = 429
        content      = "{\"response\": \"block\"}"
        content_type = "application/json"
      }
    }
    ratelimit = {
      characteristics     = ["ip.src", "cf.colo.id"]
      period              = 10
      requests_per_period = 5
      mitigation_timeout  = 30
      counting_expression = "(http.host eq \"www.example.com\") and (http.request.uri.path matches \"^/status/\") and (http.response.code eq 404)"
    }
  }]
}
resource "cloudflare_ruleset" "zone_rl_custom_response" {
  zone_id     = "<ZONE_ID>"
  name        = "Advanced rate limiting rule for my zone"
  description = ""
  kind        = "zone"
  phase       = "http_ratelimit"

  rules {
    ref         = "rate_limit_example_com_status_404"
    description = "Rate limit requests to www.example.com when exceeding the threshold of 404 responses on /status/"
    expression  = "http.host eq \"www.example.com\" and (http.request.uri.path matches \"^/status/\")"
    action      = "block"
    action_parameters {
      response {
        status_code  = 429
        content      = "{\"response\": \"block\"}"
        content_type = "application/json"
      }
    }
    ratelimit {
      characteristics     = ["ip.src", "cf.colo.id"]
      period              = 10
      requests_per_period = 5
      mitigation_timeout  = 30
      counting_expression = "(http.host eq \"www.example.com\") and (http.request.uri.path matches \"^/status/\") and (http.response.code eq 404)"
    }
  }
}

別の rate limiting rule を作成するには、同じ cloudflare_ruleset リソースに新しい rules オブジェクトを追加します。


役に立ちましたか?