Skip to content

Commit eaef6e3

Browse files
authored
Making codedeploy and test listener optional (#60)
* Making codedeploy and test listener optional; small fixes * terraform-docs: automated update action --------- Co-authored-by: adenot <[email protected]>
1 parent de8c916 commit eaef6e3

File tree

9 files changed

+55
-39
lines changed

9 files changed

+55
-39
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ module "ecs_apps" {
7777
| alb\_internal | Deploys a second internal ALB for private APIs. | `bool` | `false` | no |
7878
| alb\_internal\_ssl\_policy | The name of the SSL Policy for the listener. Required if protocol is HTTPS or TLS. | `string` | `"ELBSecurityPolicy-TLS-1-2-Ext-2018-06"` | no |
7979
| alb\_only | Whether to deploy only an alb and no cloudFront or not with the cluster. | `bool` | `false` | no |
80+
| alb\_sg\_allow\_alb\_test\_listener | Whether to allow world access to the test listeners | `bool` | `true` | no |
8081
| alb\_sg\_allow\_egress\_https\_world | Whether to allow ALB to access HTTPS endpoints - needed when using OIDC authentication | `bool` | `true` | no |
81-
| alb\_sg\_allow\_test\_listener | Whether to allow world access to the test listeners | `bool` | `true` | no |
8282
| alb\_ssl\_policy | The name of the SSL Policy for the listener. Required if protocol is HTTPS or TLS. | `string` | `"ELBSecurityPolicy-2016-08"` | no |
83+
| alb\_test\_listener | Enables a second listener on ports 8080 and 8443 for a phased deploy/cutover (blue/green) | `bool` | `true` | no |
8384
| architecture | Architecture to select the AMI, x86\_64 or arm64 | `string` | `"x86_64"` | no |
8485
| asg\_capacity\_rebalance | Indicates whether capacity rebalance is enabled | `bool` | `false` | no |
8586
| asg\_max | Max number of instances for autoscaling group. | `number` | `4` | no |
@@ -91,6 +92,7 @@ module "ecs_apps" {
9192
| backup | Assing a backup tag to efs resource - Backup will be performed by AWS Backup. | `string` | `"true"` | no |
9293
| certificate\_arn | n/a | `any` | n/a | yes |
9394
| certificate\_internal\_arn | certificate arn for internal ALB. | `string` | `""` | no |
95+
| code\_deploy | Enables CodeDeploy role to be used for deployment | `bool` | `true` | no |
9496
| container\_insights | Enables CloudWatch Container Insights for a cluster. | `bool` | `false` | no |
9597
| create\_efs | Enables creation of EFS volume for cluster | `bool` | `true` | no |
9698
| create\_iam\_service\_linked\_role | Create iam\_service\_linked\_role for ECS or not. | `bool` | `false` | no |

_outputs.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ output "ecs_service_iam_role_arn" {
4343
}
4444

4545
output "ecs_codedeploy_iam_role_arn" {
46-
value = aws_iam_role.codedeploy_service.arn
46+
value = try(aws_iam_role.codedeploy_service[0].arn, "")
4747
}
4848

4949
output "ecs_service_iam_role_name" {
@@ -71,19 +71,19 @@ output "ecs_name" {
7171
}
7272

7373
output "alb_listener_https_arn" {
74-
value = aws_lb_listener.ecs_https.*.arn
74+
value = try(aws_lb_listener.ecs_https[0].arn, "")
7575
}
7676

7777
output "alb_listener_test_traffic_arn" {
78-
value = aws_lb_listener.ecs_test_https.*.arn
78+
value = try(aws_lb_listener.ecs_test_https[0].arn, "")
7979
}
8080

8181
output "alb_internal_listener_https_arn" {
82-
value = aws_lb_listener.ecs_https_internal.*.arn
82+
value = try(aws_lb_listener.ecs_https_internal[0].arn, "")
8383
}
8484

8585
output "alb_internal_listener_test_traffic_arn" {
86-
value = aws_lb_listener.ecs_test_https_internal.*.arn
86+
value = try(aws_lb_listener.ecs_test_https_internal.*.arn, "")
8787
}
8888

8989
output "ecs_nodes_secgrp_id" {
@@ -100,4 +100,4 @@ output "efs_fs_id" {
100100

101101
output "private_key_pem" {
102102
value = try(tls_private_key.algorithm[0].private_key_pem, "")
103-
}
103+
}

_variables.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ variable "alb_http_listener" {
9090
description = "Whether to enable HTTP listeners"
9191
}
9292

93-
variable "alb_sg_allow_test_listener" {
93+
variable "alb_sg_allow_alb_test_listener" {
9494
default = true
9595
description = "Whether to allow world access to the test listeners"
9696
}
@@ -371,4 +371,16 @@ variable "container_insights" {
371371
type = bool
372372
default = false
373373
description = "Enables CloudWatch Container Insights for a cluster."
374-
}
374+
}
375+
376+
variable "alb_test_listener" {
377+
type = bool
378+
default = true
379+
description = "Enables a second listener on ports 8080 and 8443 for a phased deploy/cutover (blue/green)"
380+
}
381+
382+
variable "code_deploy" {
383+
type = bool
384+
default = true
385+
description = "Enables CodeDeploy role to be used for deployment"
386+
}

alb-internal.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "aws_lb" "ecs_internal" {
2828
var.tags,
2929
{
3030
"Terraform" = true,
31-
Name = "ecs-${var.name}-internal"
31+
Name = "ecs-${var.name}-internal"
3232
},
3333
)
3434
}
@@ -47,7 +47,7 @@ resource "aws_lb_listener" "ecs_https_internal" {
4747
target_group_arn = aws_lb_target_group.ecs_default_https_internal[0].arn
4848
}
4949

50-
tags = merge(
50+
tags = merge(
5151
var.tags,
5252
{
5353
"Terraform" = true
@@ -56,7 +56,7 @@ resource "aws_lb_listener" "ecs_https_internal" {
5656
}
5757

5858
resource "aws_lb_listener" "ecs_test_https_internal" {
59-
count = var.alb_internal ? 1 : 0
59+
count = var.alb_internal && var.alb_test_listener ? 1 : 0
6060

6161
load_balancer_arn = aws_lb.ecs_internal[0].arn
6262
port = "8443"
@@ -69,7 +69,7 @@ resource "aws_lb_listener" "ecs_test_https_internal" {
6969
#target_group_arn = aws_lb_target_group.ecs_replacement_https[0].arn
7070
target_group_arn = aws_lb_target_group.ecs_default_https_internal[0].arn
7171
}
72-
tags = merge(
72+
tags = merge(
7373
var.tags,
7474
{
7575
"Terraform" = true
@@ -92,7 +92,7 @@ resource "aws_lb_target_group" "ecs_default_https_internal" {
9292
port = 80
9393
protocol = "HTTP"
9494
vpc_id = var.vpc_id
95-
tags = merge(
95+
tags = merge(
9696
var.tags,
9797
{
9898
"Terraform" = true

alb.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "aws_lb" "ecs" {
2828
var.tags,
2929
{
3030
"Terraform" = true,
31-
"Name" = "ecs-${var.name}"
31+
"Name" = "ecs-${var.name}"
3232
},
3333
)
3434
}
@@ -47,7 +47,7 @@ resource "aws_lb_listener" "ecs_https" {
4747
target_group_arn = aws_lb_target_group.ecs_default_https[0].arn
4848
}
4949

50-
tags = merge(
50+
tags = merge(
5151
var.tags,
5252
{
5353
"Terraform" = true
@@ -71,7 +71,7 @@ resource "aws_lb_listener" "ecs_http_redirect" {
7171
status_code = "HTTP_301"
7272
}
7373
}
74-
tags = merge(
74+
tags = merge(
7575
var.tags,
7676
{
7777
"Terraform" = true
@@ -80,7 +80,7 @@ resource "aws_lb_listener" "ecs_http_redirect" {
8080
}
8181

8282
resource "aws_lb_listener" "ecs_test_https" {
83-
count = var.alb ? 1 : 0
83+
count = var.alb && var.alb_test_listener ? 1 : 0
8484

8585
load_balancer_arn = aws_lb.ecs[0].arn
8686
port = "8443"
@@ -93,7 +93,7 @@ resource "aws_lb_listener" "ecs_test_https" {
9393
#target_group_arn = aws_lb_target_group.ecs_replacement_https[0].arn
9494
target_group_arn = aws_lb_target_group.ecs_default_https[0].arn
9595
}
96-
tags = merge(
96+
tags = merge(
9797
var.tags,
9898
{
9999
"Terraform" = true
@@ -102,7 +102,7 @@ resource "aws_lb_listener" "ecs_test_https" {
102102
}
103103

104104
resource "aws_lb_listener" "ecs_test_http_redirect" {
105-
count = var.alb && var.alb_http_listener ? 1 : 0
105+
count = var.alb && var.alb_http_listener && var.alb_test_listener ? 1 : 0
106106

107107
load_balancer_arn = aws_lb.ecs[0].arn
108108
port = "8080"
@@ -117,7 +117,7 @@ resource "aws_lb_listener" "ecs_test_http_redirect" {
117117
status_code = "HTTP_301"
118118
}
119119
}
120-
tags = merge(
120+
tags = merge(
121121
var.tags,
122122
{
123123
"Terraform" = true
@@ -140,7 +140,7 @@ resource "aws_lb_target_group" "ecs_default_http" {
140140
protocol = "HTTP"
141141
vpc_id = var.vpc_id
142142

143-
tags = merge(
143+
tags = merge(
144144
var.tags,
145145
{
146146
"Terraform" = true
@@ -160,7 +160,7 @@ resource "aws_lb_target_group" "ecs_default_https" {
160160
protocol = "HTTP"
161161
vpc_id = var.vpc_id
162162

163-
tags = merge(
163+
tags = merge(
164164
var.tags,
165165
{
166166
"Terraform" = true

ec2-launch-template.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ resource "aws_launch_template" "ecs" {
2323

2424
user_data = base64encode(templatefile("${path.module}/userdata.tpl", {
2525
tf_cluster_name = var.name
26-
tf_efs_id = aws_efs_file_system.ecs[0].id
26+
tf_efs_id = try(aws_efs_file_system.ecs[0].id, "")
2727
userdata_extra = var.userdata
2828
}))
2929

@@ -33,7 +33,7 @@ resource "aws_launch_template" "ecs" {
3333
create_before_destroy = true
3434
}
3535

36-
tags = merge(
36+
tags = merge(
3737
var.tags,
3838
{
3939
"Terraform" = true
@@ -45,18 +45,18 @@ resource "tls_private_key" "algorithm" {
4545
count = var.ec2_key_enabled ? 1 : 0
4646
algorithm = "RSA"
4747
rsa_bits = 4096
48-
48+
4949
}
5050

5151
resource "aws_key_pair" "generated_key" {
5252
count = var.ec2_key_enabled ? 1 : 0
5353
key_name = "${var.name}-key"
5454
public_key = tls_private_key.algorithm[0].public_key_openssh
5555

56-
tags = merge(
56+
tags = merge(
5757
var.tags,
5858
{
5959
"Terraform" = true
6060
},
6161
)
62-
}
62+
}

iam-codedeploy.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
resource "aws_iam_role" "codedeploy_service" {
2-
name = "codedeploy-service-${var.name}-${data.aws_region.current.name}"
2+
count = var.code_deploy ? 1 : 0
3+
name = "codedeploy-service-${var.name}-${data.aws_region.current.name}"
34
tags = merge(
45
var.tags,
56
{
@@ -24,6 +25,7 @@ EOF
2425
}
2526

2627
resource "aws_iam_role_policy_attachment" "codedeploy_service" {
27-
role = aws_iam_role.codedeploy_service.name
28+
count = var.code_deploy ? 1 : 0
29+
role = aws_iam_role.codedeploy_service[0].name
2830
policy_arn = "arn:aws:iam::aws:policy/AWSCodeDeployRoleForECS"
29-
}
31+
}

sg-alb-internal.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ resource "aws_security_group" "alb_internal" {
55
description = "SG for ECS Internal ALB"
66
vpc_id = var.vpc_id
77

8-
tags = merge(
8+
tags = merge(
99
var.tags,
1010
{
1111
terraform = "true"
12-
Name = "ecs-${var.name}-lb"
12+
Name = "ecs-${var.name}-lb"
1313
},
1414
)
1515
}
@@ -26,8 +26,8 @@ resource "aws_security_group_rule" "https_from_world_to_alb_internal" {
2626
cidr_blocks = data.aws_subnet.private_subnets[*].cidr_block
2727
}
2828

29-
resource "aws_security_group_rule" "https_test_listener_from_world_to_alb_internal" {
30-
count = var.alb_internal ? 1 : 0
29+
resource "aws_security_group_rule" "https_alb_test_listener_from_world_to_alb_internal" {
30+
count = var.alb_internal && var.alb_test_listener ? 1 : 0
3131

3232
description = "HTTPS ECS Internal ALB Test Listener"
3333
type = "ingress"

sg-alb.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ resource "aws_security_group" "alb" {
55
description = "SG for ECS ALB"
66
vpc_id = var.vpc_id
77

8-
tags = merge(
8+
tags = merge(
99
var.tags,
1010
{
1111
terraform = "true"
12-
Name = "ecs-${var.name}-lb"
12+
Name = "ecs-${var.name}-lb"
1313
},
1414
)
1515
}
@@ -38,8 +38,8 @@ resource "aws_security_group_rule" "https_from_world_to_alb" {
3838
cidr_blocks = ["0.0.0.0/0"]
3939
}
4040

41-
resource "aws_security_group_rule" "https_test_listener_from_world_to_alb" {
42-
count = var.alb && var.alb_sg_allow_test_listener ? 1 : 0
41+
resource "aws_security_group_rule" "https_alb_test_listener_from_world_to_alb" {
42+
count = var.alb && var.alb_sg_allow_alb_test_listener && var.alb_test_listener ? 1 : 0
4343

4444
description = "HTTPS ECS ALB Test Listener"
4545
type = "ingress"

0 commit comments

Comments
 (0)