Skip to content

Commit 8822156

Browse files
authored
ISSUE-53 - Add variable to use a custom LB instead of creating one (#55)
1 parent bd91c47 commit 8822156

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

.terraform.lock.hcl

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ In order to run all checks at any point run the following command:
6666

6767
| Name | Source | Version |
6868
|------|--------|---------|
69-
| <a name="module_ecs-alb"></a> [ecs-alb](#module\_ecs-alb) | cn-terraform/ecs-alb/aws | 1.0.23 |
69+
| <a name="module_ecs-alb"></a> [ecs-alb](#module\_ecs-alb) | cn-terraform/ecs-alb/aws | 1.0.24 |
7070
| <a name="module_ecs-autoscaling"></a> [ecs-autoscaling](#module\_ecs-autoscaling) | cn-terraform/ecs-service-autoscaling/aws | 1.0.6 |
7171

7272
## Resources
@@ -86,6 +86,7 @@ In order to run all checks at any point run the following command:
8686
| <a name="input_assign_public_ip"></a> [assign\_public\_ip](#input\_assign\_public\_ip) | (Optional) Assign a public IP address to the ENI (Fargate launch type only). If true service will be associated with public subnets. Default false. | `bool` | `false` | no |
8787
| <a name="input_block_s3_bucket_public_access"></a> [block\_s3\_bucket\_public\_access](#input\_block\_s3\_bucket\_public\_access) | (Optional) If true, public access to the S3 bucket will be blocked. | `bool` | `true` | no |
8888
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | Name of the running container | `any` | n/a | yes |
89+
| <a name="input_custom_lb_arn"></a> [custom\_lb\_arn](#input\_custom\_lb\_arn) | ARN of the Load Balancer to use in the ECS service. If provided, this module will not create a load balancer and will use the one provided in this variable | `string` | `null` | no |
8990
| <a name="input_default_certificate_arn"></a> [default\_certificate\_arn](#input\_default\_certificate\_arn) | (Optional) The ARN of the default SSL server certificate. Required if var.https\_ports is set. | `string` | `null` | no |
9091
| <a name="input_deployment_controller"></a> [deployment\_controller](#input\_deployment\_controller) | (Optional) Deployment controller | `list(string)` | `[]` | no |
9192
| <a name="input_deployment_maximum_percent"></a> [deployment\_maximum\_percent](#input\_deployment\_maximum\_percent) | (Optional) The upper limit (as a percentage of the service's desiredCount) of the number of running tasks that can be running in a service during a deployment. | `number` | `200` | no |

main.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# AWS LOAD BALANCER
33
#------------------------------------------------------------------------------
44
module "ecs-alb" {
5+
count = var.custom_lb_arn == null ? 1 : 0
6+
57
source = "cn-terraform/ecs-alb/aws"
68
version = "1.0.24"
79

@@ -74,15 +76,15 @@ resource "aws_ecs_service" "service" {
7476
force_new_deployment = var.force_new_deployment
7577

7678
dynamic "load_balancer" {
77-
for_each = module.ecs-alb.lb_http_tgs_map_arn_port
79+
for_each = module.ecs-alb[0].lb_http_tgs_map_arn_port
7880
content {
7981
target_group_arn = load_balancer.key
8082
container_name = var.container_name
8183
container_port = load_balancer.value
8284
}
8385
}
8486
dynamic "load_balancer" {
85-
for_each = module.ecs-alb.lb_https_tgs_map_arn_port
87+
for_each = module.ecs-alb[0].lb_https_tgs_map_arn_port
8688
content {
8789
target_group_arn = load_balancer.key
8890
container_name = var.container_name
@@ -161,13 +163,13 @@ resource "aws_security_group_rule" "egress" {
161163
}
162164

163165
resource "aws_security_group_rule" "ingress_through_http_and_https" {
164-
for_each = toset(concat(module.ecs-alb.lb_https_tgs_ports, module.ecs-alb.lb_http_tgs_ports))
166+
for_each = toset(concat(module.ecs-alb[0].lb_https_tgs_ports, module.ecs-alb[0].lb_http_tgs_ports))
165167
security_group_id = aws_security_group.ecs_tasks_sg.id
166168
type = "ingress"
167169
from_port = each.key
168170
to_port = each.key
169171
protocol = "tcp"
170-
source_security_group_id = module.ecs-alb.aws_security_group_lb_access_sg_id
172+
source_security_group_id = module.ecs-alb[0].aws_security_group_lb_access_sg_id
171173
}
172174

173175
module "ecs-autoscaling" {

outputs.tf

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,124 +49,124 @@ output "ecs_tasks_sg_description" {
4949
#------------------------------------------------------------------------------
5050
output "aws_lb_lb_id" {
5151
description = "The ARN of the load balancer (matches arn)."
52-
value = module.ecs-alb.aws_lb_lb_id
52+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_lb_lb_id : null
5353
}
5454

5555
output "aws_lb_lb_arn" {
5656
description = "The ARN of the load balancer (matches id)."
57-
value = module.ecs-alb.aws_lb_lb_arn
57+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_lb_lb_arn : null
5858
}
5959

6060
output "aws_lb_lb_arn_suffix" {
6161
description = "The ARN suffix for use with CloudWatch Metrics."
62-
value = module.ecs-alb.aws_lb_lb_arn_suffix
62+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_lb_lb_arn_suffix : null
6363
}
6464

6565
output "aws_lb_lb_dns_name" {
6666
description = "The DNS name of the load balancer."
67-
value = module.ecs-alb.aws_lb_lb_dns_name
67+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_lb_lb_dns_name : null
6868
}
6969

7070
output "aws_lb_lb_zone_id" {
7171
description = "The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record)."
72-
value = module.ecs-alb.aws_lb_lb_zone_id
72+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_lb_lb_zone_id : null
7373
}
7474

7575
#------------------------------------------------------------------------------
7676
# ACCESS CONTROL TO APPLICATION LOAD BALANCER
7777
#------------------------------------------------------------------------------
7878
output "aws_security_group_lb_access_sg_id" {
7979
description = "The ID of the security group"
80-
value = module.ecs-alb.aws_security_group_lb_access_sg_id
80+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_id : null
8181
}
8282

8383
output "aws_security_group_lb_access_sg_arn" {
8484
description = "The ARN of the security group"
85-
value = module.ecs-alb.aws_security_group_lb_access_sg_arn
85+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_arn : null
8686
}
8787

8888
output "aws_security_group_lb_access_sg_vpc_id" {
8989
description = "The VPC ID."
90-
value = module.ecs-alb.aws_security_group_lb_access_sg_vpc_id
90+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_vpc_id : null
9191
}
9292

9393
output "aws_security_group_lb_access_sg_owner_id" {
9494
description = "The owner ID."
95-
value = module.ecs-alb.aws_security_group_lb_access_sg_owner_id
95+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_owner_id : null
9696
}
9797

9898
output "aws_security_group_lb_access_sg_name" {
9999
description = "The name of the security group"
100-
value = module.ecs-alb.aws_security_group_lb_access_sg_name
100+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_name : null
101101
}
102102

103103
output "aws_security_group_lb_access_sg_description" {
104104
description = "The description of the security group"
105-
value = module.ecs-alb.aws_security_group_lb_access_sg_description
105+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_description : null
106106
}
107107

108108
output "aws_security_group_lb_access_sg_ingress" {
109109
description = "The ingress rules."
110-
value = module.ecs-alb.aws_security_group_lb_access_sg_ingress
110+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_ingress : null
111111
}
112112

113113
output "aws_security_group_lb_access_sg_egress" {
114114
description = "The egress rules."
115-
value = module.ecs-alb.aws_security_group_lb_access_sg_egress
115+
value = var.custom_lb_arn == null ? module.ecs-alb[0].aws_security_group_lb_access_sg_egress : null
116116
}
117117

118118
#------------------------------------------------------------------------------
119119
# AWS LOAD BALANCER - Target Groups
120120
#------------------------------------------------------------------------------
121121
output "lb_http_tgs_ids" {
122122
description = "List of HTTP Target Groups IDs"
123-
value = module.ecs-alb.lb_http_tgs_ids
123+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_http_tgs_ids : null
124124
}
125125

126126
output "lb_http_tgs_arns" {
127127
description = "List of HTTP Target Groups ARNs"
128-
value = module.ecs-alb.lb_http_tgs_arns
128+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_http_tgs_arns : null
129129
}
130130

131131
output "lb_http_tgs_names" {
132132
description = "List of HTTP Target Groups Names"
133-
value = module.ecs-alb.lb_http_tgs_names
133+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_http_tgs_names : null
134134
}
135135

136136
output "lb_https_tgs_ids" {
137137
description = "List of HTTPS Target Groups IDs"
138-
value = module.ecs-alb.lb_https_tgs_ids
138+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_https_tgs_ids : null
139139
}
140140

141141
output "lb_https_tgs_arns" {
142142
description = "List of HTTPS Target Groups ARNs"
143-
value = module.ecs-alb.lb_https_tgs_arns
143+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_https_tgs_arns : null
144144
}
145145

146146
output "lb_https_tgs_names" {
147147
description = "List of HTTPS Target Groups Names"
148-
value = module.ecs-alb.lb_https_tgs_names
148+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_https_tgs_names : null
149149
}
150150

151151
#------------------------------------------------------------------------------
152152
# AWS LOAD BALANCER - Listeners
153153
#------------------------------------------------------------------------------
154154
output "lb_http_listeners_ids" {
155155
description = "List of HTTP Listeners IDs"
156-
value = module.ecs-alb.lb_http_listeners_ids
156+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_http_listeners_ids : null
157157
}
158158

159159
output "lb_http_listeners_arns" {
160160
description = "List of HTTP Listeners ARNs"
161-
value = module.ecs-alb.lb_http_listeners_arns
161+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_http_listeners_arns : null
162162
}
163163

164164
output "lb_https_listeners_ids" {
165165
description = "List of HTTPS Listeners IDs"
166-
value = module.ecs-alb.lb_https_listeners_ids
166+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_https_listeners_ids : null
167167
}
168168

169169
output "lb_https_listeners_arns" {
170170
description = "List of HTTPS Listeners ARNs"
171-
value = module.ecs-alb.lb_https_listeners_arns
171+
value = var.custom_lb_arn == null ? module.ecs-alb[0].lb_https_listeners_arns : null
172172
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ variable "scale_target_min_capacity" {
206206
#------------------------------------------------------------------------------
207207
# AWS LOAD BALANCER
208208
#------------------------------------------------------------------------------
209+
variable "custom_lb_arn" {
210+
description = "ARN of the Load Balancer to use in the ECS service. If provided, this module will not create a load balancer and will use the one provided in this variable"
211+
type = string
212+
default = null
213+
}
214+
209215
variable "lb_internal" {
210216
description = "(Optional) If true, the LB will be internal."
211217
type = bool

0 commit comments

Comments
 (0)