תיעוד 149
הגדרת כללי הגבלת קצב באמצעות Terraform
עודכן לאחרונה: 29 באפריל 2026
הדף הזה מציג דוגמאות ליצירת כללי הגבלת קצב באזור או בחשבון באמצעות Terraform.
אם אתם משתמשים ב-API של Cloudflare, עיינו במשאבים הבאים:
הערה
למידע נוסף על הגדרת הגרסה הקודמת של כללי הגבלת קצב ב-Terraform, עיינו במשאב cloudflare_rate_limit בתיעוד של Terraform.
#לפני שמתחילים
#השגת מזהי החשבון או האזור הנדרשים
ההגדרות של Terraform בדף הזה זקוקות למזהה האזור (או למזהה החשבון) של האזור או החשבון שבו תפרסו קבוצות כללים.
- כדי לאחזר את רשימת החשבונות שיש לכם גישה אליהם, כולל המזהים שלהם, השתמשו בפעולה List accounts.
- כדי לאחזר את רשימת האזורים שיש לכם גישה אליהם, כולל המזהים שלהם, השתמשו בפעולה List zones.
#ייבוא או מחיקה של קבוצות כללים קיימות
Terraform מניח שיש לו שליטה מלאה על קבוצות הכללים של החשבון ושל האזור. אם כבר מוגדרות אצלכם קבוצות כללים בחשבון או באזור, בצעו אחת מהאפשרויות הבאות:
- ייבאו קבוצות כללים קיימות אל Terraform באמצעות הכלי
cf-terraforming. גרסאות עדכניות של הכלי יכולות ליצור הגדרות משאבים עבור קבוצות כללים קיימות ולייבא את ההגדרה שלהן אל מצב Terraform. - התחילו מאפס על ידי מחיקת קבוצות הכללים הקיימות (קבוצות כללים של חשבון ושל אזור עם
"kind": "root"ו-"kind": "zone", בהתאמה) ואז הגדירו את תצורת קבוצות הכללים שלכם ב-Terraform.
#יצירת כלל הגבלת קצב ברמת האזור
הדוגמה הזו יוצרת כלל הגבלת קצב באזור עם המזהה <ZONE_ID> שחוסם תעבורה שחורגת מהקצב שהוגדר:
הרשאות נדרשות לאסימון API
נדרשת לפחות אחת מהרשאות האסימון הבאות:
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
}
}
}כדי ליצור כלל הגבלת קצב נוסף, הוסיפו אובייקט rules חדש לאותו משאב cloudflare_ruleset.
השתמשו במשאב קבוצת כללים אחד לכל שלב
כל אזור תומך בקבוצת כללים אחת של נקודת כניסה לכל שלב. הגדירו את כל כללי הגבלת הקצב של אזור נתון בתוך משאב cloudflare_ruleset יחיד עם kind = "zone" ו-phase = "http_ratelimit".
#יצירת כלל הגבלת קצב ברמת החשבון
הערות
- הגדרת הגבלת קצב ברמת החשבון דורשת תוכנית Enterprise עם תוספת בתשלום.
- קבוצות כללים מותאמות אישית שנפרסות ברמת החשבון יחולו רק על תעבורה נכנסת של אזורים בתוכנית Enterprise. הביטוי של כלל ה-
executeשלכם חייב להסתיים ב-and cf.zone.plan eq "ENT".
הדוגמה הזו מגדירה קבוצת כללים מותאמת אישית עם כלל הגבלת קצב יחיד בחשבון עם המזהה <ACCOUNT_ID>, שחוסם תעבורה לנתיב /api/ שחורגת מהקצב שהוגדר. משאב cloudflare_ruleset השני מגדיר כלל execute שפורס את קבוצת הכללים המותאמת אישית עבור תעבורה המיועדת אל example.com.
הרשאות נדרשות לאסימון API
נדרשות כל הרשאות האסימון הבאות:
Account WAF WriteAccount 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
}
}
}כדי ליצור כלל הגבלת קצב נוסף, הוסיפו אובייקט rules חדש לאותו משאב cloudflare_ruleset.
#יצירת כלל הגבלת קצב מתקדם
הדוגמה הזו יוצרת כלל הגבלת קצב באזור עם המזהה <ZONE_ID> עם:
- ביטוי ספירה מותאם אישית שכולל שדה תגובה (
http.response.code). - תגובת JSON מותאמת אישית לבקשות שהוגבלו בקצב.
הרשאות נדרשות לאסימון API
נדרשת לפחות אחת מהרשאות האסימון הבאות:
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)"
}
}
}כדי ליצור כלל הגבלת קצב נוסף, הוסיפו אובייקט rules חדש לאותו משאב cloudflare_ruleset.