The following Terraform configuration examples address common scenarios for managing, configuring, and using AI Security for Apps.
For more information, refer to the Terraform Cloudflare provider documentation ↗︎.
If you are using the Cloudflare API, refer to Get started.
Use the cloudflare_ai_security ↗︎ resource to turn on AI Security for Apps for a zone. This resource requires Terraform Cloudflare provider version 5.29.0 or later. For example:
resource "cloudflare_ai_security" "zone_ai_security" {
zone_id = var.cloudflare_zone_id
enabled = true
}AI Security for Apps only scans requests to endpoints with the cf-llm label. For more information, refer to Add cf-llm label to endpoint.
Use the cloudflare_ai_security_custom_topics ↗︎ resource to define the custom topics for a zone. This resource requires Terraform Cloudflare provider version 5.29.0 or later. For example:
resource "cloudflare_ai_security_custom_topics" "zone_custom_topics" {
zone_id = var.cloudflare_zone_id
topics = [
{
label = "competitors"
topic = "seeking info on competitors"
},
{
label = "financial-advice"
topic = "seeking financial advice"
},
{
label = "job-seeking"
topic = "asking for résumé advice"
}
]
}For more information, refer to Custom topics.
This example adds two custom rules that use AI Security for Apps fields in their expressions:
- The first rule blocks requests where Cloudflare detected personally identifiable information (PII) in the LLM prompt, using the
cf.llm.prompt.pii_detectedfield. The rule returns a custom JSON response. - The second rule blocks requests where the LLM prompt is highly relevant to the
competitorscustom topic, using thecf.llm.prompt.custom_topic_categoriesfield. The label in the expression must match a label in your custom topics list.
To use these fields you must enable AI Security for Apps.
Required API token permissions
At least one of the following token permissions is required:
Zone WAF Write
Configure the cloudflare_ruleset ↗︎ resource:
resource "cloudflare_ruleset" "zone_custom_firewall_ai_security" {
zone_id = var.cloudflare_zone_id
name = "Phase entry point ruleset for custom rules in my zone"
description = ""
kind = "zone"
phase = "http_request_firewall_custom"
rules = [
{
ref = "block_llm_prompts_with_pii"
description = "Block LLM prompts containing PII"
expression = "(cf.llm.prompt.pii_detected)"
action = "block"
action_parameters = {
response = {
status_code = 403
content = "{\"error\": \"Your request was blocked. Please rephrase your request.\"}"
content_type = "application/json"
}
}
},
{
ref = "block_llm_prompts_about_competitors"
description = "Block LLM prompts about competitors"
expression = "(cf.llm.prompt.custom_topic_categories[\"competitors\"] lt 20)"
action = "block"
}
]
}A zone has a single entry point ruleset for custom rules. If you already manage this ruleset with Terraform, add the rules to the existing cloudflare_ruleset resource instead of creating a new one.
For more rule examples, refer to Example mitigation rules.
For additional Terraform configuration examples, refer to WAF custom rules configuration using Terraform.