Skip to content

Commit 4f905a6

Browse files
[Rules] Add Terraform example for Bulk Redirects (cloudflare#22416)
--------- Co-authored-by: Pedro Sousa <[email protected]>
1 parent 41f3501 commit 4f905a6

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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" />

src/content/partials/fundamentals/account-permissions-table.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import { Markdown } from "~/components";
4747
| { props.src === "api" && "Account" } API Gateway {props.editWord} | Grants write access to [API Gateway (including API Shield)](/api-shield/) for all domains in an account. |
4848
| Billing Read | Grants read access to [billing profile, subscriptions, and access to fetch invoices](/billing/) and entitlements. |
4949
| Billing {props.editWord} | Grants write access to [billing profile, subscriptions, and access to fetch invoices and entitlements](/billing/). |
50-
| Bulk URL Redirects Read | Grants read access to [Bulk URL Redirects](/rules/url-forwarding/bulk-redirects/). |
51-
| Bulk URL Redirects {props.editWord} | Grants write access to [Bulk URL Redirects](/rules/url-forwarding/bulk-redirects/). |
50+
| Bulk URL Redirects Read | Grants read access to [Bulk Redirects](/rules/url-forwarding/bulk-redirects/). |
51+
| Bulk URL Redirects {props.editWord} | Grants write access to [Bulk Redirects](/rules/url-forwarding/bulk-redirects/). |
5252
| China Network Steering Read | Grants read access to [China Network Steering](/china-network/). |
5353
| China Network Steering {props.editWord} | Grants write access to [China Network Steering](/china-network/). |
5454
| Cloudchamber Read | Grants read access to Cloudchamber deployments. |

0 commit comments

Comments
 (0)