@@ -21,122 +21,45 @@ resource "aws_security_group" "this" {
2121# ##################################################
2222
2323locals {
24- flattened_ingress_rules = concat (
25- [
26- for rule in var . ingress_rules :
27- rule
28- if length (lookup (rule, " source_security_group_ids" , [])) < 1
29- ],
30- flatten ([
31- for rule in var . ingress_rules : [
32- for source_security_group_id in rule . source_security_group_ids :
33- merge (rule, {
34- source_security_group_id = source_security_group_id
35- })
36- ] if length (lookup (rule, " source_security_group_ids" , [])) > 0
37- ])
38- )
39- flattened_egress_rules = concat (
40- [
41- for rule in var . egress_rules :
42- rule
43- if length (lookup (rule, " source_security_group_ids" , [])) < 1
44- ],
45- flatten ([
46- for rule in var . egress_rules : [
47- for source_security_group_id in rule . source_security_group_ids :
48- merge (rule, {
49- source_security_group_id = source_security_group_id
50- })
51- ] if length (lookup (rule, " source_security_group_ids" , [])) > 0
52- ])
53- )
54-
5524 normalized_ingress_rules = [
56- for rule in local . flattened_ingress_rules : {
25+ for rule in var . ingress_rules : {
26+ id = rule . id
5727 description = lookup (rule, " description" , " Managed by Terraform" )
5828
5929 protocol = rule . protocol
6030 from_port = rule . from_port
6131 to_port = rule . to_port
6232
63- cidr_blocks = lookup (rule, " cidr_blocks " , null ) != null ? sort (compact (rule. cidr_blocks )) : null
64- ipv6_cidr_blocks = lookup (rule, " ipv6_cidr_blocks " , null ) != null ? sort (compact (rule. ipv6_cidr_blocks )) : null
65- prefix_list_ids = lookup (rule, " prefix_list_ids " , null ) != null ? sort (compact (rule. prefix_list_ids )) : null
66- source_security_group_id = lookup (rule, " source_security_group_id" , null )
67- self = lookup (rule, " self" , null ) != null ? true : null
33+ cidr_blocks = try ( sort (compact (rule. cidr_blocks )), null )
34+ ipv6_cidr_blocks = try ( sort (compact (rule. ipv6_cidr_blocks )), null )
35+ prefix_list_ids = try ( sort (compact (rule. prefix_list_ids )), null )
36+ source_security_group_id = try (rule. source_security_group_id , null )
37+ self = try (rule. self , false ) ? true : null
6838 }
6939 ]
7040 normalized_egress_rules = [
71- for rule in local . flattened_egress_rules : {
41+ for rule in var . egress_rules : {
42+ id = rule . id
7243 description = lookup (rule, " description" , " Managed by Terraform" )
7344
7445 protocol = rule . protocol
7546 from_port = rule . from_port
7647 to_port = rule . to_port
7748
78- cidr_blocks = lookup (rule, " cidr_blocks " , null ) != null ? sort (compact (rule. cidr_blocks )) : null
79- ipv6_cidr_blocks = lookup (rule, " ipv6_cidr_blocks " , null ) != null ? sort (compact (rule. ipv6_cidr_blocks )) : null
80- prefix_list_ids = lookup (rule, " prefix_list_ids " , null ) != null ? sort (compact (rule. prefix_list_ids )) : null
81- source_security_group_id = lookup (rule, " source_security_group_id" , null )
82- self = lookup (rule, " self" , null ) != null ? true : null
49+ cidr_blocks = try ( sort (compact (rule. cidr_blocks )), null )
50+ ipv6_cidr_blocks = try ( sort (compact (rule. ipv6_cidr_blocks )), null )
51+ prefix_list_ids = try ( sort (compact (rule. prefix_list_ids )), null )
52+ source_security_group_id = try (rule. source_security_group_id , null )
53+ self = try (rule. self , false ) ? true : null
8354 }
8455 ]
85-
86- # Filter if empty
87- compacted_ingress_rules = [
88- for rule in local . normalized_ingress_rules :
89- rule
90- if length (compact (flatten ([
91- rule . cidr_blocks ,
92- rule . ipv6_cidr_blocks ,
93- rule . prefix_list_ids ,
94- rule . source_security_group_id ,
95- rule . self != null ? " self" : null ,
96- ]))) > 0
97- ]
98- compacted_egress_rules = [
99- for rule in local . normalized_egress_rules :
100- rule
101- if length (compact (flatten ([
102- rule . cidr_blocks ,
103- rule . ipv6_cidr_blocks ,
104- rule . prefix_list_ids ,
105- rule . source_security_group_id ,
106- rule . self != null ? " self" : null ,
107- ]))) > 0
108- ]
109-
110- ingress_rules = {
111- for rule in local . compacted_ingress_rules :
112- join (" _" , compact (flatten ([
113- rule . protocol ,
114- rule . from_port ,
115- rule . to_port ,
116- rule . cidr_blocks ,
117- rule . ipv6_cidr_blocks ,
118- rule . prefix_list_ids ,
119- rule . source_security_group_id ,
120- rule . self != null ? " self" : null ,
121- ]))) => rule
122- }
123- egress_rules = {
124- for rule in local . compacted_egress_rules :
125- join (" _" , compact (flatten ([
126- rule . protocol ,
127- rule . from_port ,
128- rule . to_port ,
129- rule . cidr_blocks ,
130- rule . ipv6_cidr_blocks ,
131- rule . prefix_list_ids ,
132- rule . source_security_group_id ,
133- rule . self != null ? " self" : null ,
134- ]))) => rule
135- }
13656}
13757
13858resource "aws_security_group_rule" "ingress" {
139- for_each = local. ingress_rules
59+ for_each = {
60+ for rule in local . normalized_ingress_rules :
61+ rule.id = > rule
62+ }
14063
14164 security_group_id = aws_security_group. this . id
14265 type = " ingress"
@@ -154,7 +77,10 @@ resource "aws_security_group_rule" "ingress" {
15477}
15578
15679resource "aws_security_group_rule" "egress" {
157- for_each = local. egress_rules
80+ for_each = {
81+ for rule in local . normalized_egress_rules :
82+ rule.id = > rule
83+ }
15884
15985 security_group_id = aws_security_group. this . id
16086 type = " egress"
0 commit comments