Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
title: Configure Bulk Redirects using Terraform
pcx_content_type: configuration
sidebar:
order: 8
label: Configure using Terraform
---

import { Render, Tabs, TabItem } from "~/components";

<Render file="v4-code-snippets" product="terraform" />

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.

```tf
# Cloudflare account ID
variable "cloudflare_account_id" {
default = "<ACCOUNT_ID>"
}

# Bulk redirect list description
variable "bulk_redirect_list_description" {
default = "my bulk redirect description"
}

# Bulk redirect list name
variable "bulk_redirect_list_name" {
default = "my_bulk_redirect_list_name"
}

# Bulk redirect list item (URL redirect)
variable "bulk_redirects" {
type = map(object({
source_url = string
target_url = string
status_code = number
}))

default = {
"redirect1" = {
source_url = "https://source.url/redirect/1"
target_url = "https://target.url/?redirect=1"
status_code = 301
}
"redirect2" = {
source_url = "https://source.url/redirect/2"
target_url = "https://target.url/?redirect=2"
status_code = 302
}
"redirect3" = {
source_url = "https://source.url/redirect/3"
target_url = "https://target.url/?redirect=3"
status_code = 307
}
}
}

# Create redirect list
resource "cloudflare_list" "bulk_redirect_to_id" {
account_id = var.cloudflare_account_id
name = var.bulk_redirect_list_name
description = var.bulk_redirect_list_description
kind = "redirect"
}

# Add redirect item into the redirect list
resource "cloudflare_list_item" "bulk_redirect_to_id_item" {
for_each = { for redirect in var.bulk_redirects : "${redirect.source_url}" => redirect }

account_id = var.cloudflare_account_id
list_id = cloudflare_list.bulk_redirect_to_id.id

redirect {
source_url = each.value.source_url
target_url = each.value.target_url
status_code = each.value.status_code
}

depends_on = [
cloudflare_list.bulk_redirect_to_id
]

}

# Create bulk redirect and attach redirect list
resource "cloudflare_ruleset" "bulk_root_redirect_to_id" {
account_id = var.cloudflare_account_id
name = var.bulk_redirect_list_name
description = var.bulk_redirect_list_description
kind = "root"
phase = "http_request_redirect"

rules {
action = "redirect"
action_parameters {
from_list {
name = var.bulk_redirect_list_name
key = "http.request.full_uri"
}
}
expression = "http.request.full_uri in ${"$"}${var.bulk_redirect_list_name}"
description = var.bulk_redirect_list_description
enabled = true
}

depends_on = [
cloudflare_list_item.bulk_redirect_to_id_item
]
}
```

## Required token permissions

Your API token must have at least the following [permissions](/fundamentals/api/reference/permissions/):

<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard">

- Account Filter Lists > Edit
- Bulk URL Redirects > Edit

</TabItem> <TabItem label="API">

- Account Rule Lists Write
- Bulk URL Redirects Write

</TabItem> </Tabs>

<Render file="terraform-additional-resources" />
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import { Markdown } from "~/components";
| { 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. |
| Billing Read | Grants read access to [billing profile, subscriptions, and access to fetch invoices](/billing/) and entitlements. |
| Billing {props.editWord} | Grants write access to [billing profile, subscriptions, and access to fetch invoices and entitlements](/billing/). |
| Bulk URL Redirects Read | Grants read access to [Bulk URL Redirects](/rules/url-forwarding/bulk-redirects/). |
| Bulk URL Redirects {props.editWord} | Grants write access to [Bulk URL Redirects](/rules/url-forwarding/bulk-redirects/). |
| Bulk URL Redirects Read | Grants read access to [Bulk Redirects](/rules/url-forwarding/bulk-redirects/). |
| Bulk URL Redirects {props.editWord} | Grants write access to [Bulk Redirects](/rules/url-forwarding/bulk-redirects/). |
| China Network Steering Read | Grants read access to [China Network Steering](/china-network/). |
| China Network Steering {props.editWord} | Grants write access to [China Network Steering](/china-network/). |
| Cloudchamber Read | Grants read access to Cloudchamber deployments. |
Expand Down
Loading