コンテンツにスキップ

レート制限ルール

このページでは、Terraformを使用してゾーン内にレート制限ルールを作成する例を提供します。

レート制限ルールに関する詳細は、Cloudflare WAFドキュメントのレート制限ルールを参照してください。

始める前に

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

The Terraform configurations provided in this page need the zone ID (or account ID) of the zone/account where you will deploy rulesets.

  • To retrieve the list of accounts you have access to, including their IDs, use the List accounts operation.
  • To retrieve the list of zones you have access to, including their IDs, use the List zones operation.

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

Terraform assumes that it has complete control over account and zone rulesets. If you already have rulesets configured in your account or zone, do one of the following:

  • Import existing rulesets to Terraform using the cf-terraforming tool. Recent versions of the tool can generate resource definitions for existing rulesets and import their configuration to Terraform state.
  • Start from scratch by deleting existing rulesets (account and zone rulesets with "kind": "root" and "kind": "zone", respectively) and then defining your rulesets configuration in Terraform.

レート制限ルールを作成する

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

resource "cloudflare_ruleset" "zone_rl" {
zone_id = "<ZONE_ID>"
name = "私のゾーンのレート制限"
description = ""
kind = "zone"
phase = "http_ratelimit"
rules {
action = "block"
ratelimit {
characteristics = ["cf.colo.id", "ip.src"]
period = 60
requests_per_period = 100
mitigation_timeout = 600
}
expression = "(http.request.uri.path matches \"^/api/\")"
description = "私のレート制限ルール"
enabled = true
}
}

To create another レート制限ルール, add a new rules object to the same cloudflare_ruleset resource.


アカウントレベルの例の構成

この例では、IDが<ACCOUNT_ID>のアカウントにおいて、/api/パスのトラフィックを設定されたレートを超えてブロックする単一のレート制限ルールを持つカスタムルールセットを定義します。2つ目のcloudflare_rulesetリソースは、example.comに向けられたトラフィックのためにカスタムルールセットを展開するexecuteルールを定義します。

resource "cloudflare_ruleset" "account_rl" {
account_id = <ACCOUNT_ID>
name = "API用のレート制限ルール"
description = ""
kind = "custom"
phase = "http_ratelimit"
rules {
action = "block"
ratelimit {
characteristics = ["cf.colo.id", "ip.src"]
period = 60
requests_per_period = 100
mitigation_timeout = 600
}
expression = "http.request.uri.path contains \"/api/\""
description = "APIルール"
enabled = true
}
}
# 'http_ratelimit'フェーズのアカウントレベルのエントリポイントルールセット
resource "cloudflare_ruleset" "account_rl_entrypoint" {
account_id = <ACCOUNT_ID>
name = "アカウントレベルのレート制限"
description = ""
kind = "root"
phase = "http_ratelimit"
depends_on = [cloudflare_ruleset.account_rl]
rules {
# レート制限ルールを含む以前に定義されたカスタムルールセットを展開
action = "execute"
action_parameters {
id = cloudflare_ruleset.account_rl.id
}
expression = "cf.zone.name eq \"example.com\" and cf.zone.plan eq \"ENT\""
description = "RLルールを持つカスタムルールセットを展開"
enabled = true
}
}

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

この例では、IDが<ZONE_ID>のゾーンに次の条件を持つレート制限ルールを作成します:

  • レスポンスフィールド(http.response.code)を含むカスタムカウント式。
  • レート制限されたリクエストのためのカスタムJSONレスポンス。
resource "cloudflare_ruleset" "zone_rl_custom_response" {
zone_id = "<ZONE_ID>"
name = "私のゾーンの高度なレート制限ルール"
description = ""
kind = "zone"
phase = "http_ratelimit"
rules {
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)"
}
expression = "http.host eq \"www.example.com\" and (http.request.uri.path matches \"^/status/\")"
description = "404レスポンスの閾値を超えた場合、www.example.comへのリクエストをレート制限"
enabled = true
}
}

To create another レート制限ルール, add a new rules object to the same cloudflare_ruleset resource.