Skip to content

fix: use static ARNs in attached_policies to resolve for_each unknown value error #3

Merged
merged 3 commits into from
Jul 23, 2026

Conversation

arnol377
Copy link
Collaborator

Description

  • Replace attached_policies = [aws_iam_policy.*.arn] with statically constructed ARNs in all three role module calls (patch_role, patch_execution_role, patch_execproxy_role)

Purpose

Terraform cannot use compute-time resource ARNs as for_each set keys because they are unknown at plan time. This caused Invalid for_each argument errors on every tf plan run when the IAM policies and role policy attachments were being created together for the first time.

The fix constructs the ARN using locals that are always resolved during plan:

# Before (breaks plan)
attached_policies = [aws_iam_policy.patch_role.arn]

# After (plan-safe)
attached_policies = [format("arn:%v:iam::%v:policy/%v", local.partition, local.account_id, local.patch_policy_name)]

local.partition and local.account_id come from data.aws_partition and data.aws_caller_identity which are always resolved at plan time. Policy names are derived from var.name_prefix which is a static input.

tf-plan output

Plan run from 036728032133-lab-gov-operations-nonprod/infrastructure/global/apps/inf-patch with this fix applied:

Plan: 33 to add, 0 to change, 0 to destroy.

No errors. Deprecation warnings only (unrelated to this change).

Dave Arnold added 3 commits July 9, 2026 16:06
Replace SSH-style git@ source with git::https:// for all three
aws-iam-role module references (patch_role, patch_execution_role,
patch_execproxy_role). HTTPS is required in environments where SSH
agent forwarding is unavailable or not configured.
…alue error

Terraform cannot use compute-time resource ARNs (aws_iam_policy.*.arn) as
for_each set keys since they are unknown at plan time. Replace with statically
constructed ARNs using locals that are always known at plan time:
  format("arn:%v:iam::%v:policy/%v", local.partition, local.account_id, local.<name>)

Fixes 'Invalid for_each argument' errors on all three role modules.
@arnol377 arnol377 merged commit 6776504 into master Jul 23, 2026
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant