@@ -16,8 +16,12 @@ module "labels" {
16
16
managedby = var. managedby
17
17
label_order = var. label_order
18
18
}
19
+
19
20
locals {
20
- security_group_count = var. enable_security_group == true ? 1 : 0
21
+ security_group_count = var. enable_security_group == true ? 1 : 0
22
+ enable_cidr_rules = var. enable_security_group && (length (var. allowed_ip ) > 0 )
23
+ enable_source_sec_group_rules = var. enable_security_group && (length (var. security_groups ) > 0 )
24
+ ports_source_sec_group_product = setproduct (compact (var. allowed_ports ), compact (var. security_groups ))
21
25
}
22
26
23
27
# Module : SECURITY GROUP
@@ -53,7 +57,7 @@ resource "aws_security_group_rule" "egress" {
53
57
# Description : Provides a security group rule resource. Represents a single ingress
54
58
# group rule, which can be added to external Security Groups.
55
59
resource "aws_security_group_rule" "ingress" {
56
- count = var . enable_security_group == true ? length (compact (var. allowed_ports )) : 0
60
+ count = local . enable_cidr_rules == true ? length (compact (var. allowed_ports )) : 0
57
61
58
62
type = " ingress"
59
63
from_port = element (var. allowed_ports , count. index )
@@ -62,3 +66,14 @@ resource "aws_security_group_rule" "ingress" {
62
66
cidr_blocks = var. allowed_ip
63
67
security_group_id = aws_security_group. default [0 ]. id
64
68
}
69
+
70
+ resource "aws_security_group_rule" "ingress_sg" {
71
+ count = local. enable_source_sec_group_rules == true ? length (local. ports_source_sec_group_product ) : 0
72
+
73
+ type = " ingress"
74
+ from_port = element (element (local. ports_source_sec_group_product , count. index ), 0 )
75
+ to_port = element (element (local. ports_source_sec_group_product , count. index ), 0 )
76
+ protocol = var. protocol
77
+ source_security_group_id = element (element (local. ports_source_sec_group_product , count. index ), 1 )
78
+ security_group_id = aws_security_group. default [0 ]. id
79
+ }
0 commit comments