Skip to content

Commit

Permalink
OPS-6301: Adjusted to Apply policy to multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
vikkasyousaf committed Oct 30, 2024
1 parent 4a8efde commit d8107b8
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,30 @@ resource "aws_organizations_policy" "scp" {
content = templatefile(lookup(each.value, "file"), {})
}

# Create a local variable to flatten policies with target IDs
locals {
policy_attachments = [
for policy in var.policies : [
for target_id in policy.target_ids : {
policy_name = policy.name
target_id = target_id
}
]
]
}

# Flatten the local variable to a single list of attachments
locals {
flattened_policy_attachments = flatten(local.policy_attachments)
}

# Attach SCP policies to multiple target accounts or OUs
resource "aws_organizations_policy_attachment" "attach_scp" {
for_each = {
for policy in aws_organizations_policy.scp :
policy.name => policy
for idx, attachment in local.flattened_policy_attachments :
"${attachment.policy_name}-${attachment.target_id}" => attachment
}
policy_id = each.value.id
target_id = flatten([for p in var.policies : p.target_ids if p.name == each.key])[0]

policy_id = aws_organizations_policy.scp[each.value.policy_name].id
target_id = each.value.target_id
}

0 comments on commit d8107b8

Please sign in to comment.