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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# locals =====================================================================

data "aws_availability_zones" "available" { state = "available" }

locals {
  region_abbr = split("-", data.aws_availability_zones.available.zone_ids[0])[0]

  module_tags = {
    managed-by  = "terraform"
    module-name = "networking:security-groups"
  }

  sg_names = {
    for sg_key, sg in var.security_groups :
    sg_key => "${var.name_prefix}-${local.region_abbr}-${sg.name_suffix}"
  }

  # Ingress rules, step 1: expand each rule into one entry per source.
  # A rule with 2 CIDRs becomes 2 entries here, sharing the same
  # protocol, ports, and description. source_sg_key resolves to another
  # SG created by this module call; source_sg_id references any existing
  # SG by ID directly. Both collapse into a single "sg" source type,
  # disambiguated by a key:/id: prefix resolved at the resource block.
  ingress_sources = flatten([
    for sg_key, sg in var.security_groups : [
      for idx, rule in sg.ingress_rules : [
        for source in concat(
          [for c in rule.ipv4_cidr_blocks : { type = "cidr4", value = c }],
          [for c in rule.ipv6_cidr_blocks : { type = "cidr6", value = c }],
          rule.source_sg_key != null ? [{ type = "sg", value = "key:${rule.source_sg_key}" }] : [],
          rule.source_sg_id != null ? [{ type = "sg", value = "id:${rule.source_sg_id}" }] : [],
          rule.self ? [{ type = "self", value = "" }] : []
          ) : {
          sg_key       = sg_key
          idx          = idx
          rule         = rule
          source_type  = source.type
          source_value = source.value
        }
      ]
    ]
  ])

  # Ingress rules, step 2: key each expanded entry for for_each.
  # The index keeps keys unique when a rule expands to multiple sources.
  ingress_rule_map = {
    for i, item in local.ingress_sources :
    "${item.sg_key}-ingress-${item.idx}-${item.source_type}-${i}" => item
  }

  # Egress rules, step 1: expand each rule into one entry per source.
  # A rule with 2 CIDRs becomes 2 entries here, sharing the same
  # protocol, ports, and description. source_sg_key resolves to another
  # SG created by this module call; source_sg_id references any existing
  # SG by ID directly. Both collapse into a single "sg" source type,
  # disambiguated by a key:/id: prefix resolved at the resource block.
  egress_sources = flatten([
    for sg_key, sg in var.security_groups : [
      for idx, rule in sg.egress_rules : [
        for source in concat(
          [for c in rule.ipv4_cidr_blocks : { type = "cidr4", value = c }],
          [for c in rule.ipv6_cidr_blocks : { type = "cidr6", value = c }],
          rule.source_sg_key != null ? [{ type = "sg", value = "key:${rule.source_sg_key}" }] : [],
          rule.source_sg_id != null ? [{ type = "sg", value = "id:${rule.source_sg_id}" }] : [],
          rule.self ? [{ type = "self", value = "" }] : []
          ) : {
          sg_key       = sg_key
          idx          = idx
          rule         = rule
          source_type  = source.type
          source_value = source.value
        }
      ]
    ]
  ])

  # Egress rules, step 2: key each expanded entry for for_each.
  # The index keeps keys unique when a rule expands to multiple sources.
  egress_rule_map = {
    for i, item in local.egress_sources :
    "${item.sg_key}-egress-${item.idx}-${item.source_type}-${i}" => item
  }
}