-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.tf
196 lines (153 loc) · 6 KB
/
security.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
resource "aws_security_group" "lb_sg" {
count = local.create_lb ? 1 : 0
description = "Controls access to the ${local.name} load balancer"
vpc_id = local.vpc_id
name_prefix = local.load_balancer_sg_name
tags = merge(
local.tags,
{ Name = "${local.name} LB SG" },
)
}
resource "aws_security_group_rule" "lb_egress" {
count = local.create_lb ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow all traffic out"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = [
"0.0.0.0/0",
]
}
resource "aws_security_group_rule" "user_to_lb_http_cidrs" {
count = local.create_http_listeners && local.create_cidr_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow HTTP traffic from the user to the lb for specific CIDRs"
type = "ingress"
protocol = "tcp"
from_port = var.http_port
to_port = var.http_port
cidr_blocks = var.restricted_cidr_blocks
}
resource "aws_security_group_rule" "user_to_lb_http_sgs" {
count = local.create_http_listeners && local.create_sg_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow HTTP traffic from the user to the lb for specific SGs"
type = "ingress"
protocol = "tcp"
from_port = var.http_port
to_port = var.http_port
source_security_group_id = var.restricted_sg
}
resource "aws_security_group_rule" "user_to_lb_https_cidrs" {
count = local.create_https_listeners && local.create_cidr_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow HTTPS traffic from the user to the lb"
type = "ingress"
protocol = "tcp"
from_port = var.https_port
to_port = var.https_port
cidr_blocks = var.restricted_cidr_blocks
}
resource "aws_security_group_rule" "user_to_lb_https_sgs" {
count = local.create_https_listeners && local.create_sg_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow HTTPS traffic from the user to the lb"
type = "ingress"
protocol = "tcp"
from_port = var.https_port
to_port = var.https_port
source_security_group_id = var.restricted_sg
}
resource "aws_security_group_rule" "user_to_nlb_cidrs" {
count = local.create_nlb && local.create_cidr_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow TCP traffic from the user to the nlb"
type = "ingress"
protocol = "tcp"
from_port = var.tcp_port
to_port = var.tcp_port
cidr_blocks = var.restricted_cidr_blocks
}
resource "aws_security_group_rule" "user_to_nlb_sgs" {
count = local.create_nlb && local.create_sg_access_rule ? 1 : 0
security_group_id = aws_security_group.lb_sg[0].id
description = "Allow TCP traffic from the user to the nlb"
type = "ingress"
protocol = "tcp"
from_port = var.tcp_port
to_port = var.tcp_port
source_security_group_id = var.restricted_sg
}
resource "aws_security_group" "service_sg" {
description = "Controls access to the ${local.name} service resources"
vpc_id = local.vpc_id
name_prefix = local.service_sg_name
tags = merge(
local.tags,
{ Name = "${local.name} Service SG" },
)
}
## Allow All Traffic out anywhere for egress
resource "aws_security_group_rule" "service_egress" {
security_group_id = aws_security_group.service_sg.id
description = "Allow all traffic out"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = [
"0.0.0.0/0",
]
}
resource "aws_security_group_rule" "lb_to_service" {
count = local.create_lb ? 1 : 0
security_group_id = aws_security_group.service_sg.id
description = "Allow container port traffic from the lb to the ECS service"
type = "ingress"
protocol = "tcp"
from_port = var.container_port
to_port = var.container_port
source_security_group_id = aws_security_group.lb_sg[0].id
}
resource "aws_security_group_rule" "nlb_service_access_cidr" {
count = local.create_nlb && local.create_nlb_cidr_access_rule ? 1 : 0
security_group_id = aws_security_group.service_sg.id
description = "Allow container port traffic to the ECS service for specific CIDR blocks"
type = "ingress"
protocol = "tcp"
from_port = var.container_port
to_port = var.container_port
cidr_blocks = var.restricted_cidr_blocks
}
resource "aws_security_group_rule" "nlb_service_access_sg" {
count = local.create_nlb && local.create_nlb_sg_access_rule ? 1 : 0
security_group_id = aws_security_group.service_sg.id
description = "Allow container port traffic to the ECS service for specific SGs"
type = "ingress"
protocol = "tcp"
from_port = var.container_port
to_port = var.container_port
source_security_group_id = var.restricted_sg
}
resource "aws_security_group_rule" "user_to_virtual_node_access_cidr" {
count = local.create_virtual_node_cidr_access_rule ? 1 : 0
description = "Allow container port traffic to the ECS service for CIDR block"
security_group_id = aws_security_group.service_sg.id
type = "ingress"
protocol = "tcp"
from_port = var.container_port
to_port = var.container_port
cidr_blocks = var.restricted_cidr_blocks
}
resource "aws_security_group_rule" "user_to_virtual_node_access_sg" {
count = local.create_virtual_node_sg_access_rule ? 1 : 0
security_group_id = aws_security_group.service_sg.id
description = "Allow container port traffic to the ECS service for specific SGs"
type = "ingress"
protocol = "tcp"
from_port = var.container_port
to_port = var.container_port
source_security_group_id = var.restricted_sg
}