-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
295 lines (233 loc) · 9.94 KB
/
main.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* -------------------------------------------------------------------------- */
/* ECS Cluster */
/* -------------------------------------------------------------------------- */
resource "aws_ecs_cluster" "this" {
name = format("%s-cluster", local.cluster_name)
dynamic "setting" {
for_each = var.is_enable_container_insights ? [true] : []
content {
name = "containerInsights"
value = "enabled"
}
}
tags = merge(
local.tags,
{ "Name" = format("%s-cluster", local.cluster_name) }
)
}
/* -------------------------------------------------------------------------- */
/* ECS Security Group */
/* -------------------------------------------------------------------------- */
resource "aws_security_group" "ecs_tasks" {
count = var.is_create_ecs_task_security_group ? 1 : 0
name = format("%s-ecs-tasks-sg", local.cluster_name)
description = format("Security group for ECS task %s-ecs-tasks-sg", local.cluster_name)
vpc_id = var.vpc_id
tags = merge(local.tags, { "Name" = format("%s-ecs-tasks-sg", local.cluster_name) })
}
resource "aws_security_group_rule" "ecs_tasks_ingress" {
for_each = var.additional_security_group_ingress_rules
type = "ingress"
from_port = lookup(each.value, "from_port", lookup(each.value, "port", null))
to_port = lookup(each.value, "to_port", lookup(each.value, "port", null))
protocol = lookup(each.value, "protocol", null)
security_group_id = local.ecs_task_security_group_id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
description = lookup(each.value, "description", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
source_security_group_id = lookup(each.value, "source_security_group_id", null)
}
resource "aws_security_group_rule" "tasks_to_tasks_all" {
count = var.is_create_ecs_task_security_group ? 1 : 0
security_group_id = local.ecs_task_security_group_id
source_security_group_id = local.ecs_task_security_group_id
type = "ingress"
from_port = 0
to_port = 65535
protocol = "-1"
}
resource "aws_security_group_rule" "tasks_to_world" {
count = var.is_create_ecs_task_security_group ? 1 : 0
security_group_id = local.ecs_task_security_group_id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# We want all Fargate traffic to come from the ALB and within the subnet
# We aren't locking down ports but that it must come from ALB
resource "aws_security_group_rule" "alb_to_tasks" {
count = var.is_create_ecs_task_security_group ? 1 : 0
security_group_id = local.ecs_task_security_group_id
source_security_group_id = local.alb_aws_security_group_id
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
}
/* -------------------------------------------------------------------------- */
/* ALB Security Group */
/* -------------------------------------------------------------------------- */
resource "aws_security_group" "alb" {
count = var.is_create_alb_security_group ? 1 : 0
name = format("%s-alb-sg", local.cluster_name)
description = format("Security group for ALB %s-alb", local.cluster_name)
vpc_id = var.vpc_id
tags = merge(local.tags, { "Name" = format("%s-alb-sg", local.cluster_name) })
}
resource "aws_security_group_rule" "public_to_alb" {
count = var.is_create_alb_security_group && var.alb_listener_port == 443 ? 1 : 0
security_group_id = local.alb_aws_security_group_id
type = "ingress"
from_port = var.alb_listener_port
to_port = var.alb_listener_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "public_to_alb_http" {
count = var.is_create_alb_security_group ? 1 : 0
security_group_id = local.alb_aws_security_group_id
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "leaving_alb" {
count = var.is_create_alb_security_group ? 1 : 0
security_group_id = local.alb_aws_security_group_id
source_security_group_id = local.ecs_task_security_group_id
type = "egress"
from_port = 0
to_port = 0
protocol = -1
}
resource "aws_security_group_rule" "alb_ingress" {
for_each = var.additional_security_group_alb_ingress_rules
type = "ingress"
from_port = lookup(each.value, "from_port", lookup(each.value, "port", null))
to_port = lookup(each.value, "to_port", lookup(each.value, "port", null))
protocol = lookup(each.value, "protocol", null)
security_group_id = local.alb_aws_security_group_id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
description = lookup(each.value, "description", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
source_security_group_id = lookup(each.value, "source_security_group_id", null)
}
/* -------------------------------------------------------------------------- */
/* ALB */
/* -------------------------------------------------------------------------- */
# Define the routing for the workloads
# Application Load Balancer Creation (ALB) in the DMZ
resource "aws_lb" "this" {
count = var.is_create_alb ? 1 : 0
name = var.is_public_alb ? format("%s-alb", local.cluster_name) : format("%s-internal-alb", local.cluster_name)
load_balancer_type = "application"
internal = !var.is_public_alb
subnets = var.is_public_alb ? var.public_subnet_ids : var.private_subnet_ids
security_groups = [local.alb_aws_security_group_id]
drop_invalid_header_fields = true
enable_deletion_protection = var.enable_deletion_protection
dynamic "access_logs" {
for_each = var.is_enable_access_log ? [true] : []
content {
bucket = try(var.alb_access_logs_bucket_name, null)
prefix = format("%s-alb", local.cluster_name)
enabled = var.is_enable_access_log
}
}
tags = merge(local.tags, { "Name" : var.is_public_alb ? format("%s-alb", local.cluster_name) : format("%s-internal-alb", local.cluster_name) })
}
resource "aws_lb_listener" "http" {
count = var.is_create_alb ? 1 : 0
load_balancer_arn = aws_lb.this[0].id
port = var.alb_listener_port
protocol = var.alb_listener_port == 443 ? "HTTPS" : "HTTP"
certificate_arn = var.alb_listener_port == 443 ? var.alb_certificate_arn : ""
ssl_policy = var.alb_listener_port == 443 ? "ELBSecurityPolicy-FS-1-2-Res-2019-08" : ""
default_action {
type = "fixed-response"
fixed_response {
content_type = var.default_fixed_response.content_type
message_body = try(var.default_fixed_response.message_body, null)
status_code = try(var.default_fixed_response.status_code, null)
}
order = try(var.default_fixed_response.order, null)
}
}
resource "aws_lb_listener" "front_end_https_http_redirect" {
# If not var.alb_listener_port == 443, the listener rule will overlap and raise error
count = var.is_create_alb && var.alb_listener_port == 443 ? 1 : 0
depends_on = [
aws_lb_listener.http
]
load_balancer_arn = aws_lb.this[0].id
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
/* -------------------------------------------------------------------------- */
/* DNS */
/* -------------------------------------------------------------------------- */
# Setup DNS discovery
resource "aws_service_discovery_private_dns_namespace" "internal" {
# This name does not follow convention because it is used as part of the domain name
name = "${local.cluster_name}.internal"
description = "Service Discovery for internal communcation for ${local.cluster_name} ECS cluster"
vpc = var.vpc_id
tags = merge(local.tags, { "Name" : format("%s.internal", local.cluster_name) })
}
# /* --------------------------------- Route53 -------------------------------- */
module "application_record" {
source = "oozou/route53/aws"
version = "1.0.2"
count = var.is_create_alb && var.is_create_alb_dns_record ? 1 : 0
is_create_zone = false
is_public_zone = true # Default `true`
prefix = var.prefix
environment = var.environment
dns_name = var.route53_hosted_zone_name
dns_records = {
application_record = {
name = replace(var.fully_qualified_domain_name, ".${var.route53_hosted_zone_name}", "") # Auto append with dns_name
type = "A"
alias = {
name = aws_lb.this[0].dns_name # Target DNS name
zone_id = aws_lb.this[0].zone_id
evaluate_target_health = true
}
}
}
}
/* -------------------------------------------------------------------------- */
/* IAM Role */
/* -------------------------------------------------------------------------- */
resource "aws_iam_role" "this" {
count = var.is_create_role ? 1 : 0
name = format("%s-ecs-access-role", local.cluster_name)
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
AWS = var.allow_access_from_principals
}
},
]
})
managed_policy_arns = var.additional_managed_policy_arns
tags = merge(local.tags, { "Name" : format("%s-ecs-access-role", local.cluster_name) })
}