-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecurity-groups.tf
274 lines (244 loc) · 10.6 KB
/
security-groups.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#######################################
# Security Group - VPC Default Deny All
#######################################
resource "aws_default_security_group" "this" {
vpc_id = module.vpc.vpc_id
tags = merge(var.tags, { Name = "DoNotUse" })
# TODO This is a CommandSynter limitation and should be addressed
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow outgoing connections"
}
# ingress {
# from_port = 0
# to_port = 0
# protocol = -1
# self = true
# description = "Deny all traffic on VPCs Default Security Group"
# }
}
#################################
# Security Group - Load Balancer
#################################
resource "aws_security_group" "load_balancer" {
name = "${var.namespace}-load-balancer"
description = "Allow HTTP/HTTPS Traffic From Internet"
vpc_id = module.vpc.vpc_id
tags = merge(var.tags, { Name = "LoadBalancer" })
ingress {
from_port = 80
to_port = 80
protocol = "6"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming HTTP traffic from the Internet"
}
ingress {
from_port = 443
to_port = 443
protocol = "6"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming HTTPS traffic from the Internet"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow outgoing connections"
}
}
############################
# Security Group - ECS Tasks
############################
resource "aws_security_group" "ecs_tasks" {
description = "ECS Tasks traffic rules"
vpc_id = module.vpc.vpc_id
name = "${var.namespace}-ecs-tasks"
tags = merge(var.tags, { Name = "ECSTasks" })
}
#######################################################
# Security Group Rule - Allow incoming traffic from ALB
#######################################################
resource "aws_security_group_rule" "ecs_alb_access" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.load_balancer.id
description = "Allow incoming connections from ALB"
security_group_id = aws_security_group.ecs_tasks.id
}
######################################################################
# Security Group Rule - Allow internal communication between ECS tasks
######################################################################
resource "aws_security_group_rule" "ecs_internal_access" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.ecs_tasks.id
description = "Allow internal communication between ECS tasks"
security_group_id = aws_security_group.ecs_tasks.id
}
#################################################################
# Security Group Rule - Allow outgoing connections from ECS tasks
#################################################################
resource "aws_security_group_rule" "ecs_egress_access" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow outgoing connections from ECS tasks"
security_group_id = aws_security_group.ecs_tasks.id
}
####################################
# Security Groups - Database Traffic
####################################
resource "aws_security_group" "database" {
name = "${var.namespace}-database-access"
description = "Database traffic rules"
vpc_id = module.vpc.vpc_id
tags = merge(var.tags, { Name = "Database" })
}
###########################################################
# Security Group Rule - Allows ECS Tasks access to database
###########################################################
resource "aws_security_group_rule" "database_ecs_access" {
type = "ingress"
from_port = local.database_port
to_port = local.database_port
protocol = "6"
source_security_group_id = aws_security_group.ecs_tasks.id
description = "Allow incoming connections from ECS Tasks"
security_group_id = aws_security_group.database.id
}
# TODO This is a CommandSynter limitation and should be addressed
###########################################################################
# Security Group Rule - Allows CommandSynter Deployments access to database
###########################################################################
resource "aws_security_group_rule" "database_command_synter" {
type = "ingress"
from_port = local.database_port
to_port = local.database_port
protocol = "6"
source_security_group_id = aws_default_security_group.this.id
description = "Allow incoming connections from CommandSynter Deployment Tasks"
security_group_id = aws_security_group.database.id
}
########################################################################################
# Security Group Rule - Allows Public Database Acccess (Used only during provisioning)
########################################################################################
resource "aws_security_group_rule" "database_public_access" {
count = var.database_publicly_accessible ? 1 : 0
type = "ingress"
from_port = local.database_port
to_port = local.database_port
protocol = "6"
cidr_blocks = var.database_public_cidrs
description = "Allow incoming connections from public networks"
security_group_id = aws_security_group.database.id
}
################################
# Security Group - Redis Traffic
################################
resource "aws_security_group" "redis" {
count = var.use_redis ? 1 : 0
name = "${var.namespace}-redis-access"
description = "Redis traffic rules"
vpc_id = module.vpc.vpc_id
tags = merge(var.tags, { Name = "Redis" })
}
########################################################
# Security Group Rule - Allows ECS Tasks access to Redis
########################################################
resource "aws_security_group_rule" "redis_ecs_access" {
count = var.use_redis ? 1 : 0
type = "ingress"
from_port = 6379
to_port = 6379
protocol = "6"
source_security_group_id = aws_security_group.ecs_tasks.id
description = "Allow incoming connections from ECS Tasks"
security_group_id = aws_security_group.redis[0].id
}
# TODO This is a CommandSynter limitation and should be addressed
#############################################################################
# Security Group Rule - Allows CommandSynter Deployment Tasks access to Redis
#############################################################################
resource "aws_security_group_rule" "redis_command_synter" {
count = var.use_redis ? 1 : 0
type = "ingress"
from_port = 6379
to_port = 6379
protocol = "6"
source_security_group_id = aws_default_security_group.this.id
description = "Allow incoming connections from CommandSynter Deployment Tasks"
security_group_id = aws_security_group.redis[0].id
}
########################################
# Security Group - ElasticSearch Traffic
########################################
resource "aws_security_group" "elasticsearch" {
count = var.use_elasticsearch ? 1 : 0
name = "${var.namespace}-elasticsearch-access"
description = "ElasticSearch traffic rules"
vpc_id = module.vpc.vpc_id
tags = merge(var.tags, { Name = "ElasticSearch" })
}
#####################################################################
# Security Group Rule - Allow outgoing connections from ElasticSearch
#####################################################################
resource "aws_security_group_rule" "elasticsearch_egress_access" {
count = var.use_elasticsearch ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow outgoing connections from ElasticSearch"
security_group_id = aws_security_group.elasticsearch[0].id
}
#####################################################################
# Security Group Rule - Allows ElasticSearch Node to Node connections
#####################################################################
resource "aws_security_group_rule" "elasticsearch_node_access" {
count = var.use_elasticsearch ? 1 : 0
type = "ingress"
from_port = 9300
to_port = 9300
protocol = "6"
source_security_group_id = aws_security_group.elasticsearch[0].id
description = "Allow incoming connections from ECS Tasks"
security_group_id = aws_security_group.elasticsearch[0].id
}
################################################################
# Security Group Rule - Allows ECS Tasks access to ElasticSearch
################################################################
resource "aws_security_group_rule" "elasticsearch_ecs_access" {
count = var.use_elasticsearch ? length(local.es_ports) : 0
type = "ingress"
from_port = local.es_ports[count.index]
to_port = local.es_ports[count.index]
protocol = "6"
source_security_group_id = aws_security_group.ecs_tasks.id
description = "Allow incoming connections from ECS Tasks"
security_group_id = aws_security_group.elasticsearch[0].id
}
# TODO This is a CommandSynter limitation and should be addressed
#####################################################################################
# Security Group Rule - Allows CommandSynter Deployment Tasks access to ElasticSearch
#####################################################################################
resource "aws_security_group_rule" "elasticsearch_command_synter" {
count = var.use_elasticsearch ? length(local.es_ports) : 0
type = "ingress"
from_port = local.es_ports[count.index]
to_port = local.es_ports[count.index]
protocol = "6"
source_security_group_id = aws_default_security_group.this.id
description = "Allow incoming connections from CommandSynter Deployment Tasks"
security_group_id = aws_security_group.elasticsearch[0].id
}