Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPS-6301: Adjusted to apply policy to multiple accounts #2

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
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
}