Skip to content

Commit 4647086

Browse files
committed
fix: Replace try() with can(coalesce()) to better handle null values
The default behaviour of `try` will return a `null` value if present. So update the logic to use `can(coalesce())` which will ignore a `null` value.
1 parent 5121d71 commit 4647086

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

main.tf

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ resource "aws_lb_listener" "this" {
9292
certificate_arn = try(each.value.certificate_arn, null)
9393

9494
dynamic "default_action" {
95-
for_each = try([each.value.authenticate_cognito], [])
95+
for_each = can(coalesce(each.value.authenticate_cognito)) ? [each.value.authenticate_cognito] : []
9696

9797
content {
9898
authenticate_cognito {
@@ -112,7 +112,7 @@ resource "aws_lb_listener" "this" {
112112
}
113113

114114
dynamic "default_action" {
115-
for_each = try([each.value.authenticate_oidc], [])
115+
for_each = can(coalesce(each.value.authentication_oidc)) ? [each.value.authenticate_oidc] : []
116116

117117
content {
118118
authenticate_oidc {
@@ -135,7 +135,7 @@ resource "aws_lb_listener" "this" {
135135
}
136136

137137
dynamic "default_action" {
138-
for_each = try([each.value.fixed_response], [])
138+
for_each = can(coalesce(each.value.fixed_response)) ? [each.value.fixed_response] : []
139139

140140
content {
141141
fixed_response {
@@ -150,7 +150,7 @@ resource "aws_lb_listener" "this" {
150150
}
151151

152152
dynamic "default_action" {
153-
for_each = try([each.value.forward], [])
153+
for_each = can(coalesce(each.value.forward)) ? [each.value.forward] : []
154154

155155
content {
156156
order = try(default_action.value.order, null)
@@ -160,7 +160,7 @@ resource "aws_lb_listener" "this" {
160160
}
161161

162162
dynamic "default_action" {
163-
for_each = try([each.value.weighted_forward], [])
163+
for_each = can(coalesce(each.value.weighted_forward)) ? [each.value.weighted_forward] : []
164164

165165
content {
166166
forward {
@@ -189,7 +189,7 @@ resource "aws_lb_listener" "this" {
189189
}
190190

191191
dynamic "default_action" {
192-
for_each = try([each.value.redirect], [])
192+
for_each = can(coalesce(each.value.redirect)) ? [each.value.redirect] : []
193193

194194
content {
195195
order = try(default_action.value.order, null)
@@ -208,7 +208,7 @@ resource "aws_lb_listener" "this" {
208208
}
209209

210210
dynamic "mutual_authentication" {
211-
for_each = try([each.value.mutual_authentication], [])
211+
for_each = can(coalesce(each.value.mutual_authentication)) ? [each.value.mutual_authentication] : []
212212
content {
213213
mode = mutual_authentication.value.mode
214214
trust_store_arn = try(mutual_authentication.value.trust_store_arn, null)
@@ -493,7 +493,7 @@ resource "aws_lb_target_group" "this" {
493493
deregistration_delay = try(each.value.deregistration_delay, null)
494494

495495
dynamic "health_check" {
496-
for_each = try([each.value.health_check], [])
496+
for_each = can(coalesce(each.value.health_check)) ? [each.value.health_check] : []
497497

498498
content {
499499
enabled = try(health_check.value.enabled, null)
@@ -523,7 +523,7 @@ resource "aws_lb_target_group" "this" {
523523
slow_start = try(each.value.slow_start, null)
524524

525525
dynamic "stickiness" {
526-
for_each = try([each.value.stickiness], [])
526+
for_each = can(coalesce(each.value.stickiness)) ? [each.value.stickiness] : []
527527

528528
content {
529529
cookie_duration = try(stickiness.value.cookie_duration, null)
@@ -534,7 +534,7 @@ resource "aws_lb_target_group" "this" {
534534
}
535535

536536
dynamic "target_failover" {
537-
for_each = try(each.value.target_failover, [])
537+
for_each = can(coalesce(each.value.target_failover)) ? each.value.target_failover : []
538538

539539
content {
540540
on_deregistration = target_failover.value.on_deregistration
@@ -543,12 +543,11 @@ resource "aws_lb_target_group" "this" {
543543
}
544544

545545
dynamic "target_group_health" {
546-
for_each = try([each.value.target_group_health], [])
546+
for_each = can(coalesce(each.value.target_group_health)) ? [each.value.target_group_health] : []
547547

548548
content {
549-
550549
dynamic "dns_failover" {
551-
for_each = try([target_group_health.value.dns_failover], [])
550+
for_each = can(coalesce(target_group_health.value.dns_failover)) ? [target_group_health.value.dns_failover] : []
552551

553552
content {
554553
minimum_healthy_targets_count = try(dns_failover.value.minimum_healthy_targets_count, null)
@@ -557,7 +556,7 @@ resource "aws_lb_target_group" "this" {
557556
}
558557

559558
dynamic "unhealthy_state_routing" {
560-
for_each = try([target_group_health.value.unhealthy_state_routing], [])
559+
for_each = can(coalesce(target_group_health.value.unhealthy_state_routing)) ? [target_group_health.value.unhealthy_state_routing] : []
561560

562561
content {
563562
minimum_healthy_targets_count = try(unhealthy_state_routing.value.minimum_healthy_targets_count, null)
@@ -568,7 +567,7 @@ resource "aws_lb_target_group" "this" {
568567
}
569568

570569
dynamic "target_health_state" {
571-
for_each = try([each.value.target_health_state], [])
570+
for_each = can(coalesce(each.value.target_health_state)) ? [each.value.target_health_state] : []
572571
content {
573572
enable_unhealthy_connection_termination = try(target_health_state.value.enable_unhealthy_connection_termination, true)
574573
unhealthy_draining_interval = try(target_health_state.value.unhealthy_draining_interval, null)

0 commit comments

Comments
 (0)