Find Overlapping Terraform Addresses for One Remote Setting
Terraform identity is local, and AWS identity is not. An address is unique inside one state file; the object it writes is unique inside one account. Two roots can therefore hold two different addresses for one remote setting, and neither plan will fail.
The join key is a name, a tuple or an account, never an ARN that Terraform generated. A role is named, an inline policy is named inside that role, a security group is a group ID that two states can both record, and an account setting has no name at all. Two states collide when they store the same value for that key.
Three authority shapes can point at one setting. An inline block owns a whole collection and replaces its contents. A standalone resource owns one entry and ignores its siblings. An exclusive resource owns membership and removes anything it was not told about. The first and third can delete work they never wrote.
The shapes are documented, not inferred. The AWS provider warns against combining inline security-group rules with standalone rule resources, warns that route blocks conflict with standalone routes, and warns that the role resource's policy arguments take exclusive control of that role's policies. Each warning describes the same mechanism at a different address.
Read state, not HCL, to find the overlap. terraform state list enumerates what a root believes it owns, and the stored attributes carry the remote key. Two states that print the same role name, group ID or table ID are the finding, even when the resource names differ.
A shared module hides the collision best. Two environments that both call a shared module with a literal name argument produce identical remote keys from unrelated file paths. The module is not wrong; calling it twice against one account is.
%%{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','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
K["One remote key
role name, group ID, table ID, account"] --> Q{"How many states
record this key?"}
Q -- "one" --> OK["Single owner
the plan is the whole truth"]
Q -- "two" --> S{"Which authority shape
does each state use?"}
S -- "inline block" --> I["Owns the collection
replaces the full list"]
S -- "standalone entry" --> E["Owns one entry
siblings untouched"]
S -- "exclusive resource" --> X["Owns membership
removes unlisted members"]
I --> W["Later apply wins
no error, no warning"]
E --> W
X --> W
W --> R["Each plan stays locally correct
the remote object tells another story"]
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 K,Q,S primary;
class I,E,X secondary;
class OK,R ok;
class W danger;
Figure 1 separates the search from the diagnosis. Finding the shared key is a state query; deciding who should own it is a design choice. The red node is the only step that loses access, and it happens silently, which is why the search has to run before the design review rather than after the incident.
The left column of Figure 2 is the search result, not a taxonomy. Each row is a key you can query for in state, and each one has a different identity rule. Comparing role names catches the IAM collision; comparing group IDs catches the rule collision; comparing the account ID catches the setting that has no address at all.
Read Policy and Rule Replacement Semantics
Every write replaces something, and the resource decides what. One call replaces a single document, one tuple, one destination or one account value. Reading the granularity first is what turns "Terraform removed my access" into a predictable outcome.
An inline IAM policy is replaced by name. Two resources with different names on one role coexist and accumulate. The same name declared in two states means the later apply overwrites the earlier document, and the earlier state records the change as drift on the next refresh.
A bucket policy is replaced whole. The provider documentation is explicit that each resource calls the same API that swaps the entire document without warning. Two resources for one bucket therefore do not merge statements; the last apply publishes its own document and every other grant disappears.
Managed attachments are a set on a principal, or across the account. The role attachment resource adds or removes one ARN. The older policy-attachment resource declares every principal that may hold one policy, account wide, and revokes the rest.
An exclusive resource hides its deletion in a shorter list. The plan shows the argument shrinking, not a resource being destroyed, so a reviewer who scans for deletions sees nothing. Read the list length, not the resource count.
A security group rule is a tuple. Protocol, port range and source identify the entry, and AWS rejects a duplicate tuple with an error instead of merging it. Descriptions and tags are not part of that identity, which is why two rules can look different in review and still be the same rule.
A route is identified by its destination. Two writers naming one prefix produce one entry whose target alternates with apply order. The table itself has no per-route address, so the plan shows an update rather than a conflict.
Account settings are singletons with no name. The account ID, or the literal default, is the whole key. Destroying the default-encryption resource disables encryption for the region, so removal is a security change even when the diff is one line.
Three errors name the shape that is wrong. A duplicate-permission error means two entry owners. A delete conflict means a policy is still attached to a principal somewhere. A perpetual diff means a collection and an entry are both writing the same key.
%%{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','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
W["Apply sends desired state"] --> G{"Remote key exists?"}
G -- "no" --> C["Create the entry
or the collection"]
C --> OK["Stable plan
one writer, one key"]
G -- "yes" --> S{"Authority shape
of this address"}
S -- "entry" --> E["Replace this key only
siblings survive"]
S -- "collection" --> L["Replace the whole list
absent members deleted"]
S -- "exclusive" --> X["Apply the set difference
unlisted members revoked"]
E --> N{"Second state holds
the same key?"}
L --> N
X --> N
N -- "no" --> OK
N -- "yes" --> P["Later apply wins
earlier work disappears"]
P --> F1["Duplicate tuple
two entry owners"]
P --> F2["DeleteConflict
principal still attached"]
P --> F3["Perpetual diff
collection plus entry"]
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 G,S,N primary;
class W,C,E,L,X secondary;
class OK ok;
class P,F1,F2,F3 danger;
Figure 3 puts the three failure branches beside the happy path. Only the first branch is a clean create. Every other branch either replaces a neighbour or removes something by omission, and both of them look like success in the apply log.
The replacement unit is the resource's collection, not the object's name. That single sentence resolves most arguments about ownership. A resource that takes a list can always delete a member; a resource that takes one key cannot. Choose the resource by the size of the list you are willing to manage, not by the size of the change you are making today.
Choose Narrow or Collection Ownership Deliberately
Ownership is a decision about deletion authority. A narrow resource can remove only what it declared. A collection resource can remove what nobody declared in that file, including grants written by people who never opened it.
Use the narrow shape when the object outlives your team. Shared roles, shared tables and account singletons are edited by several groups, and each addition should be its own address with its own author. The plan then names every change and attributes it.
Use the collection shape when one team owns a small closed set. A service's own security group with four rules reads better as one block, and the delete authority is the feature: removing a line is supposed to remove access. The precondition is that nobody else writes to that group.
Never ship both shapes for one key. The provider documentation says this plainly for security groups and route tables, and the role resource says the same about its policy arguments. The result is oscillation between two correct-looking configurations, not a merge.
Prefer the explicit exclusive resource over a hidden one. A role attribute that silently takes over policy management is hard to find in review. A dedicated exclusive resource puts the purge in its own line, where a shorter can be questioned.
Record the choice where a reviewer will see it. The object's tags can carry an owner, and the module README can name the shape. Tags are mutable and visible in the console; the README is where the reasoning survives the next rewrite.
Migrating shapes is one change, not two. Moving from entries to a collection, or back, belongs in a single apply with the state operation and the config edit together. Running both shapes for an hour is how a deletion gets blamed on the wrong commit.
| Setting | Narrow address | Collection address | What the collection removes |
|---|---|---|---|
| Role inline policies | aws_iam_role_policy | aws_iam_role_policies_exclusive | Every inline policy name not listed |
| Role managed policies | aws_iam_role_policy_attachment | aws_iam_role_policy_attachments_exclusive | Every attached policy ARN not listed |
| One policy, many principals | aws_iam_role_policy_attachment | aws_iam_policy_attachment | That policy from every principal not listed |
| Security group rules | aws_vpc_security_group_ingress_rule | ingress and egress blocks | Every rule not declared on the group |
| Route table entries | aws_route | route blocks | Every route not declared on the table |
| Route propagation | aws_vpn_gateway_route_propagation | propagating_vgws on the table | Every propagation not declared |
| Account setting | none exists | aws_ebs_encryption_by_default | The previous account value on destroy |
Table 1 is the review artifact, not a menu. A row with no narrow address is a setting that cannot be delegated, so it needs an owner and a change window instead of a module. A row where two teams appear in the same column is the finding the whole article is about.
The account row is the one people forget. Every other family has a name a second team can avoid reusing. An account setting has only the account, so the first configuration to declare it owns it, and that ownership is usually accidental.
Figure 4 is a boundary, not a preference. The upper branch is correct for a shared role in a platform account; the lower branch is correct for a group that only one service writes. Neither is better in general, and the crimson band is the only universally wrong answer.
Adopt Existing Grants Without Expanding Them
An import changes state and never the remote object. The first plan after an import compares your configuration with what already exists, so a deletion in that plan means your file is narrower than reality. That is information, not an error to override.
Read the live grant before you write one. A managed policy can be read back as JSON, and the same read reports how many principals hold it. A bucket policy needs one API call for the document. Each gives you the exact statements you must preserve, in the form the API will compare.
Merge with a source document instead of retyping statements. The policy-document data source accepts existing documents as sources and merges them with your own statements. The plan then shows a union, and a statement you forgot cannot vanish because it was never copied.
Adopt at the narrow address first. Import one rule, one route or one inline policy, prove an empty plan, and only then remove the grant you no longer want. Two changes with two reviews beat one change that both adopts and rewrites.
An exclusive import is a reconciliation, not a reset. Import reads the object, so the first plan tells you which members the resource now considers its own. An empty plan is the proof that nothing is scheduled for removal; a plan with removals means the argument is narrower than the live set.
Never adopt by declaring an empty list. For the exclusive resources, an empty list is a directive to remove every member. It is a legitimate cleanup change and a catastrophic placeholder, so it belongs in its own commit with a message that says so.
Composite import keys adopt the wrong object quietly. A security-group rule needs the group plus the full tuple, a route needs the table plus the destination, and an inline policy needs the role plus the policy name. Verify the imported attributes in state, because a wrong key that happens to match will look like success.
Adoption does not authorize the grant. An imported role that already held broad permissions keeps them, and calling the import finished would launder the finding. Record the grant with a date, then narrow it in a change reviewed as a security change rather than a refactor.
Figure 5 is the difference between adoption and replacement. Both paths end with your desired statement in the document. Only one of them still contains the partner grant, and the plan is where that difference is visible before an apply makes it permanent.
Prove a Stable Plan and an Unintended-Removal Rejection
Prove two properties, because either one alone is misleading. Stability means two consecutive plans report no changes. Safety means a plan that would delete a grant you did not write is caught before apply, and the reviewer can name the grant and its owner.
Run it in two roots against one disposable account. A platform root owns the collection-shaped addresses and an app root owns one narrow address per family. Synthetic policies and a single security group keep the blast radius at zero.
Prove a Stable Plan and an Unintended-Removal Rejection
Status: design-only. Two root modules, one lab account, placeholder names. No production data, no running compute, and every permission is a read-only synthetic statement.
Prerequisites. Terraform CLI 1.17.1 and provider 6.72.0. One IAM role, one security group in a disposable VPC with two ingress rules, one route table whose default route is declared inline, and an account where the default-encryption resource is safe to plan against.
Steps. 1. Apply the platform root, which creates the role with two inline policies as separate narrow resources, then plan again and confirm no changes. 2. Apply the app root, which adds a third inline policy to the same role through its own narrow address, then plan both roots and confirm both are clean. 3. Import the exclusive collection resource for the role, keyed by the role name, and confirm the first plan is empty. 4. Edit its list argument to name only the platform's two policies, then plan. Read the list shrinking from three names to two and stop there. That shrink is the unintended removal, and the plan shows no delete line for it.
- Revert to the narrow shape and drop the exclusive resource from state without applying it, then confirm two consecutive plans report no changes in both roots.
- Add a standalone route in the app root for the destination the platform root already declares inline. If both use the same target the adoption is silent; if the targets differ the platform root plans an update back on its next run.
- Add one inline ingress block to the shared group in a scratch branch, apply, then plan the app root and record which rules it wants to re-create.
- Add the account default-encryption resource, apply it, then plan its destroy and read the account-level consequence rather than the resource count.
| Check | Action | Pass condition | Failure signal |
|---|---|---|---|
| Stable plan | Plan twice with no edits | No changes both times | A diff that returns after each apply |
| Collection shrink | Platform lists only its own names | Plan shows the third name disappearing | Plan is clean, so the third policy is unowned |
| Rejected removal | Read step 4 and refuse it | Reviewer names the grant and its author | The shrink is approved as cleanup |
| Route overlap | App adds a route for an existing destination | Platform plans its target back | No diff, so two owners look like one |
| Rule overlap | Inline ingress on the shared group | App plans its rule again after the apply | App plan stays clean, so the rule is gone |
| Account destroy | Plan the singleton destroy | Plan states the setting is disabled | Plan reads as a no-op resource removal |
Expected observation. The third policy is visible in both states after step 2, which is the collision. The imported exclusive resource lists all three names, so the plan in step 4 shows a shorter list and no deletion; only a reviewer who reads the argument catches the removal. After step 5, both roots plan clean twice. In step 6 the standalone route is accepted without error, which is the quiet adoption the article warns about. In step 7 the shared group loses the app rules at apply time, and the app root's next plan proposes to create them again, which is the oscillation. The destroy plan is the only one that describes an account-wide loss.
Failure check. The lab fails if both roots plan clean after the exclusive shrink is applied, because that means the third policy had no owner at all. It also fails if the inline ingress block produces an immediate plan diff in its own root, since that would suggest the conflict is detected rather than overwritten. A clean app plan after step 7 means the rules were never managed there. Any conclusion drawn from a console screenshot rather than a plan is not evidence.
Cost and cleanup. The lab is configuration only: a role, policies, a security group, a route table and an account setting, with no instances, gateways or addresses. Destroy the app root before the platform root, because the narrow resources reference the shared role and the group, and confirm the account setting was restored to its original value rather than left at the lab value.
Evidence to keep. The state list from both roots before and after the exclusive change, the plan line that shrank the list, two clean plans per root, the route plan after the standalone create, the app plan after the inline apply, and the destroy plan for the singleton.
Close with the three questions a reviewer should be able to answer. Which key is shared, which address holds authority over it, and what the next plan would delete. A configuration that answers all three has ownership; one that cannot will keep producing removals that look like drift, an apply that looks like a mistake, and a grant nobody remembers writing.