-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg.tf
81 lines (73 loc) · 2.57 KB
/
sg.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
locals {
security_groups_config = [ # https://docs.imperva.com/bundle/v4.11-data-risk-analytics-installation-guide/page/63052.htm
{
name = ["dra", "admin"]
internet_access = false
udp = []
tcp = [8443]
cidrs = concat(var.allowed_admin_cidrs, var.allowed_all_cidrs)
},
{
name = ["other"]
internet_access = true
udp = []
tcp = [22]
cidrs = concat(var.allowed_ssh_cidrs, var.allowed_all_cidrs)
},
{
name = ["agent", "gateway"]
internet_access = false
udp = []
tcp = [22]
cidrs = concat(var.allowed_agent_gateways_cidrs, var.allowed_all_cidrs)
},
{
name = ["hub"]
internet_access = false
udp = []
tcp = [22]
cidrs = concat(var.allowed_hub_cidrs, var.allowed_all_cidrs)
}
]
# Skip sg creation if external sg list is given
_security_groups_config = length(var.security_group_ids) == 0 ? local.security_groups_config : []
}
data "aws_subnet" "selected_subnet" {
id = var.subnet_id
}
##############################################################################
### Ingress security group
##############################################################################
resource "aws_security_group" "dsf_base_sg" {
for_each = { for idx, config in local._security_groups_config : idx => config }
name = join("-", [var.name, join("-", each.value.name)])
vpc_id = data.aws_subnet.selected_subnet.vpc_id
description = format("%s - %s ingress access", var.name, join(" ", each.value.name))
dynamic "ingress" {
for_each = { for idx, port in each.value.tcp : idx => port }
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = each.value.cidrs
}
}
dynamic "ingress" {
for_each = { for idx, port in each.value.udp : idx => port }
content {
from_port = ingress.value
to_port = ingress.value
protocol = "udp"
cidr_blocks = each.value.cidrs
}
}
# Conditionally assign egress rules based on a "internet_access" memeber
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = each.value.internet_access ? ["0.0.0.0/0"] : []
ipv6_cidr_blocks = each.value.internet_access ? ["::/0"] : []
}
tags = merge(var.tags, { Name = join("-", [var.name, join("-", each.value.name)]) })
}