|
| 1 | +--- |
| 2 | +title: Configure Bulk Redirects using Terraform |
| 3 | +pcx_content_type: configuration |
| 4 | +sidebar: |
| 5 | + order: 8 |
| 6 | + label: Configure using Terraform |
| 7 | +--- |
| 8 | + |
| 9 | +import { Render, Tabs, TabItem } from "~/components"; |
| 10 | + |
| 11 | +<Render file="v4-code-snippets" product="terraform" /> |
| 12 | + |
| 13 | +This Terraform example configures account-level Bulk Redirects. It creates a [Bulk Redirect List](/rules/url-forwarding/bulk-redirects/concepts/#bulk-redirect-lists) populated with [URL redirects](/rules/url-forwarding/bulk-redirects/concepts/#url-redirects) and a corresponding [Bulk Redirect Rule](/rules/url-forwarding/bulk-redirects/concepts/#bulk-redirect-rules) to activate them. |
| 14 | + |
| 15 | +```tf |
| 16 | +# Cloudflare account ID |
| 17 | +variable "cloudflare_account_id" { |
| 18 | + default = "<ACCOUNT_ID>" |
| 19 | +} |
| 20 | +
|
| 21 | +# Bulk redirect list description |
| 22 | +variable "bulk_redirect_list_description" { |
| 23 | + default = "my bulk redirect description" |
| 24 | +} |
| 25 | +
|
| 26 | +# Bulk redirect list name |
| 27 | +variable "bulk_redirect_list_name" { |
| 28 | + default = "my_bulk_redirect_list_name" |
| 29 | +} |
| 30 | +
|
| 31 | +# Bulk redirect list item (URL redirect) |
| 32 | +variable "bulk_redirects" { |
| 33 | + type = map(object({ |
| 34 | + source_url = string |
| 35 | + target_url = string |
| 36 | + status_code = number |
| 37 | + })) |
| 38 | +
|
| 39 | + default = { |
| 40 | + "redirect1" = { |
| 41 | + source_url = "https://source.url/redirect/1" |
| 42 | + target_url = "https://target.url/?redirect=1" |
| 43 | + status_code = 301 |
| 44 | + } |
| 45 | + "redirect2" = { |
| 46 | + source_url = "https://source.url/redirect/2" |
| 47 | + target_url = "https://target.url/?redirect=2" |
| 48 | + status_code = 302 |
| 49 | + } |
| 50 | + "redirect3" = { |
| 51 | + source_url = "https://source.url/redirect/3" |
| 52 | + target_url = "https://target.url/?redirect=3" |
| 53 | + status_code = 307 |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +# Create redirect list |
| 59 | +resource "cloudflare_list" "bulk_redirect_to_id" { |
| 60 | + account_id = var.cloudflare_account_id |
| 61 | + name = var.bulk_redirect_list_name |
| 62 | + description = var.bulk_redirect_list_description |
| 63 | + kind = "redirect" |
| 64 | +} |
| 65 | +
|
| 66 | +# Add redirect item into the redirect list |
| 67 | +resource "cloudflare_list_item" "bulk_redirect_to_id_item" { |
| 68 | + for_each = { for redirect in var.bulk_redirects : "${redirect.source_url}" => redirect } |
| 69 | +
|
| 70 | + account_id = var.cloudflare_account_id |
| 71 | + list_id = cloudflare_list.bulk_redirect_to_id.id |
| 72 | +
|
| 73 | + redirect { |
| 74 | + source_url = each.value.source_url |
| 75 | + target_url = each.value.target_url |
| 76 | + status_code = each.value.status_code |
| 77 | + } |
| 78 | +
|
| 79 | + depends_on = [ |
| 80 | + cloudflare_list.bulk_redirect_to_id |
| 81 | + ] |
| 82 | +
|
| 83 | +} |
| 84 | +
|
| 85 | +# Create bulk redirect and attach redirect list |
| 86 | +resource "cloudflare_ruleset" "bulk_root_redirect_to_id" { |
| 87 | + account_id = var.cloudflare_account_id |
| 88 | + name = var.bulk_redirect_list_name |
| 89 | + description = var.bulk_redirect_list_description |
| 90 | + kind = "root" |
| 91 | + phase = "http_request_redirect" |
| 92 | +
|
| 93 | + rules { |
| 94 | + action = "redirect" |
| 95 | + action_parameters { |
| 96 | + from_list { |
| 97 | + name = var.bulk_redirect_list_name |
| 98 | + key = "http.request.full_uri" |
| 99 | + } |
| 100 | + } |
| 101 | + expression = "http.request.full_uri in ${"$"}${var.bulk_redirect_list_name}" |
| 102 | + description = var.bulk_redirect_list_description |
| 103 | + enabled = true |
| 104 | + } |
| 105 | +
|
| 106 | + depends_on = [ |
| 107 | + cloudflare_list_item.bulk_redirect_to_id_item |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Required token permissions |
| 113 | + |
| 114 | +Your API token must have at least the following [permissions](/fundamentals/api/reference/permissions/): |
| 115 | + |
| 116 | +<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard"> |
| 117 | + |
| 118 | +- Account Filter Lists > Edit |
| 119 | +- Bulk URL Redirects > Edit |
| 120 | + |
| 121 | +</TabItem> <TabItem label="API"> |
| 122 | + |
| 123 | +- Account Rule Lists Write |
| 124 | +- Bulk URL Redirects Write |
| 125 | + |
| 126 | +</TabItem> </Tabs> |
| 127 | + |
| 128 | +<Render file="terraform-additional-resources" /> |
0 commit comments