Skip to content

Commit f29f8f0

Browse files
committed
fix(alb): cannot use null in length due to optional block variables
Output load balancer's dns name and zone id.
1 parent e2e5f50 commit f29f8f0

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

modules/alb/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ No modules.
4343
| Name | Description |
4444
|------|-------------|
4545
| <a name="output_arn"></a> [arn](#output\_arn) | ARN of the Load Balancer |
46+
| <a name="output_dns_name"></a> [dns\_name](#output\_dns\_name) | DNS name of the Load Balancer |
4647
| <a name="output_id"></a> [id](#output\_id) | Identifier of the Load Balancer |
4748
| <a name="output_listeners_arns"></a> [listeners\_arns](#output\_listeners\_arns) | ARNs of the Listeners |
4849
| <a name="output_listeners_ids"></a> [listeners\_ids](#output\_listeners\_ids) | Identifiers of the Listeners |
4950
| <a name="output_target_groups_arns"></a> [target\_groups\_arns](#output\_target\_groups\_arns) | ARNs of the Target Groups |
5051
| <a name="output_target_groups_ids"></a> [target\_groups\_ids](#output\_target\_groups\_ids) | Identifiers of the Target Groups |
52+
| <a name="output_zone_id"></a> [zone\_id](#output\_zone\_id) | Canonical hosted zone ID of the Load Balancer (to be used in a Route 53 Alias record) |
5153
<!-- END_TF_DOCS -->

modules/alb/main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "aws_lb_target_group" "this" {
3131
target_type = each.value.target_type
3232

3333
dynamic "health_check" {
34-
for_each = length(try(each.value.health_check, {})) > 0 ? [1] : []
34+
for_each = (try(each.value.health_check, null) != null && length(try(each.value.health_check, {})) > 0) ? [1] : []
3535

3636
content {
3737
enabled = try(each.value.health_check.enabled, null)
@@ -65,7 +65,7 @@ resource "aws_lb_listener" "this" {
6565
ssl_policy = element(var.listeners, count.index).ssl_policy
6666

6767
dynamic "mutual_authentication" {
68-
for_each = length(try(element(var.listeners, count.index).mutual_authentication, {})) > 0 ? [1] : []
68+
for_each = (try(element(var.listeners, count.index).mutual_authentication, null) != null && length(try(element(var.listeners, count.index).mutual_authentication, {})) > 0) ? [1] : []
6969

7070
content {
7171
mode = element(var.listeners, count.index).mutual_authentication.mode
@@ -84,7 +84,7 @@ resource "aws_lb_listener" "this" {
8484
order = default_action.value.order
8585

8686
dynamic "authenticate_cognito" {
87-
for_each = length(try(default_action.value.authenticate_cognito, {})) > 0 ? [1] : []
87+
for_each = (try(default_action.value.authenticate_cognito, null) != null && length(try(default_action.value.authenticate_cognito, {})) > 0) ? [1] : []
8888

8989
content {
9090
user_pool_arn = default_action.value.authenticate_cognito.user_pool_arn
@@ -99,7 +99,7 @@ resource "aws_lb_listener" "this" {
9999
}
100100

101101
dynamic "authenticate_oidc" {
102-
for_each = length(try(default_action.value.authenticate_oidc, {})) > 0 ? [1] : []
102+
for_each = (try(default_action.value.authenticate_oidc, null) != null && length(try(default_action.value.authenticate_oidc, {})) > 0) ? [1] : []
103103

104104
content {
105105
authorization_endpoint = default_action.value.authenticate_oidc.authorization_endpoint
@@ -117,7 +117,7 @@ resource "aws_lb_listener" "this" {
117117
}
118118

119119
dynamic "fixed_response" {
120-
for_each = length(try(default_action.value.fixed_response, {})) > 0 ? [1] : []
120+
for_each = (try(default_action.value.fixed_response, null) != null && length(try(default_action.value.fixed_response, {})) > 0) ? [1] : []
121121

122122
content {
123123
content_type = default_action.value.fixed_response.content_type
@@ -127,7 +127,7 @@ resource "aws_lb_listener" "this" {
127127
}
128128

129129
dynamic "forward" {
130-
for_each = length(try(default_action.value.forward, {})) > 0 ? [1] : []
130+
for_each = (try(default_action.value.forward, null) != null && length(try(default_action.value.forward, {})) > 0) ? [1] : []
131131

132132
content {
133133
dynamic "target_group" {
@@ -141,7 +141,7 @@ resource "aws_lb_listener" "this" {
141141
}
142142

143143
dynamic "stickiness" {
144-
for_each = length(try(default_action.value.forward.stickiness, {})) > 0 ? [1] : []
144+
for_each = (try(default_action.value.forward.stickiness, null) != null && length(try(default_action.value.forward.stickiness, {})) > 0) ? [1] : []
145145

146146
content {
147147
duration = default_action.value.forward.stickiness.duration
@@ -152,7 +152,7 @@ resource "aws_lb_listener" "this" {
152152
}
153153

154154
dynamic "redirect" {
155-
for_each = length(try(default_action.value.redirect, {})) > 0 ? [1] : []
155+
for_each = (try(default_action.value.redirect, null) != null && length(try(default_action.value.redirect, {})) > 0) ? [1] : []
156156

157157
content {
158158
status_code = default_action.value.redirect.status_code

modules/alb/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ output "arn" {
1212
value = aws_lb.this.arn
1313
}
1414

15+
output "dns_name" {
16+
description = "DNS name of the Load Balancer"
17+
value = aws_lb.this.dns_name
18+
}
19+
20+
output "zone_id" {
21+
description = "Canonical hosted zone ID of the Load Balancer (to be used in a Route 53 Alias record)"
22+
value = aws_lb.this.zone_id
23+
}
24+
1525
################################################################################
1626
# Load Balancer Target Group
1727
################################################################################

0 commit comments

Comments
 (0)