-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
main.tf
1013 lines (896 loc) · 40 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
locals {
enabled = module.this.enabled
ecs_service_enabled = local.enabled && var.ecs_service_enabled
task_role_arn = try(var.task_role_arn[0], tostring(var.task_role_arn), "")
create_task_role = local.enabled && length(var.task_role_arn) == 0
task_exec_role_arn = try(var.task_exec_role_arn[0], tostring(var.task_exec_role_arn), "")
create_exec_role = local.enabled && length(var.task_exec_role_arn) == 0
enable_ecs_service_role = module.this.enabled && var.network_mode != "awsvpc" && length(var.ecs_load_balancers) >= 1
create_service_connect_tls_role = local.enabled && length(flatten(flatten(var.service_connect_configurations[*].service[*].tls[*]))) > 0 && length(compact(flatten(flatten(var.service_connect_configurations[*].service[*].tls[*].role_arn)))) == 0
create_security_group = local.enabled && var.network_mode == "awsvpc" && var.security_group_enabled
create_task_definition = local.enabled && length(var.task_definition) == 0
volumes = concat(var.docker_volumes, var.efs_volumes, var.fsx_volumes, var.bind_mount_volumes)
redeployment_trigger = var.force_new_deployment && var.redeploy_on_apply ? {
redeployment = plantimestamp()
} : {}
task_policy_arns_map = merge({ for i, a in var.task_policy_arns : format("_#%v_", i) => a }, var.task_policy_arns_map)
task_exec_policy_arns_map = merge({ for i, a in var.task_exec_policy_arns : format("_#%v_", i) => a }, var.task_exec_policy_arns_map)
}
module "task_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.create_task_role
attributes = ["task"]
context = module.this.context
}
module "service_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["service"]
context = module.this.context
}
module "exec_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.create_exec_role
attributes = ["exec"]
context = module.this.context
}
module "service_connect_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.create_service_connect_tls_role
attributes = ["service-connect-tls"]
context = module.this.context
}
resource "aws_ecs_task_definition" "default" {
count = local.create_task_definition ? 1 : 0
family = module.this.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
ipc_mode = var.ipc_mode
pid_mode = var.pid_mode
execution_role_arn = length(local.task_exec_role_arn) > 0 ? local.task_exec_role_arn : one(aws_iam_role.ecs_exec[*]["arn"])
task_role_arn = length(local.task_role_arn) > 0 ? local.task_role_arn : one(aws_iam_role.ecs_task[*]["arn"])
track_latest = var.track_latest
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 "ephemeral_storage" {
for_each = var.ephemeral_storage_size == 0 ? [] : [var.ephemeral_storage_size]
content {
size_in_gib = var.ephemeral_storage_size
}
}
dynamic "placement_constraints" {
for_each = var.task_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
dynamic "runtime_platform" {
for_each = var.runtime_platform
content {
operating_system_family = lookup(runtime_platform.value, "operating_system_family", null)
cpu_architecture = lookup(runtime_platform.value, "cpu_architecture", null)
}
}
dynamic "volume" {
for_each = local.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)
}
}
}
}
dynamic "fsx_windows_file_server_volume_configuration" {
for_each = lookup(volume.value, "fsx_windows_file_server_volume_configuration", [])
content {
file_system_id = lookup(fsx_windows_file_server_volume_configuration.value, "file_system_id", null)
root_directory = lookup(fsx_windows_file_server_volume_configuration.value, "root_directory", null)
dynamic "authorization_config" {
for_each = lookup(fsx_windows_file_server_volume_configuration.value, "authorization_config", [])
content {
credentials_parameter = lookup(authorization_config.value, "credentials_parameter", null)
domain = lookup(authorization_config.value, "domain", null)
}
}
}
}
}
}
tags = var.use_old_arn ? null : module.this.tags
}
# IAM
data "aws_iam_policy_document" "ecs_task" {
count = local.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 = local.create_task_role ? 1 : 0
name = module.task_label.id
assume_role_policy = one(data.aws_iam_policy_document.ecs_task[*]["json"])
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = var.role_tags_enabled ? module.task_label.tags : null
}
resource "aws_iam_role_policy_attachment" "ecs_task" {
for_each = local.create_task_role ? local.task_policy_arns_map : {}
policy_arn = each.value
role = one(aws_iam_role.ecs_task[*]["id"])
}
data "aws_iam_policy_document" "ecs_service" {
count = local.enabled ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_service" {
count = local.enable_ecs_service_role && var.service_role_arn == null ? 1 : 0
name = module.service_label.id
assume_role_policy = one(data.aws_iam_policy_document.ecs_service[*]["json"])
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = var.role_tags_enabled ? module.service_label.tags : null
}
data "aws_iam_policy_document" "ecs_service_policy" {
count = local.enable_ecs_service_role && var.service_role_arn == null ? 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 = local.enable_ecs_service_role && var.service_role_arn == null ? 1 : 0
name = module.service_label.id
policy = one(data.aws_iam_policy_document.ecs_service_policy[*]["json"])
role = one(aws_iam_role.ecs_service[*]["id"])
}
data "aws_iam_policy_document" "ecs_ssm_exec" {
count = local.create_task_role && var.exec_enabled ? 1 : 0
statement {
effect = "Allow"
resources = ["*"]
actions = [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
]
}
}
resource "aws_iam_role_policy" "ecs_ssm_exec" {
count = local.create_task_role && var.exec_enabled ? 1 : 0
name = module.task_label.id
policy = one(data.aws_iam_policy_document.ecs_ssm_exec[*]["json"])
role = one(aws_iam_role.ecs_task[*]["id"])
}
# IAM role that the Amazon ECS container agent and the Docker daemon can assume
data "aws_iam_policy_document" "ecs_task_exec" {
count = local.create_exec_role ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_exec" {
count = local.create_exec_role ? 1 : 0
name = module.exec_label.id
assume_role_policy = one(data.aws_iam_policy_document.ecs_task_exec[*]["json"])
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = var.role_tags_enabled ? module.exec_label.tags : null
}
data "aws_iam_policy_document" "ecs_exec" {
count = local.create_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" {
for_each = local.create_exec_role ? toset(["true"]) : toset([])
name = module.exec_label.id
policy = one(data.aws_iam_policy_document.ecs_exec[*]["json"])
role = one(aws_iam_role.ecs_exec[*]["id"])
}
resource "aws_iam_role_policy_attachment" "ecs_exec" {
for_each = local.create_exec_role ? local.task_exec_policy_arns_map : {}
policy_arn = each.value
role = one(aws_iam_role.ecs_exec[*]["id"])
}
# IAM role that Amazon ECS uses to enable TLS on Service Connect
data "aws_iam_policy_document" "ecs_service_connect_tls" {
count = local.create_service_connect_tls_role ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_service_connect_tls" {
count = local.create_service_connect_tls_role ? 1 : 0
name = module.service_connect_label.id
assume_role_policy = one(data.aws_iam_policy_document.ecs_service_connect_tls[*]["json"])
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = var.role_tags_enabled ? module.service_connect_label.tags : null
}
resource "aws_iam_role_policy_attachment" "ecs_service_connect_tls" {
count = local.create_service_connect_tls_role ? 1 : 0
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSInfrastructureRolePolicyForServiceConnectTransportLayerSecurity"
role = one(aws_iam_role.ecs_service_connect_tls[*]["id"])
}
# Service
## Security Groups
resource "aws_security_group" "ecs_service" {
count = local.create_security_group ? 1 : 0
vpc_id = var.vpc_id
name = module.service_label.id
description = var.security_group_description
tags = module.service_label.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "allow_all_egress" {
count = local.create_security_group && var.enable_all_egress_rule ? 1 : 0
description = "Allow all outbound traffic to any IPv4 address"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = one(aws_security_group.ecs_service[*]["id"])
}
resource "aws_security_group_rule" "allow_icmp_ingress" {
count = local.create_security_group && var.enable_icmp_rule ? 1 : 0
description = "Allow 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 = one(aws_security_group.ecs_service[*]["id"])
}
resource "aws_security_group_rule" "alb" {
count = local.create_security_group && var.use_alb_security_group ? 1 : 0
description = "Allow inbound traffic from ALB"
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 = one(aws_security_group.ecs_service[*]["id"])
}
resource "aws_security_group_rule" "nlb" {
count = local.create_security_group && var.use_nlb_cidr_blocks ? 1 : 0
description = "Allow inbound traffic from NLB"
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 = one(aws_security_group.ecs_service[*]["id"])
}
resource "aws_ecs_service" "ignore_changes_task_definition" {
count = local.ecs_service_enabled && var.ignore_changes_task_definition && !var.ignore_changes_desired_count ? 1 : 0
name = module.this.id
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
availability_zone_rebalancing = var.availability_zone_rebalancing
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 = local.enable_ecs_service_role ? coalesce(var.service_role_arn, one(aws_iam_role.ecs_service[*]["arn"])) : null
wait_for_steady_state = var.wait_for_steady_state
force_new_deployment = var.force_new_deployment
enable_execute_command = var.exec_enabled
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 "service_connect_configuration" {
for_each = var.service_connect_configurations
content {
enabled = service_connect_configuration.value.enabled
namespace = service_connect_configuration.value.namespace
dynamic "log_configuration" {
for_each = try(service_connect_configuration.value.log_configuration, null) == null ? [] : [service_connect_configuration.value.log_configuration]
content {
log_driver = log_configuration.value.log_driver
options = log_configuration.value.options
dynamic "secret_option" {
for_each = length(log_configuration.value.secret_option) == 0 ? [] : [log_configuration.value.secret_option]
content {
name = secret_option.value.name
value_from = secret_option.value.value_from
}
}
}
}
dynamic "service" {
for_each = length(service_connect_configuration.value.service) == 0 ? [] : service_connect_configuration.value.service
content {
discovery_name = service.value.discovery_name
ingress_port_override = service.value.ingress_port_override
port_name = service.value.port_name
dynamic "client_alias" {
for_each = service.value.client_alias
content {
dns_name = client_alias.value.dns_name
port = client_alias.value.port
}
}
dynamic "timeout" {
for_each = length(service.value.timeout) == 0 ? [] : service.value.timeout
content {
idle_timeout_seconds = timeout.value.idle_timeout_seconds
per_request_timeout_seconds = timeout.value.per_request_timeout_seconds
}
}
dynamic "tls" {
for_each = length(service.value.tls) == 0 ? [] : service.value.tls
content {
kms_key = tls.value.kms_key
role_arn = tls.value.role_arn != null ? tls.value.role_arn : one(aws_iam_role.ecs_service_connect_tls[*].arn)
issuer_cert_authority {
aws_pca_authority_arn = tls.value.issuer_cert_authority.aws_pca_authority_arn
}
}
}
}
}
}
}
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.this.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 = compact(concat(var.security_group_ids, aws_security_group.ecs_service[*]["id"]))
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
dynamic "deployment_circuit_breaker" {
for_each = var.deployment_controller_type == "ECS" ? ["true"] : []
content {
enable = var.circuit_breaker_deployment_enabled
rollback = var.circuit_breaker_rollback_enabled
}
}
triggers = local.redeployment_trigger
lifecycle {
ignore_changes = [task_definition]
}
# Avoid race condition on destroy.
# See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service
depends_on = [aws_iam_role.ecs_service, aws_iam_role_policy.ecs_service]
}
resource "aws_ecs_service" "ignore_changes_task_definition_and_desired_count" {
count = local.ecs_service_enabled && var.ignore_changes_task_definition && var.ignore_changes_desired_count ? 1 : 0
name = module.this.id
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
availability_zone_rebalancing = var.availability_zone_rebalancing
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 = local.enable_ecs_service_role ? coalesce(var.service_role_arn, one(aws_iam_role.ecs_service[*]["arn"])) : null
wait_for_steady_state = var.wait_for_steady_state
force_new_deployment = var.force_new_deployment
enable_execute_command = var.exec_enabled
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 "service_connect_configuration" {
for_each = var.service_connect_configurations
content {
enabled = service_connect_configuration.value.enabled
namespace = service_connect_configuration.value.namespace
dynamic "log_configuration" {
for_each = try(service_connect_configuration.value.log_configuration, null) == null ? [] : [service_connect_configuration.value.log_configuration]
content {
log_driver = log_configuration.value.log_driver
options = log_configuration.value.options
dynamic "secret_option" {
for_each = length(log_configuration.value.secret_option) == 0 ? [] : [log_configuration.value.secret_option]
content {
name = secret_option.value.name
value_from = secret_option.value.value_from
}
}
}
}
dynamic "service" {
for_each = length(service_connect_configuration.value.service) == 0 ? [] : service_connect_configuration.value.service
content {
discovery_name = service.value.discovery_name
ingress_port_override = service.value.ingress_port_override
port_name = service.value.port_name
dynamic "client_alias" {
for_each = service.value.client_alias
content {
dns_name = client_alias.value.dns_name
port = client_alias.value.port
}
}
dynamic "timeout" {
for_each = length(service.value.timeout) == 0 ? [] : service.value.timeout
content {
idle_timeout_seconds = timeout.value.idle_timeout_seconds
per_request_timeout_seconds = timeout.value.per_request_timeout_seconds
}
}
dynamic "tls" {
for_each = length(service.value.tls) == 0 ? [] : service.value.tls
content {
kms_key = tls.value.kms_key
role_arn = tls.value.role_arn != null ? tls.value.role_arn : one(aws_iam_role.ecs_service_connect_tls[*].arn)
issuer_cert_authority {
aws_pca_authority_arn = tls.value.issuer_cert_authority.aws_pca_authority_arn
}
}
}
}
}
}
}
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.this.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 = compact(concat(var.security_group_ids, aws_security_group.ecs_service[*]["id"]))
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
dynamic "deployment_circuit_breaker" {
for_each = var.deployment_controller_type == "ECS" ? ["true"] : []
content {
enable = var.circuit_breaker_deployment_enabled
rollback = var.circuit_breaker_rollback_enabled
}
}
triggers = local.redeployment_trigger
lifecycle {
ignore_changes = [task_definition, desired_count]
}
# Avoid race condition on destroy.
# See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service
depends_on = [aws_iam_role.ecs_service, aws_iam_role_policy.ecs_service]
}
resource "aws_ecs_service" "ignore_changes_desired_count" {
count = local.ecs_service_enabled && !var.ignore_changes_task_definition && var.ignore_changes_desired_count ? 1 : 0
name = module.this.id
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
availability_zone_rebalancing = var.availability_zone_rebalancing
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 = local.enable_ecs_service_role ? coalesce(var.service_role_arn, one(aws_iam_role.ecs_service[*]["arn"])) : null
wait_for_steady_state = var.wait_for_steady_state
force_new_deployment = var.force_new_deployment
enable_execute_command = var.exec_enabled
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 "service_connect_configuration" {
for_each = var.service_connect_configurations
content {
enabled = service_connect_configuration.value.enabled
namespace = service_connect_configuration.value.namespace
dynamic "log_configuration" {
for_each = try(service_connect_configuration.value.log_configuration, null) == null ? [] : [service_connect_configuration.value.log_configuration]
content {
log_driver = log_configuration.value.log_driver
options = log_configuration.value.options
dynamic "secret_option" {
for_each = length(log_configuration.value.secret_option) == 0 ? [] : [log_configuration.value.secret_option]
content {
name = secret_option.value.name
value_from = secret_option.value.value_from
}
}
}
}
dynamic "service" {
for_each = length(service_connect_configuration.value.service) == 0 ? [] : service_connect_configuration.value.service
content {
discovery_name = service.value.discovery_name
ingress_port_override = service.value.ingress_port_override
port_name = service.value.port_name
dynamic "client_alias" {
for_each = service.value.client_alias
content {
dns_name = client_alias.value.dns_name
port = client_alias.value.port
}
}
dynamic "timeout" {
for_each = length(service.value.timeout) == 0 ? [] : service.value.timeout
content {
idle_timeout_seconds = timeout.value.idle_timeout_seconds
per_request_timeout_seconds = timeout.value.per_request_timeout_seconds
}
}
dynamic "tls" {
for_each = length(service.value.tls) == 0 ? [] : service.value.tls
content {
kms_key = tls.value.kms_key
role_arn = tls.value.role_arn != null ? tls.value.role_arn : one(aws_iam_role.ecs_service_connect_tls[*].arn)
issuer_cert_authority {
aws_pca_authority_arn = tls.value.issuer_cert_authority.aws_pca_authority_arn
}
}
}
}
}
}
}
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.this.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 = compact(concat(var.security_group_ids, aws_security_group.ecs_service[*]["id"]))
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
dynamic "deployment_circuit_breaker" {
for_each = var.deployment_controller_type == "ECS" ? ["true"] : []
content {
enable = var.circuit_breaker_deployment_enabled
rollback = var.circuit_breaker_rollback_enabled
}
}
triggers = local.redeployment_trigger
lifecycle {
ignore_changes = [desired_count]
}
# Avoid race condition on destroy.
# See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service
depends_on = [aws_iam_role.ecs_service, aws_iam_role_policy.ecs_service]
}
resource "aws_ecs_service" "default" {
count = local.ecs_service_enabled && !var.ignore_changes_task_definition && !var.ignore_changes_desired_count ? 1 : 0
name = module.this.id
task_definition = local.create_task_definition ? "${join("", aws_ecs_task_definition.default[*].family)}:${join("", aws_ecs_task_definition.default[*].revision)}" : var.task_definition[0]
desired_count = var.desired_count
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
availability_zone_rebalancing = var.availability_zone_rebalancing
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 = local.enable_ecs_service_role ? coalesce(var.service_role_arn, one(aws_iam_role.ecs_service[*]["arn"])) : null
wait_for_steady_state = var.wait_for_steady_state
force_new_deployment = var.force_new_deployment
enable_execute_command = var.exec_enabled
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 "service_connect_configuration" {
for_each = var.service_connect_configurations
content {
enabled = service_connect_configuration.value.enabled
namespace = service_connect_configuration.value.namespace
dynamic "log_configuration" {
for_each = try(service_connect_configuration.value.log_configuration, null) == null ? [] : [service_connect_configuration.value.log_configuration]
content {
log_driver = log_configuration.value.log_driver
options = log_configuration.value.options
dynamic "secret_option" {
for_each = length(log_configuration.value.secret_option) == 0 ? [] : [log_configuration.value.secret_option]
content {
name = secret_option.value.name
value_from = secret_option.value.value_from
}
}
}
}
dynamic "service" {
for_each = length(service_connect_configuration.value.service) == 0 ? [] : service_connect_configuration.value.service
content {
discovery_name = service.value.discovery_name
ingress_port_override = service.value.ingress_port_override
port_name = service.value.port_name
dynamic "client_alias" {
for_each = service.value.client_alias
content {
dns_name = client_alias.value.dns_name
port = client_alias.value.port
}
}
dynamic "timeout" {
for_each = length(service.value.timeout) == 0 ? [] : service.value.timeout
content {
idle_timeout_seconds = timeout.value.idle_timeout_seconds
per_request_timeout_seconds = timeout.value.per_request_timeout_seconds
}
}
dynamic "tls" {
for_each = length(service.value.tls) == 0 ? [] : service.value.tls
content {
kms_key = tls.value.kms_key
role_arn = tls.value.role_arn != null ? tls.value.role_arn : one(aws_iam_role.ecs_service_connect_tls[*].arn)
issuer_cert_authority {
aws_pca_authority_arn = tls.value.issuer_cert_authority.aws_pca_authority_arn
}
}
}
}
}
}
}
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.this.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 = compact(concat(var.security_group_ids, aws_security_group.ecs_service[*]["id"]))
subnets = var.subnet_ids
assign_public_ip = var.assign_public_ip
}
}
dynamic "deployment_circuit_breaker" {
for_each = var.deployment_controller_type == "ECS" ? ["true"] : []