Skip to content

Commit fb74b20

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 fb74b20

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

main.tf

Lines changed: 22 additions & 23 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,12 +160,12 @@ 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 {
167167
dynamic "target_group" {
168-
for_each = try(default_action.value.target_groups, [])
168+
for_each = can(coalesce(default_action.value.target_groups)) ? default_action.value.target_groups : []
169169

170170
content {
171171
arn = try(target_group.value.arn, aws_lb_target_group.this[target_group.value.target_group_key].arn, null)
@@ -174,7 +174,7 @@ resource "aws_lb_listener" "this" {
174174
}
175175

176176
dynamic "stickiness" {
177-
for_each = try([default_action.value.stickiness], [])
177+
for_each = can(coalesce(default_action.value.stickiness)) ? [default_action.value.stickiness] : []
178178

179179
content {
180180
duration = try(stickiness.value.duration, 60)
@@ -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)
@@ -367,7 +367,7 @@ resource "aws_lb_listener_rule" "this" {
367367

368368
content {
369369
dynamic "host_header" {
370-
for_each = try([condition.value.host_header], [])
370+
for_each = can(coalesce(condition.value.host_header)) ? [condition.value.host_header] : []
371371

372372
content {
373373
values = host_header.value.values
@@ -381,7 +381,7 @@ resource "aws_lb_listener_rule" "this" {
381381

382382
content {
383383
dynamic "http_header" {
384-
for_each = try([condition.value.http_header], [])
384+
for_each = can(coalesce(condition.value.http_header)) ? [condition.value.http_header] : []
385385

386386
content {
387387
http_header_name = http_header.value.http_header_name
@@ -396,7 +396,7 @@ resource "aws_lb_listener_rule" "this" {
396396

397397
content {
398398
dynamic "http_request_method" {
399-
for_each = try([condition.value.http_request_method], [])
399+
for_each = can(coalesce(condition.value.http_request_method)) ? [condition.value.http_request_method] : []
400400

401401
content {
402402
values = http_request_method.value.values
@@ -410,7 +410,7 @@ resource "aws_lb_listener_rule" "this" {
410410

411411
content {
412412
dynamic "path_pattern" {
413-
for_each = try([condition.value.path_pattern], [])
413+
for_each = can(coalesce(condition.value.path_pattern)) ? [condition.value.path_pattern] : []
414414

415415
content {
416416
values = path_pattern.value.values
@@ -424,7 +424,7 @@ resource "aws_lb_listener_rule" "this" {
424424

425425
content {
426426
dynamic "query_string" {
427-
for_each = try(flatten([condition.value.query_string]), [])
427+
for_each = can(coalesce(condition.value.query_string)) ? [condition.value.query_string] : []
428428

429429
content {
430430
key = try(query_string.value.key, null)
@@ -439,7 +439,7 @@ resource "aws_lb_listener_rule" "this" {
439439

440440
content {
441441
dynamic "source_ip" {
442-
for_each = try([condition.value.source_ip], [])
442+
for_each = can(coalesce(condition.value.source_ip)) ? [condition.value.source_ip] : []
443443

444444
content {
445445
values = source_ip.value.values
@@ -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)