Terraform configuration examples
Last updated May 6, 2026 The following Terraform configuration examples address common scenarios for managing, configuring, and using leaked credentials detection.
For more information, refer to the Terraform Cloudflare provider documentation ↗︎.
If you are using the Cloudflare API, refer to Common API calls.
Enable leaked credentials detection
Use the cloudflare_leaked_credential_check resource to enable leaked credentials detection for a zone. For example:
resource "cloudflare_leaked_credential_check" "zone_lcc_example" {
zone_id = var.cloudflare_zone_id
enabled = true
}
Configure a custom detection location
Use the cloudflare_leaked_credential_check_rule resource to add a custom detection location. For example:
resource "cloudflare_leaked_credential_check_rule" "custom_location_example" {
zone_id = var.cloudflare_zone_id
username = "lookup_json_string(http.request.body.raw, \"user\")"
password = "lookup_json_string(http.request.body.raw, \"secret\")"
}
You only need to provide an expression for the username in custom detection locations.
Add a custom rule to challenge requests with leaked credentials
This example adds a custom rule that challenges requests with leaked credentials by using one of the leaked credentials fields in the rule expression.
To use the cf.waf.credential_check.username_and_password_leaked field you must enable leaked credentials detection.
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Zone WAF Write</code>
</details>
Configure the cloudflare_ruleset ↗︎ resource:
resource "cloudflare_ruleset" "zone_custom_firewall_leaked_creds" {
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 = "challenge_leaked_username_password"
description = "Challenge requests with a leaked username and password"
expression = "(cf.waf.credential_check.username_and_password_leaked)"
action = "managed_challenge"
}]
}
resource "cloudflare_ruleset" "zone_custom_firewall_leaked_creds" {
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 = "challenge_leaked_username_password"
description = "Challenge requests with a leaked username and password"
expression = "(cf.waf.credential_check.username_and_password_leaked)"
action = "managed_challenge"
}
}
More resources
For additional Terraform configuration examples, refer to WAF custom rules configuration using Terraform.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/waf/detections/leaked-credentials/terraform-examples/#page","headline":"Terraform configuration examples","description":"Terraform examples for managing and configuring leaked credentials detection.","url":"https://developers.cloudflare.com/waf/detections/leaked-credentials/terraform-examples/","inLanguage":"en","image":"https://developers.cloudflare.com/waf/detections/leaked-credentials/terraform-examples/og.png?v=0a6682a5c188692b","dateModified":"2026-05-06","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Terraform"]}
description: Scores from 1 to 99 indicating the likelihood a request came from a bot. title: Bot scores image: https://developers.cloudflare.com/bots/concepts/bot-score/og.png?v=9d963a4a1c972135
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/bots/llms.txt
Use this file to discover all available pages before exploring further.
Docs