forked from terraform-aws-modules/terraform-aws-atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
848 lines (683 loc) · 27.1 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
locals {
# VPC - existing or new?
vpc_id = var.vpc_id == "" ? module.vpc.vpc_id : var.vpc_id
private_subnet_ids = coalescelist(module.vpc.private_subnets, var.private_subnet_ids, [""])
public_subnet_ids = coalescelist(module.vpc.public_subnets, var.public_subnet_ids, [""])
# Atlantis
atlantis_image = var.atlantis_image == "" ? "ghcr.io/runatlantis/atlantis:${var.atlantis_version}" : var.atlantis_image
atlantis_url = "https://${coalesce(
var.atlantis_fqdn,
element(concat(aws_route53_record.atlantis[*].fqdn, [""]), 0),
module.alb.lb_dns_name,
"_"
)}"
atlantis_url_events = "${local.atlantis_url}/events"
# Include only one group of secrets - for github, github app, gitlab or bitbucket
has_secrets = try(coalesce(var.atlantis_gitlab_user_token, var.atlantis_github_user_token, var.atlantis_github_app_key, var.atlantis_bitbucket_user_token) != "", false)
# token/key
secret_name_key = local.has_secrets ? var.atlantis_gitlab_user_token != "" ? "ATLANTIS_GITLAB_TOKEN" : var.atlantis_github_user_token != "" ? "ATLANTIS_GH_TOKEN" : var.atlantis_github_app_key != "" ? "ATLANTIS_GH_APP_KEY" : "ATLANTIS_BITBUCKET_TOKEN" : ""
secret_name_value_from = local.has_secrets ? var.atlantis_gitlab_user_token != "" ? var.atlantis_gitlab_user_token_ssm_parameter_name : var.atlantis_github_user_token != "" ? var.atlantis_github_user_token_ssm_parameter_name : var.atlantis_github_app_key != "" ? var.atlantis_github_app_key_ssm_parameter_name : var.atlantis_bitbucket_user_token_ssm_parameter_name : ""
# webhook
secret_webhook_key = local.has_secrets || var.atlantis_github_webhook_secret != "" ? var.atlantis_gitlab_user_token != "" ? "ATLANTIS_GITLAB_WEBHOOK_SECRET" : var.atlantis_github_user_token != "" || var.atlantis_github_webhook_secret != "" ? "ATLANTIS_GH_WEBHOOK_SECRET" : var.atlantis_github_app_key != "" || var.atlantis_github_webhook_secret != "" ? "ATLANTIS_GH_WEBHOOK_SECRET" : "ATLANTIS_BITBUCKET_WEBHOOK_SECRET" : ""
# determine if the alb has authentication enabled, otherwise forward the traffic unauthenticated
alb_authentication_method = length(keys(var.alb_authenticate_oidc)) > 0 ? "authenticate-oidc" : length(keys(var.alb_authenticate_cognito)) > 0 ? "authenticate-cognito" : "forward"
# ECS - existing or new?
ecs_cluster_id = var.create_ecs_cluster ? module.ecs.ecs_cluster_id : var.ecs_cluster_id
# Container definitions
container_definitions = var.custom_container_definitions == "" ? var.atlantis_bitbucket_user_token != "" ? jsonencode(concat([module.container_definition_bitbucket.json_map_object], var.extra_container_definitions)) : jsonencode(concat([module.container_definition_github_gitlab.json_map_object], var.extra_container_definitions)) : var.custom_container_definitions
container_definition_environment = [
{
name = "ATLANTIS_ALLOW_REPO_CONFIG"
value = var.allow_repo_config
},
{
name = "ATLANTIS_GITLAB_HOSTNAME"
value = var.atlantis_gitlab_hostname
},
{
name = "ATLANTIS_LOG_LEVEL"
value = var.atlantis_log_level
},
{
name = "ATLANTIS_PORT"
value = var.atlantis_port
},
{
name = "ATLANTIS_ATLANTIS_URL"
value = local.atlantis_url
},
{
name = "ATLANTIS_GH_USER"
value = var.atlantis_github_user
},
{
name = "ATLANTIS_GITLAB_USER"
value = var.atlantis_gitlab_user
},
{
name = "ATLANTIS_BITBUCKET_USER"
value = var.atlantis_bitbucket_user
},
{
name = "ATLANTIS_BITBUCKET_BASE_URL"
value = var.atlantis_bitbucket_base_url
},
{
name = "ATLANTIS_REPO_ALLOWLIST"
value = join(",", var.atlantis_repo_allowlist)
},
{
name = "ATLANTIS_HIDE_PREV_PLAN_COMMENTS"
value = var.atlantis_hide_prev_plan_comments
},
{
name = "ATLANTIS_GH_APP_ID"
value = var.atlantis_github_app_id
},
{
name = "ATLANTIS_WRITE_GIT_CREDS"
value = var.atlantis_write_git_creds
}
]
# ECS task definition
latest_task_definition_rev = var.external_task_definition_updates ? max(aws_ecs_task_definition.atlantis.revision, data.aws_ecs_task_definition.atlantis[0].revision) : aws_ecs_task_definition.atlantis.revision
# Secret access tokens
container_definition_secrets_1 = local.secret_name_key != "" && local.secret_name_value_from != "" ? [
{
name = local.secret_name_key
valueFrom = local.secret_name_value_from
},
] : []
# Webhook secrets are not supported by BitBucket
container_definition_secrets_2 = local.secret_webhook_key != "" ? [
{
name = local.secret_webhook_key
valueFrom = var.webhook_ssm_parameter_name
},
] : []
tags = merge(
{
"Name" = var.name
},
var.tags,
)
policies_arn = var.policies_arn != null ? var.policies_arn : ["arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"]
# Chunk these into groups of 5, the limit for IPs in an AWS lb listener
whitelist_unauthenticated_cidr_block_chunks = chunklist(
sort(compact(concat(var.allow_github_webhooks ? var.github_webhooks_cidr_blocks : [], var.whitelist_unauthenticated_cidr_blocks))),
5
)
# break up user to uid and gid -- set both to 0 if null
uid = var.user == null ? 0 : split(":", var.user)[0]
gid = var.user == null ? 0 : split(":", var.user)[1]
# default mount points for efs if ephemeral storage is not enabled and mount points aren't specified
mount_points = var.enable_ephemeral_storage || length(var.mount_points) > 0 ? var.mount_points : [{
containerPath = "/home/atlantis"
sourceVolume = "efs-storage"
readOnly = "false"
}]
}
data "aws_partition" "current" {}
data "aws_region" "current" {}
data "aws_route53_zone" "this" {
count = var.create_route53_record || var.create_route53_aaaa_record ? 1 : 0
name = var.route53_zone_name
private_zone = var.route53_private_zone
}
################################################################################
# Secret for webhook
################################################################################
resource "random_id" "webhook" {
count = var.atlantis_github_webhook_secret != "" ? 0 : 1
byte_length = "64"
}
resource "aws_ssm_parameter" "webhook" {
count = var.atlantis_bitbucket_user_token != "" ? 0 : 1
name = var.webhook_ssm_parameter_name
type = "SecureString"
value = coalesce(var.atlantis_github_webhook_secret, join("", random_id.webhook[*].hex))
tags = local.tags
}
resource "aws_ssm_parameter" "atlantis_github_user_token" {
count = var.atlantis_github_user_token != "" ? 1 : 0
name = var.atlantis_github_user_token_ssm_parameter_name
type = "SecureString"
value = var.atlantis_github_user_token
tags = local.tags
}
resource "aws_ssm_parameter" "atlantis_gitlab_user_token" {
count = var.atlantis_gitlab_user_token != "" ? 1 : 0
name = var.atlantis_gitlab_user_token_ssm_parameter_name
type = "SecureString"
value = var.atlantis_gitlab_user_token
tags = local.tags
}
resource "aws_ssm_parameter" "atlantis_bitbucket_user_token" {
count = var.atlantis_bitbucket_user_token != "" ? 1 : 0
name = var.atlantis_bitbucket_user_token_ssm_parameter_name
type = "SecureString"
value = var.atlantis_bitbucket_user_token
tags = local.tags
}
resource "aws_ssm_parameter" "atlantis_github_app_key" {
count = var.atlantis_github_app_key != "" ? 1 : 0
name = var.atlantis_github_app_key_ssm_parameter_name
type = "SecureString"
value = var.atlantis_github_app_key
tags = local.tags
}
################################################################################
# VPC
################################################################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "v3.6.0"
create_vpc = var.vpc_id == ""
name = var.name
cidr = var.cidr
azs = var.azs
private_subnets = var.private_subnets
public_subnets = var.public_subnets
enable_nat_gateway = var.enable_nat_gateway
single_nat_gateway = var.single_nat_gateway
enable_dns_hostnames = !var.enable_ephemeral_storage
manage_default_security_group = var.manage_default_security_group
default_security_group_ingress = var.default_security_group_ingress
default_security_group_egress = var.default_security_group_egress
tags = local.tags
}
################################################################################
# ALB
################################################################################
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "v6.5.0"
name = var.name
internal = var.internal
enable_cross_zone_load_balancing = var.alb_enable_cross_zone_load_balancing
vpc_id = local.vpc_id
subnets = local.public_subnet_ids
security_groups = flatten([module.alb_https_sg.security_group_id, module.alb_http_sg.security_group_id, var.security_group_ids])
access_logs = {
enabled = var.alb_logging_enabled
bucket = var.alb_log_bucket_name
prefix = var.alb_log_location_prefix
}
ip_address_type = var.alb_ip_address_type
enable_deletion_protection = var.alb_enable_deletion_protection
drop_invalid_header_fields = var.alb_drop_invalid_header_fields
listener_ssl_policy_default = var.alb_listener_ssl_policy_default
https_listeners = [
{
target_group_index = 0
port = 443
protocol = "HTTPS"
certificate_arn = var.certificate_arn == "" ? module.acm.acm_certificate_arn : var.certificate_arn
action_type = local.alb_authentication_method
authenticate_oidc = var.alb_authenticate_oidc
authenticate_cognito = var.alb_authenticate_cognito
},
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
action_type = "redirect"
redirect = {
port = 443
protocol = "HTTPS"
status_code = "HTTP_301"
}
},
]
target_groups = [
{
name = var.name
backend_protocol = "HTTP"
backend_port = var.atlantis_port
target_type = "ip"
deregistration_delay = 10
health_check = {
path = "/healthz"
}
},
]
tags = local.tags
}
# Forward action for certain CIDR blocks to bypass authentication (eg. GitHub webhooks)
resource "aws_lb_listener_rule" "unauthenticated_access_for_cidr_blocks" {
count = var.allow_unauthenticated_access ? length(local.whitelist_unauthenticated_cidr_block_chunks) : 0
listener_arn = module.alb.https_listener_arns[0]
priority = var.allow_unauthenticated_access_priority + count.index
action {
type = "forward"
target_group_arn = module.alb.target_group_arns[0]
}
condition {
source_ip {
values = local.whitelist_unauthenticated_cidr_block_chunks[count.index]
}
}
}
# Forward action for certain URL paths to bypass authentication (eg. GitHub webhooks)
resource "aws_lb_listener_rule" "unauthenticated_access_for_webhook" {
count = var.allow_unauthenticated_access && var.allow_github_webhooks ? 1 : 0
listener_arn = module.alb.https_listener_arns[0]
priority = var.allow_unauthenticated_webhook_access_priority
action {
type = "forward"
target_group_arn = module.alb.target_group_arns[0]
}
condition {
path_pattern {
values = ["/events"]
}
}
}
################################################################################
# Security groups
################################################################################
module "alb_https_sg" {
source = "terraform-aws-modules/security-group/aws//modules/https-443"
version = "v4.3.0"
name = "${var.name}-alb-https"
vpc_id = local.vpc_id
description = "Security group with HTTPS ports open for specific IPv4 CIDR block (or everybody), egress ports are all world open"
ingress_cidr_blocks = sort(compact(concat(var.allow_github_webhooks ? var.github_webhooks_cidr_blocks : [], var.alb_ingress_cidr_blocks)))
ingress_ipv6_cidr_blocks = sort(compact(concat(var.allow_github_webhooks ? var.github_webhooks_ipv6_cidr_blocks : [], var.alb_ingress_ipv6_cidr_blocks)))
tags = merge(local.tags, var.alb_https_security_group_tags)
}
module "alb_http_sg" {
source = "terraform-aws-modules/security-group/aws//modules/http-80"
version = "v4.3.0"
name = "${var.name}-alb-http"
vpc_id = local.vpc_id
description = "Security group with HTTP ports open for specific IPv4 CIDR block (or everybody), egress ports are all world open"
ingress_cidr_blocks = sort(compact(concat(var.allow_github_webhooks ? var.github_webhooks_cidr_blocks : [], var.alb_ingress_cidr_blocks)))
ingress_ipv6_cidr_blocks = sort(compact(concat(var.allow_github_webhooks ? var.github_webhooks_ipv6_cidr_blocks : [], var.alb_ingress_ipv6_cidr_blocks)))
tags = merge(local.tags, var.alb_http_security_group_tags)
}
module "atlantis_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "v4.3.0"
name = var.name
vpc_id = local.vpc_id
description = "Security group with open port for Atlantis (${var.atlantis_port}) from ALB, egress ports are all world open"
ingress_with_source_security_group_id = [
{
from_port = var.atlantis_port
to_port = var.atlantis_port
protocol = "tcp"
description = "Atlantis"
source_security_group_id = module.alb_https_sg.security_group_id
},
]
egress_rules = ["all-all"]
tags = merge(local.tags, var.atlantis_security_group_tags)
}
module "efs_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "v4.8.0"
count = var.enable_ephemeral_storage ? 0 : 1
name = "${var.name}-efs"
vpc_id = local.vpc_id
description = "Security group allowing access to the EFS storage"
ingress_with_source_security_group_id = [{
rule = "nfs-tcp",
source_security_group_id = module.atlantis_sg.security_group_id
}]
tags = local.tags
}
################################################################################
# ACM (SSL certificate)
################################################################################
module "acm" {
source = "terraform-aws-modules/acm/aws"
version = "v3.2.0"
create_certificate = var.certificate_arn == ""
domain_name = var.acm_certificate_domain_name == "" ? join(".", [var.name, var.route53_zone_name]) : var.acm_certificate_domain_name
zone_id = var.certificate_arn == "" ? element(concat(data.aws_route53_zone.this[*].id, [""]), 0) : ""
tags = local.tags
}
################################################################################
# Route53 records
################################################################################
resource "aws_route53_record" "atlantis" {
count = var.create_route53_record ? 1 : 0
zone_id = data.aws_route53_zone.this[0].zone_id
name = var.route53_record_name != null ? var.route53_record_name : var.name
type = "A"
alias {
name = module.alb.lb_dns_name
zone_id = module.alb.lb_zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "atlantis_aaaa" {
count = var.create_route53_aaaa_record ? 1 : 0
zone_id = data.aws_route53_zone.this[0].zone_id
name = var.route53_record_name != null ? var.route53_record_name : var.name
type = "AAAA"
alias {
name = module.alb.lb_dns_name
zone_id = module.alb.lb_zone_id
evaluate_target_health = true
}
}
################################################################################
# EFS
################################################################################
resource "aws_efs_file_system" "this" {
count = var.enable_ephemeral_storage ? 0 : 1
creation_token = coalesce(var.efs_file_system_token, var.name)
throughput_mode = var.efs_throughput_mode
provisioned_throughput_in_mibps = var.efs_provisioned_throughput_in_mibps
encrypted = var.efs_file_system_encrypted
}
resource "aws_efs_mount_target" "this" {
# we coalescelist in order to specify the resource keys when we create the subnets using the VPC or they're specified for us. This works around the for_each value depends on attributes which can't be determined until apply error
for_each = {
for k, v in zipmap(coalescelist(var.private_subnets, var.private_subnet_ids, [""]), local.private_subnet_ids) : k => v
if var.enable_ephemeral_storage == false
}
file_system_id = aws_efs_file_system.this[0].id
subnet_id = each.value
security_groups = [module.efs_sg[0].security_group_id, module.atlantis_sg.security_group_id]
}
resource "aws_efs_access_point" "this" {
count = var.enable_ephemeral_storage ? 0 : 1
file_system_id = aws_efs_file_system.this[0].id
posix_user {
gid = local.gid
uid = local.uid
}
root_directory {
path = "/home/atlantis"
creation_info {
owner_gid = local.gid
owner_uid = local.uid
permissions = 0750
}
}
}
################################################################################
# ECS
################################################################################
module "ecs" {
source = "terraform-aws-modules/ecs/aws"
version = "v3.3.0"
create_ecs = var.create_ecs_cluster
name = var.name
container_insights = var.ecs_container_insights
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
default_capacity_provider_strategy = [
{
capacity_provider = var.ecs_fargate_spot ? "FARGATE_SPOT" : "FARGATE"
}
]
tags = local.tags
}
data "aws_iam_policy_document" "ecs_tasks" {
statement {
actions = [
"sts:AssumeRole",
]
effect = "Allow"
principals {
type = "Service"
identifiers = compact(distinct(concat(["ecs-tasks.amazonaws.com"], var.trusted_principals)))
}
dynamic "principals" {
for_each = length(var.trusted_entities) > 0 ? [true] : []
content {
type = "AWS"
identifiers = var.trusted_entities
}
}
}
}
resource "aws_iam_role" "ecs_task_execution" {
name = "${var.name}-ecs_task_execution"
assume_role_policy = data.aws_iam_policy_document.ecs_tasks.json
max_session_duration = var.max_session_duration
permissions_boundary = var.permissions_boundary
path = var.path
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution" {
for_each = toset(local.policies_arn)
role = aws_iam_role.ecs_task_execution.id
policy_arn = each.value
}
# ref: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data.html
data "aws_iam_policy_document" "ecs_task_access_secrets" {
statement {
effect = "Allow"
resources = flatten([
aws_ssm_parameter.webhook[*].arn,
aws_ssm_parameter.atlantis_github_user_token[*].arn,
aws_ssm_parameter.atlantis_gitlab_user_token[*].arn,
aws_ssm_parameter.atlantis_bitbucket_user_token[*].arn,
aws_ssm_parameter.atlantis_github_app_key[*].arn,
try(var.repository_credentials["credentialsParameter"], [])
])
actions = [
"ssm:GetParameters",
"secretsmanager:GetSecretValue",
]
}
}
data "aws_iam_policy_document" "ecs_task_access_secrets_with_kms" {
count = var.ssm_kms_key_arn == "" ? 0 : 1
source_json = data.aws_iam_policy_document.ecs_task_access_secrets.json
statement {
sid = "AllowKMSDecrypt"
effect = "Allow"
actions = ["kms:Decrypt"]
resources = [var.ssm_kms_key_arn]
}
}
resource "aws_iam_role_policy" "ecs_task_access_secrets" {
count = local.has_secrets ? 1 : 0
name = "ECSTaskAccessSecretsPolicy"
role = aws_iam_role.ecs_task_execution.id
policy = element(
compact(
concat(
data.aws_iam_policy_document.ecs_task_access_secrets_with_kms[*].json,
data.aws_iam_policy_document.ecs_task_access_secrets[*].json,
),
),
0,
)
}
module "container_definition_github_gitlab" {
source = "cloudposse/ecs-container-definition/aws"
version = "v0.58.1"
container_name = var.name
container_image = local.atlantis_image
container_cpu = var.container_cpu != null ? var.container_cpu : var.ecs_task_cpu
container_memory = var.container_memory != null ? var.container_memory : var.ecs_task_memory
container_memory_reservation = var.container_memory_reservation
user = var.user
ulimits = var.ulimits
entrypoint = var.entrypoint
command = var.command
working_directory = var.working_directory
repository_credentials = var.repository_credentials
docker_labels = var.docker_labels
start_timeout = var.start_timeout
stop_timeout = var.stop_timeout
container_depends_on = var.container_depends_on
essential = var.essential
readonly_root_filesystem = var.readonly_root_filesystem
mount_points = local.mount_points
volumes_from = var.volumes_from
port_mappings = [
{
containerPort = var.atlantis_port
hostPort = var.atlantis_port
protocol = "tcp"
},
]
log_configuration = {
logDriver = "awslogs"
options = {
awslogs-region = data.aws_region.current.name
awslogs-group = aws_cloudwatch_log_group.atlantis.name
awslogs-stream-prefix = "ecs"
}
secretOptions = []
}
firelens_configuration = var.firelens_configuration
environment = concat(
local.container_definition_environment,
var.custom_environment_variables,
)
secrets = concat(
local.container_definition_secrets_1,
local.container_definition_secrets_2,
var.custom_environment_secrets,
)
}
module "container_definition_bitbucket" {
source = "cloudposse/ecs-container-definition/aws"
version = "v0.58.1"
container_name = var.name
container_image = local.atlantis_image
container_cpu = var.container_cpu != null ? var.container_cpu : var.ecs_task_cpu
container_memory = var.container_memory != null ? var.container_memory : var.ecs_task_memory
container_memory_reservation = var.container_memory_reservation
user = var.user
ulimits = var.ulimits
entrypoint = var.entrypoint
command = var.command
working_directory = var.working_directory
repository_credentials = var.repository_credentials
docker_labels = var.docker_labels
start_timeout = var.start_timeout
stop_timeout = var.stop_timeout
container_depends_on = var.container_depends_on
essential = var.essential
readonly_root_filesystem = var.readonly_root_filesystem
mount_points = var.mount_points
volumes_from = var.volumes_from
port_mappings = [
{
containerPort = var.atlantis_port
hostPort = var.atlantis_port
protocol = "tcp"
},
]
log_configuration = {
logDriver = "awslogs"
options = {
awslogs-region = data.aws_region.current.name
awslogs-group = aws_cloudwatch_log_group.atlantis.name
awslogs-stream-prefix = "ecs"
}
secretOptions = []
}
firelens_configuration = var.firelens_configuration
environment = concat(
local.container_definition_environment,
var.custom_environment_variables,
)
secrets = concat(
local.container_definition_secrets_1,
var.custom_environment_secrets,
)
}
resource "aws_ecs_task_definition" "atlantis" {
family = var.name
execution_role_arn = aws_iam_role.ecs_task_execution.arn
task_role_arn = aws_iam_role.ecs_task_execution.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.ecs_task_cpu
memory = var.ecs_task_memory
container_definitions = local.container_definitions
dynamic "runtime_platform" {
for_each = var.runtime_platform != null ? [var.runtime_platform] : []
content {
operating_system_family = try(runtime_platform.value.operating_system_family, null)
cpu_architecture = try(runtime_platform.value.cpu_architecture, null)
}
}
dynamic "ephemeral_storage" {
for_each = var.enable_ephemeral_storage ? [1] : []
content {
size_in_gib = var.ephemeral_storage_size
}
}
dynamic "volume" {
for_each = var.enable_ephemeral_storage ? [] : [1]
content {
name = "efs-storage"
efs_volume_configuration {
file_system_id = aws_efs_file_system.this[0].id
transit_encryption = "ENABLED"
transit_encryption_port = 2999
authorization_config {
access_point_id = aws_efs_access_point.this[0].id
iam = "ENABLED"
}
}
}
}
tags = local.tags
}
data "aws_ecs_task_definition" "atlantis" {
count = var.external_task_definition_updates ? 1 : 0
task_definition = var.name
depends_on = [aws_ecs_task_definition.atlantis]
}
resource "aws_ecs_service" "atlantis" {
name = var.name
cluster = local.ecs_cluster_id
task_definition = "${var.name}:${local.latest_task_definition_rev}"
desired_count = var.ecs_service_desired_count
launch_type = var.ecs_fargate_spot ? null : "FARGATE"
platform_version = var.ecs_service_platform_version
deployment_maximum_percent = var.ecs_service_deployment_maximum_percent
deployment_minimum_healthy_percent = var.ecs_service_deployment_minimum_healthy_percent
force_new_deployment = var.ecs_service_force_new_deployment
enable_execute_command = var.ecs_service_enable_execute_command
network_configuration {
subnets = local.private_subnet_ids
security_groups = [module.atlantis_sg.security_group_id]
assign_public_ip = var.ecs_service_assign_public_ip
}
load_balancer {
container_name = var.name
container_port = var.atlantis_port
target_group_arn = element(module.alb.target_group_arns, 0)
}
dynamic "load_balancer" {
for_each = var.extra_load_balancers
content {
container_name = load_balancer.value["container_name"]
container_port = load_balancer.value["container_port"]
target_group_arn = load_balancer.value["target_group_arn"]
}
}
dynamic "capacity_provider_strategy" {
for_each = var.ecs_fargate_spot ? [true] : []
content {
capacity_provider = "FARGATE_SPOT"
weight = 100
}
}
enable_ecs_managed_tags = var.enable_ecs_managed_tags
propagate_tags = var.propagate_tags
tags = var.use_ecs_old_arn_format ? null : local.tags
}
################################################################################
# Cloudwatch logs
################################################################################
resource "aws_cloudwatch_log_group" "atlantis" {
name = var.name
retention_in_days = var.cloudwatch_log_retention_in_days
kms_key_id = var.cloudwatch_logs_kms_key_id
tags = local.tags
}