forked from cloudposse/terraform-aws-ecs-alb-service-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
431 lines (376 loc) · 16.2 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
module "default_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.0"
enabled = var.enabled
attributes = var.attributes
delimiter = var.delimiter
name = var.name
namespace = var.namespace
stage = var.stage
environment = var.environment
tags = var.tags
}
module "task_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.0"
enabled = var.enabled && var.create_task_role
context = module.default_label.context
attributes = compact(concat(var.attributes, ["task"]))
}
module "service_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.0"
enabled = var.enabled
context = module.default_label.context
attributes = compact(concat(var.attributes, ["service"]))
}
module "exec_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.0"
enabled = var.enabled && var.create_task_exec_role
context = module.default_label.context
attributes = compact(concat(var.attributes, ["exec"]))
}
resource "aws_ecs_task_definition" "default" {
count = var.enabled ? 1 : 0
family = module.default_label.id
container_definitions = var.container_definition_json
requires_compatibilities = [var.launch_type]
network_mode = var.network_mode
cpu = var.task_cpu
memory = var.task_memory
execution_role_arn = var.create_task_exec_role == false ? var.task_exec_role_arn : join("", aws_iam_role.ecs_exec.*.arn)
task_role_arn = var.create_task_role == false ? var.task_role_arn : join("", aws_iam_role.ecs_task.*.arn)
dynamic "proxy_configuration" {
for_each = var.proxy_configuration == null ? [] : [var.proxy_configuration]
content {
type = lookup(proxy_configuration.value, "type", "APPMESH")
container_name = proxy_configuration.value.container_name
properties = proxy_configuration.value.properties
}
}
dynamic "placement_constraints" {
for_each = var.task_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
dynamic "volume" {
for_each = var.volumes
content {
host_path = lookup(volume.value, "host_path", null)
name = volume.value.name
dynamic "docker_volume_configuration" {
for_each = lookup(volume.value, "docker_volume_configuration", [])
content {
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
driver = lookup(docker_volume_configuration.value, "driver", null)
driver_opts = lookup(docker_volume_configuration.value, "driver_opts", null)
labels = lookup(docker_volume_configuration.value, "labels", null)
scope = lookup(docker_volume_configuration.value, "scope", null)
}
}
dynamic "efs_volume_configuration" {
for_each = lookup(volume.value, "efs_volume_configuration", [])
content {
file_system_id = lookup(efs_volume_configuration.value, "file_system_id", null)
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
transit_encryption = lookup(efs_volume_configuration.value, "transit_encryption", null)
transit_encryption_port = lookup(efs_volume_configuration.value, "transit_encryption_port", null)
dynamic "authorization_config" {
for_each = lookup(efs_volume_configuration.value, "authorization_config", [])
content {
access_point_id = lookup(authorization_config.value, "access_point_id", null)
iam = lookup(authorization_config.value, "iam", null)
}
}
}
}
}
}
tags = var.use_old_arn ? null : module.default_label.tags
}
# IAM
data "aws_iam_policy_document" "ecs_task" {
count = var.enabled && var.create_task_role ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_task" {
count = var.enabled && var.create_task_role ? 1 : 0
name = module.task_label.id
assume_role_policy = join("", data.aws_iam_policy_document.ecs_task.*.json)
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = module.task_label.tags
}
data "aws_iam_policy_document" "ecs_service" {
count = var.enabled ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_service" {
count = var.enabled && var.network_mode != "awsvpc" ? 1 : 0
name = module.service_label.id
assume_role_policy = join("", data.aws_iam_policy_document.ecs_service.*.json)
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = module.service_label.tags
}
data "aws_iam_policy_document" "ecs_service_policy" {
count = var.enabled && var.network_mode != "awsvpc" ? 1 : 0
statement {
effect = "Allow"
resources = ["*"]
actions = [
"elasticloadbalancing:Describe*",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"ec2:Describe*",
"ec2:AuthorizeSecurityGroupIngress",
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets"
]
}
}
resource "aws_iam_role_policy" "ecs_service" {
count = var.enabled && var.network_mode != "awsvpc" ? 1 : 0
name = module.service_label.id
policy = join("", data.aws_iam_policy_document.ecs_service_policy.*.json)
role = join("", aws_iam_role.ecs_service.*.id)
}
# IAM role that the Amazon ECS container agent and the Docker daemon can assume
data "aws_iam_policy_document" "ecs_task_exec" {
count = var.enabled && var.create_task_exec_role ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_exec" {
count = var.enabled && var.create_task_exec_role ? 1 : 0
name = module.exec_label.id
assume_role_policy = join("", data.aws_iam_policy_document.ecs_task_exec.*.json)
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = module.exec_label.tags
}
data "aws_iam_policy_document" "ecs_exec" {
count = var.enabled && var.create_task_exec_role ? 1 : 0
statement {
effect = "Allow"
resources = ["*"]
actions = [
"ssm:GetParameters",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
}
}
resource "aws_iam_role_policy" "ecs_exec" {
count = var.enabled && var.create_task_exec_role ? 1 : 0
name = module.exec_label.id
policy = join("", data.aws_iam_policy_document.ecs_exec.*.json)
role = join("", aws_iam_role.ecs_exec.*.id)
}
# Service
## Security Groups
resource "aws_security_group" "ecs_service" {
count = var.enabled && var.create_service_security_group ? 1 : 0
vpc_id = var.vpc_id
name = module.service_label.id
description = "Allow ALL egress from ECS service"
tags = module.service_label.tags
}
resource "aws_security_group_rule" "allow_all_egress" {
count = var.enabled && var.create_service_security_group && var.enable_all_egress_rule ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.ecs_service.*.id)
}
resource "aws_security_group_rule" "allow_icmp_ingress" {
count = var.enabled && var.create_service_security_group && var.enable_icmp_rule ? 1 : 0
description = "Enables ping command from anywhere, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-ping"
type = "ingress"
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.ecs_service.*.id)
}
resource "aws_security_group_rule" "alb" {
count = var.enabled && var.create_service_security_group && var.use_alb_security_group ? 1 : 0
type = "ingress"
from_port = var.container_port
to_port = var.container_port
protocol = "tcp"
source_security_group_id = var.alb_security_group
security_group_id = join("", aws_security_group.ecs_service.*.id)
}
resource "aws_security_group_rule" "nlb" {
count = var.enabled && var.create_service_security_group && var.use_nlb_cidr_blocks ? 1 : 0
type = "ingress"
from_port = var.nlb_container_port
to_port = var.nlb_container_port
protocol = "tcp"
cidr_blocks = var.nlb_cidr_blocks
security_group_id = join("", aws_security_group.ecs_service.*.id)
}
resource "aws_ecs_service" "ignore_changes_task_definition" {
count = var.enabled && var.ignore_changes_task_definition ? 1 : 0
name = module.default_label.id
task_definition = "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}"
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
health_check_grace_period_seconds = var.health_check_grace_period_seconds
launch_type = length(var.capacity_provider_strategies) > 0 ? null : var.launch_type
platform_version = var.launch_type == "FARGATE" ? var.platform_version : null
scheduling_strategy = var.launch_type == "FARGATE" ? "REPLICA" : var.scheduling_strategy
enable_ecs_managed_tags = var.enable_ecs_managed_tags
iam_role = var.network_mode != "awsvpc" ? join("", aws_iam_role.ecs_service.*.arn) : null
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategies
content {
capacity_provider = capacity_provider_strategy.value.capacity_provider
weight = capacity_provider_strategy.value.weight
base = lookup(capacity_provider_strategy.value, "base", null)
}
}
dynamic "service_registries" {
for_each = var.service_registries
content {
registry_arn = service_registries.value.registry_arn
port = lookup(service_registries.value, "port", null)
container_name = lookup(service_registries.value, "container_name", null)
container_port = lookup(service_registries.value, "container_port", null)
}
}
dynamic "ordered_placement_strategy" {
for_each = var.ordered_placement_strategy
content {
type = ordered_placement_strategy.value.type
field = lookup(ordered_placement_strategy.value, "field", null)
}
}
dynamic "placement_constraints" {
for_each = var.service_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
dynamic "load_balancer" {
for_each = var.ecs_load_balancers
content {
container_name = load_balancer.value.container_name
container_port = load_balancer.value.container_port
elb_name = lookup(load_balancer.value, "elb_name", null)
target_group_arn = lookup(load_balancer.value, "target_group_arn", null)
}
}
cluster = var.ecs_cluster_arn
propagate_tags = var.propagate_tags
tags = var.use_old_arn ? null : module.default_label.tags
deployment_controller {
type = var.deployment_controller_type
}
# https://www.terraform.io/docs/providers/aws/r/ecs_service.html#network_configuration
dynamic "network_configuration" {
for_each = var.network_mode == "awsvpc" ? ["true"] : []
content {
security_groups = var.create_service_security_group ? compact(concat(var.security_group_ids, aws_security_group.ecs_service.*.id)) : var.security_group_ids
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
lifecycle {
ignore_changes = [task_definition]
}
}
resource "aws_ecs_service" "default" {
count = var.enabled && var.ignore_changes_task_definition == false ? 1 : 0
name = module.default_label.id
task_definition = "${join("", aws_ecs_task_definition.default.*.family)}:${join("", aws_ecs_task_definition.default.*.revision)}"
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
health_check_grace_period_seconds = var.health_check_grace_period_seconds
launch_type = length(var.capacity_provider_strategies) > 0 ? null : var.launch_type
platform_version = var.launch_type == "FARGATE" ? var.platform_version : null
scheduling_strategy = var.launch_type == "FARGATE" ? "REPLICA" : var.scheduling_strategy
enable_ecs_managed_tags = var.enable_ecs_managed_tags
iam_role = var.network_mode != "awsvpc" ? join("", aws_iam_role.ecs_service.*.arn) : null
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategies
content {
capacity_provider = capacity_provider_strategy.value.capacity_provider
weight = capacity_provider_strategy.value.weight
base = lookup(capacity_provider_strategy.value, "base", null)
}
}
dynamic "service_registries" {
for_each = var.service_registries
content {
registry_arn = service_registries.value.registry_arn
port = lookup(service_registries.value, "port", null)
container_name = lookup(service_registries.value, "container_name", null)
container_port = lookup(service_registries.value, "container_port", null)
}
}
dynamic "ordered_placement_strategy" {
for_each = var.ordered_placement_strategy
content {
type = ordered_placement_strategy.value.type
field = lookup(ordered_placement_strategy.value, "field", null)
}
}
dynamic "placement_constraints" {
for_each = var.service_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
dynamic "load_balancer" {
for_each = var.ecs_load_balancers
content {
container_name = load_balancer.value.container_name
container_port = load_balancer.value.container_port
elb_name = lookup(load_balancer.value, "elb_name", null)
target_group_arn = lookup(load_balancer.value, "target_group_arn", null)
}
}
cluster = var.ecs_cluster_arn
propagate_tags = var.propagate_tags
tags = var.use_old_arn ? null : module.default_label.tags
deployment_controller {
type = var.deployment_controller_type
}
# https://www.terraform.io/docs/providers/aws/r/ecs_service.html#network_configuration
dynamic "network_configuration" {
for_each = var.network_mode == "awsvpc" ? ["true"] : []
content {
security_groups = var.create_service_security_group ? compact(concat(var.security_group_ids, aws_security_group.ecs_service.*.id)) : var.security_group_ids
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
}