Automating Cloudflare Redirects with Terraform: A Step-by-Step Guide
In this blog post, we’ll explore how to use Terraform to create a Cloudflare ruleset that redirects visitors from specific countries to corresponding country-code top-level domains (ccTLDs). We’ll cover the necessary steps to set up the infrastructure, configure the rules, and deploy the solution.
Prerequisites
Before we dive into the Terraform configuration, make sure you have the following:
- A Cloudflare account
- Terraform installed on your machine
- A basic understanding of Terraform and Cloudflare
Terraform Configuration
The Terraform configuration consists of two main components: a cloudflare_zone resource and a cloudflare_ruleset resource.
1. cloudflare_zone Resource
The cloudflare_zone resource is used to reference the Cloudflare zone that we want to apply the redirect rules to. In this example, we’ll use the kmerkuri zone:
resource "cloudflare_zone" "kmerkuri" {
zone = "klevimerkuri.com"
account_id = "your-account-id"
}
Replace your-zone-id with your actual Cloudflare zone ID.
2. cloudflare_ruleset Resource
The cloudflare_ruleset resource is used to create a new ruleset that will contain our redirect rules. In this example, we’ll create a ruleset named “country-redirect” with a description “Redirect ruleset”:
resource "cloudflare_ruleset" "country_redirect" {
zone_id = cloudflare_zone.kmerkuri.id
name = "country-redirect"
description = "Redirect ruleset"
kind = "zone"
phase = "http_request_dynamic_redirect"
dynamic "rules" {
for_each = var.country_redirect_urls
content {
action = "redirect"
action_parameters {
from_value {
status_code = 302
target_url {
value = rules.value
}
preserve_query_string = true
}
}
expression = "(http.host eq \"klevimerkuri.com\" and ip.geoip.country eq \"${rules.key}\")"
description = "Redirect visitors to ${rules.value} "
enabled = false
}
}
}
This configuration defines a single rule that will redirect visitors from specific countries to corresponding country-code top-level domains (ccTLDs). The rules block uses the for_each argument to iterate over the country_redirect_urls variable, which is a map of country codes to corresponding ccTLDs.
3. country_redirect_urls Variable
The country_redirect_urls variable is a map of country codes to corresponding ccTLDs. In this example, we’ll define the following:
variable "country_redirect_urls" {
type = map(string)
default = {
"AT" = "https://klevimerkuri.com"
"BE" = "https://kmerkuri.be"
"DK" = "https://kmerkuri.dk"
"CZ" = "https://kmerkuri.cz"
}
}
This variable defines four country codes (AT, BE, DK, and CZ) and their corresponding ccTLDs.
Deploying the Configuration
To deploy the configuration, run the following command:
terraform apply
This will create a new Cloudflare ruleset and apply the redirect rules to the specified zone.
Conclusion
In this blog post, we’ve explored how to use Terraform to create a Cloudflare ruleset that redirects visitors from specific countries to corresponding country-code top-level domains (ccTLDs).
Remember to replace your-account-id with your actual Cloudflare zone ID and adjust the country_redirect_urls variable according to your needs.