Two Terraform Addresses Can Still Target One Remote Collection
A collision is born in state, not in syntax. Two configurations can both pass terraform validate and still describe one remote object. What separates them is not the HCL; it is which state file believes it owns the collection.
Only one create can win, so collisions arrive by adoption. The second attempt to create a phase entry point fails, because the scope already has one. The dangerous path is quieter: an import into a second root, a copied state file, or an address left behind by a rename that was never finished.
Authority belongs to the object, not to the rule. A ruleset holds one ordered list, and every write replaces that list. Adding a single rule is therefore the same operation as replacing the whole policy, with a smaller payload. There is no rule-level authority to delegate.
Trace it in two disposable roots. Put a platform root and an app root side by side and give both the same imported ruleset ID. Let the platform declare two rules and the app declare one. Each plan is green and mentions only local rules, and the applies land hours apart.
The oscillation is the signature. After the app applies, the remote list holds one rule and the platform's two are gone. The platform's next plan restores them and drops the app's rule; the app's next plan reverses it again. Each round trip is a new ruleset version and a fresh set of rule IDs.
Proof takes two values per root. Compare the ruleset ID recorded in each state, then read the version that state last saw. The same ID in both states proves shared authority; a version that neither root's apply produced proves a writer neither root knows. terraform state show prints both without changing anything.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Root A state
same ruleset ID"] --> C{"Same remote object
in both states?"}
B["Root B state
same ruleset ID"] --> C
C -->|"no"| S["Separate collections
no ownership conflict"]
C -->|"yes"| D["Both roots write
the full ordered list"]
D --> E["Later apply wins
earlier rules deleted"]
E --> F["Earlier root restores
later root deletes again"]
F --> G["Version climbs
rule IDs churn"]
G --> H["Reads as drift
is actually ownership"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A,B primary;
class C secondary;
class S ok;
class D,E danger;
class F,G,H warn;
Refresh before you theorize. A refresh-only plan turns the other owner's policy into a visible deletion. When a root proposes to remove rules it never declared, that is not drift to accept; it is evidence that two configurations share one collection.
One similar pair is legitimate. An entry point and a custom ruleset in the same phase are two objects with two owners: the entry point owns the execute rule, the custom ruleset owns the content it calls. The collision test is object identity, not phase identity, so write the map as object to address and treat a second row for one object as the finding.
Merge Policy Contributions Before the Provider Write
The provider writes a list, not a patch. Every apply sends the complete ordered array, and anything omitted from it is deleted. There is no server-side merge to fall back on, so composition has to finish before the request leaves Terraform.
Contributions should be values, not resources. Each policy team publishes rule objects carrying a ref, a rank, an expression and an action. One root renders those objects into the collection, while the contributing team still owns its expressions and reviews them.
Duplicate refs can fail the plan for free. Build a map keyed by ref with tomap({ for r in local.contributions : r.ref => r }) and let Terraform reject the duplicate key. Two teams that both ship login_block stop at plan time, before any API call, which is exactly the review moment you want.
Ranks turn order into a decision. Sort by a zero-padded rank so the rendered list is deterministic and a reviewer can explain every position. format("%04d", r.rank) produces sortable keys, and the ref can serve as the tiebreak.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Policy contributions
ref, rank, expression, action"] --> B{"Duplicate ref?"}
B -->|"yes"| C["Plan fails on the duplicate key
no API call is made"]
B -->|"no"| D{"Ranks unique?"}
D -->|"no"| E["Tiebreak decides the order
stable but not intentional"]
D -->|"yes"| F["Sort by padded rank
then by tiebreak"]
E --> F
F --> G{"Exception ranked above
the rule it guards?"}
G -->|"no"| H["Block fires first
exception never runs"]
G -->|"yes"| I["One ordered list
rendered by one root"]
H --> J["Reviewed reorder
plus an order test"]
I --> K["Single provider write
whole array replaced"]
C --> L["Fix ownership
before applying"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A primary;
class B,D,G secondary;
class I,K ok;
class C,H danger;
class E,J,L warn;
Ties are a policy gap, not a sorting detail. Two rules at the same rank still produce an order, but that order comes from the tiebreak field rather than from intent. Either require unique ranks or declare the tiebreak as part of the contract.
Exceptions need a rank constraint, not a position. Say that the skip precedes the block it guards, and encode it as a lower rank. A literal list index breaks the moment someone inserts a rule above it, and the failure is silent: the block simply fires first.
Review the merged list, not the contributions. Read in isolation, a block and its exception can still land in the wrong sequence. Only the rendered list shows order, count and enabled flags together, and that list is the artifact the owner signs.
Choose Stable Refs and Inspect Rule-ID Changes
Three identities describe one rule. The ref is the logical name you choose, the resource address is where Terraform stores it, and the rule ID is generated by Cloudflare. Only the first two belong to you; the third is a consequence of them.
A ref is a key, so a rename is a replacement. The provider matches configured rules to remote rules by ref, which is why the field is not cosmetic. Changing it deletes one rule and creates another, with a new ID, a blank history and a dashboard entry that no longer matches the config.
Reordering is cheaper than renaming. Moving a rule changes its index but keeps its ref and its rule ID. Readers of logs and incident notes still find the same decision; only the evaluation position moved.
Expression edits keep identity too. A narrower expression or a changed action updates the existing rule in place, and the state keeps the same rule ID. A plan that shows a new ID for an unchanged ref is worth stopping for.
Read the plan for identity churn, not prose. A rule whose ID is already known is being updated; a rule whose ID becomes known after apply is being created. Scan those lines first, then read expressions.
Positional matching is the ref-less failure. Without refs, list position is the only identity the provider has. Insert a rule near the top and every later rule pairs with the wrong remote ID, so an unchanged expression can be written onto a different decision.
Keep the ref-to-ID map after every apply. Some override maps and incident records key on the generated ID rather than the ref. A small record of collection, ref and ID, refreshed after each apply, turns a rename into a planned migration instead of a hunt.
Import the Whole Owned Collection Before Editing It
Import adopts an object, not a fragment. The command takes a scope selector, a scope ID and a ruleset ID; there is no form of it that imports one rule. Whatever the collection already contains arrives in state with it, dashboard rules included.
The first plan is the inventory. If the configuration was generated from the same object, that plan is empty and uninteresting. If it is not empty, the diff is the list of rules the configuration is about to delete. Read it as an inventory before reading it as a bug.
Dashboard rules are the usual casualty. A rule created during an incident exists nowhere in HCL. Adopting the collection brings it into state, and the first edit that starts from a hand-written partial list removes it. Generate the config from the imported object, review it, then change one thing.
A half-migrated root keeps two owners alive. An old address and a new address can both hold the same object after a schema migration or a rushed rename. Removing state from the stale address does not touch the remote collection, so that is the safe half of the operation.
Resolve a collision in this order. Choose the surviving address and record the ruleset ID it holds. Remove the other address from state, leaving the remote object untouched. Import the object into the survivor and require an empty plan before editing any rule.
A rename is a replacement, not a move. Name and kind are immutable, so a different name is a different collection even when the rules are copied across. A plan that replaces the ruleset is a migration: the old object keeps serving until the execute link moves, and it must be retired deliberately.
Test the Resulting Request Decisions
A ruleset test is a request test. A collection can be structurally correct and still decide the wrong thing, because the decision depends on order, exceptions and the phase that runs first. Four fixtures cover the shapes that matter here: a block, an exception, a cache decision and a redirect.
Prove ruleset ownership
Execution status: design-only. The steps are written for a disposable zone you own; no API token, zone or request was used to produce this article. Every identifier below is illustrative.
Prerequisites. Terraform CLI 1.17.1, Cloudflare provider 5.28.1, a disposable zone, read access to Security Events, and a client that can send a custom header.
Fixtures. Each row targets one decision, and the exception row is the only one whose result depends on order.
| Fixture | Rule shape | Expected decision |
|---|---|---|
/__dd01/block | custom block, 403, ref dd01_block | blocked by the named ref |
/__dd01/except | skip ranked above the block | passes, block never evaluates |
/assets/app.js | cache rule, cached asset | cache HIT on the second request |
/__dd01/old | redirect 301, ref dd01_redirect | 301, custom phase never sees it |
- Import the owned collection whole. Record the ruleset ID, the version and every ref, and require an empty plan before adding anything.
- Freeze the intended order in a file: position, ref, expression, action and the rule it must precede. An order without a stated reason cannot be reviewed.
- Add the four fixtures with refs prefixed
dd01_, placing the skip above the block. Apply, then read the plan for rule count, order and any rule ID that becomes known after apply. - Send each fixture twice from the same client. Record status, ray ID, rule ref and ruleset version. The cache fixture only proves itself on the second request.
- Move the skip below the block, apply, and re-send the exception fixture. It should now be blocked. Restore the order and confirm the pass returns; a reorder keeps rule IDs, so this is a safe pair of applies.
- Prove the collision in a second root. Import the same ruleset ID there, change one rule, apply, then refresh the first root. Its plan should propose restoring rules the second root removed.
- Remove the second address from state, refresh again and confirm the plan is empty. Then remove the probes and confirm the pre-test shape returns with the same refs.
- Write the false-positive card: the ref, the symptom, who may disable it, whether disable or delete is allowed, and the request that proves the re-enable worked.
Expected observation. Each fixture returns the decision its rank predicts, the exception passes only while it sits above the block, and the cache fixture serves from cache on the second request. The first root's refresh after step 6 shows removals it never authored, which is the ownership failure made visible.
Failure checks. The exercise fails if a probe changes a rule ID, if the exception passes with the block above it, if cache is measured on a first request, or if the only evidence is a plan screenshot. It also fails if step 2 was skipped, because the order was then never a decision.
Cost and cleanup. Zero with plan-only work. A disposable zone adds a domain and any plan the feature requires; remove the probes, destroy the lab collection, and delete the sandbox zone only after its DNS and certificate records are no longer needed.
Evidence to keep. The collection inventory, the order file, the plan output, the request log with ray IDs, the second-root drift plan and the false-positive record.
Ownership is proven by the plan that stays empty. A green apply says the API accepted a document; an empty plan after another root's change says nobody else is writing the collection. That second signal is the one worth putting in the review.