locals.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# locals =======================================================================

locals {
  module_tags = {
    ManagedBy = "terraform"
    Module    = "governance/scp"
  }

  policy_tags = {
    for policy_key, policy in var.policies :
    policy_key => merge(local.module_tags, policy.tags)
  }

  # Flatten policy : target pairs for attachment.
  # Each policy can target multiple OUs or accounts.
  policy_target_pairs = flatten([
    for policy_key, policy in var.policies : [
      for idx, target in policy.targets : {
        policy_key = policy_key
        target     = target
        index      = idx
      }
    ]
  ])

  policy_target_map = {
    for pair in local.policy_target_pairs :
    "${pair.policy_key}:${pair.index}" => pair
  }

  # Flatten attachment : target pairs for pre-existing SCP attachments.
  # Each attachment can target multiple OUs or accounts.
  attachment_target_pairs = flatten([
    for policy_key, attachment in var.attachments : [
      for idx, target in attachment.targets : {
        policy_key = policy_key
        policy_id  = attachment.policy_id
        target     = target
        index      = idx
      }
    ]
  ])

  attachment_target_map = {
    for pair in local.attachment_target_pairs :
    "${pair.policy_key}:${pair.index}" => pair
  }
}