Skip to content

Commit 694a7f4

Browse files
authored
refactor(aws_lb): replace lookups with null checks for optional fields (#66)
1 parent 73c3ec0 commit 694a7f4

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

main.tf

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ resource "aws_security_group_rule" "ingress_through_https" {
118118
resource "null_resource" "lb_http_tgs_config" {
119119
for_each = {
120120
for name, config in var.http_ports : name => config
121-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
121+
if config.type == null || config.type == "forward"
122122
}
123123

124124
triggers = {
@@ -131,7 +131,7 @@ resource "null_resource" "lb_http_tgs_config" {
131131
resource "random_id" "lb_http_tgs_id" {
132132
for_each = {
133133
for name, config in var.http_ports : name => config
134-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
134+
if config.type == null || config.type == "forward"
135135
}
136136

137137
byte_length = 2
@@ -147,12 +147,12 @@ resource "random_id" "lb_http_tgs_id" {
147147
resource "aws_lb_target_group" "lb_http_tgs" {
148148
for_each = {
149149
for name, config in var.http_ports : name => config
150-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
150+
if config.type == null || config.type == "forward"
151151
}
152152
name = "${var.name_prefix}-http-${each.value.target_group_port}-${random_id.lb_http_tgs_id[each.key].hex}"
153153
port = each.value.target_group_port
154-
protocol = lookup(each.value, "target_group_protocol", "HTTP")
155-
protocol_version = lookup(each.value, "target_group_protocol_version", "HTTP1")
154+
protocol = coalesce(each.value.target_group_protocol, "HTTP")
155+
protocol_version = coalesce(each.value.target_group_protocol_version, "HTTP1")
156156
vpc_id = var.vpc_id
157157
deregistration_delay = var.deregistration_delay
158158
slow_start = var.slow_start
@@ -166,15 +166,15 @@ resource "aws_lb_target_group" "lb_http_tgs" {
166166
}
167167
}
168168
health_check {
169-
enabled = lookup(each.value, "target_group_health_check_enabled", var.target_group_health_check_enabled)
170-
interval = lookup(each.value, "target_group_health_check_interval", var.target_group_health_check_interval)
171-
path = lookup(each.value, "target_group_health_check_path", var.target_group_health_check_path)
172-
port = lookup(each.value, "target_group_health_check_port", var.target_group_health_check_port)
173-
protocol = lookup(each.value, "target_group_health_check_protocol", var.target_group_health_check_protocol)
174-
timeout = lookup(each.value, "target_group_health_check_timeout", var.target_group_health_check_timeout)
175-
healthy_threshold = lookup(each.value, "target_group_health_check_healthy_threshold", var.target_group_health_check_healthy_threshold)
176-
unhealthy_threshold = lookup(each.value, "target_group_health_check_unhealthy_threshold", var.target_group_health_check_unhealthy_threshold)
177-
matcher = lookup(each.value, "target_group_health_check_matcher", var.target_group_health_check_matcher)
169+
enabled = coalesce(each.value.target_group_health_check_enabled, var.target_group_health_check_enabled)
170+
interval = coalesce(each.value.target_group_health_check_interval, var.target_group_health_check_interval)
171+
path = coalesce(each.value.target_group_health_check_path, var.target_group_health_check_path)
172+
port = coalesce(each.value.target_group_health_check_port, var.target_group_health_check_port)
173+
protocol = coalesce(each.value.target_group_health_check_protocol, var.target_group_health_check_protocol)
174+
timeout = coalesce(each.value.target_group_health_check_timeout, var.target_group_health_check_timeout)
175+
healthy_threshold = coalesce(each.value.target_group_health_check_healthy_threshold, var.target_group_health_check_healthy_threshold)
176+
unhealthy_threshold = coalesce(each.value.target_group_health_check_unhealthy_threshold, var.target_group_health_check_unhealthy_threshold)
177+
matcher = coalesce(each.value.target_group_health_check_matcher, var.target_group_health_check_matcher)
178178
}
179179
target_type = "ip"
180180
tags = merge(
@@ -194,7 +194,7 @@ resource "aws_lb_target_group" "lb_http_tgs" {
194194
resource "null_resource" "lb_https_tgs_config" {
195195
for_each = {
196196
for name, config in var.https_ports : name => config
197-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
197+
if config.type == null || config.type == "forward"
198198
}
199199

200200
triggers = {
@@ -207,7 +207,7 @@ resource "null_resource" "lb_https_tgs_config" {
207207
resource "random_id" "lb_https_tgs_id" {
208208
for_each = {
209209
for name, config in var.https_ports : name => config
210-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
210+
if config.type == null || config.type == "forward"
211211
}
212212

213213
byte_length = 2
@@ -223,12 +223,12 @@ resource "random_id" "lb_https_tgs_id" {
223223
resource "aws_lb_target_group" "lb_https_tgs" {
224224
for_each = {
225225
for name, config in var.https_ports : name => config
226-
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
226+
if config.type == null || config.type == "forward"
227227
}
228228
name = "${var.name_prefix}-https-${each.value.target_group_port}-${random_id.lb_https_tgs_id[each.key].hex}"
229229
port = each.value.target_group_port
230-
protocol = lookup(each.value, "target_group_protocol", "HTTP")
231-
protocol_version = lookup(each.value, "target_group_protocol_version", "HTTP1")
230+
protocol = coalesce(each.value.target_group_protocol, "HTTP")
231+
protocol_version = coalesce(each.value.target_group_protocol_version, "HTTP1")
232232
vpc_id = var.vpc_id
233233
deregistration_delay = var.deregistration_delay
234234
slow_start = var.slow_start
@@ -242,15 +242,15 @@ resource "aws_lb_target_group" "lb_https_tgs" {
242242
}
243243
}
244244
health_check {
245-
enabled = lookup(each.value, "target_group_health_check_enabled", var.target_group_health_check_enabled)
246-
interval = lookup(each.value, "target_group_health_check_interval", var.target_group_health_check_interval)
247-
path = lookup(each.value, "target_group_health_check_path", var.target_group_health_check_path)
248-
port = lookup(each.value, "target_group_health_check_port", var.target_group_health_check_port)
249-
protocol = lookup(each.value, "target_group_health_check_protocol", var.target_group_health_check_protocol)
250-
timeout = lookup(each.value, "target_group_health_check_timeout", var.target_group_health_check_timeout)
251-
healthy_threshold = lookup(each.value, "target_group_health_check_healthy_threshold", var.target_group_health_check_healthy_threshold)
252-
unhealthy_threshold = lookup(each.value, "target_group_health_check_unhealthy_threshold", var.target_group_health_check_unhealthy_threshold)
253-
matcher = lookup(each.value, "target_group_health_check_matcher", var.target_group_health_check_matcher)
245+
enabled = coalesce(each.value.target_group_health_check_enabled, var.target_group_health_check_enabled)
246+
interval = coalesce(each.value.target_group_health_check_interval, var.target_group_health_check_interval)
247+
path = coalesce(each.value.target_group_health_check_path, var.target_group_health_check_path)
248+
port = coalesce(each.value.target_group_health_check_port, var.target_group_health_check_port)
249+
protocol = coalesce(each.value.target_group_health_check_protocol, var.target_group_health_check_protocol)
250+
timeout = coalesce(each.value.target_group_health_check_timeout, var.target_group_health_check_timeout)
251+
healthy_threshold = coalesce(each.value.target_group_health_check_healthy_threshold, var.target_group_health_check_healthy_threshold)
252+
unhealthy_threshold = coalesce(each.value.target_group_health_check_unhealthy_threshold, var.target_group_health_check_unhealthy_threshold)
253+
matcher = coalesce(each.value.target_group_health_check_matcher, var.target_group_health_check_matcher)
254254
}
255255
target_type = "ip"
256256
tags = merge(
@@ -277,7 +277,7 @@ resource "aws_lb_listener" "lb_http_listeners" {
277277
protocol = "HTTP"
278278

279279
dynamic "default_action" {
280-
for_each = lookup(each.value, "type", "") == "redirect" ? [1] : []
280+
for_each = each.value.type == "redirect" ? [1] : []
281281
content {
282282
type = "redirect"
283283

@@ -287,27 +287,27 @@ resource "aws_lb_listener" "lb_http_listeners" {
287287
port = each.value.port
288288
protocol = each.value.protocol
289289
query = each.value.query
290-
status_code = lookup(each.value, "status_code", "HTTP_301")
290+
status_code = coalesce(each.value.status_code, "HTTP_301")
291291
}
292292
}
293293
}
294294

295295
dynamic "default_action" {
296-
for_each = lookup(each.value, "type", "") == "fixed-response" ? [1] : []
296+
for_each = each.value.type == "fixed-response" ? [1] : []
297297
content {
298298
type = "fixed-response"
299299

300300
fixed_response {
301301
content_type = each.value.content_type
302302
message_body = each.value.message_body
303-
status_code = lookup(each.value, "status_code", "200")
303+
status_code = coalesce(each.value.status_code, "200")
304304
}
305305
}
306306
}
307307

308308
# We fallback to using forward type action if type is not defined
309309
dynamic "default_action" {
310-
for_each = (lookup(each.value, "type", "") == "" || lookup(each.value, "type", "") == "forward") ? [1] : []
310+
for_each = (each.value.type == null || each.value.type == "forward") ? [1] : []
311311
content {
312312
target_group_arn = aws_lb_target_group.lb_http_tgs[each.key].arn
313313
type = "forward"
@@ -332,7 +332,7 @@ resource "aws_lb_listener" "lb_https_listeners" {
332332
certificate_arn = var.default_certificate_arn
333333

334334
dynamic "default_action" {
335-
for_each = lookup(each.value, "type", "") == "redirect" ? [1] : []
335+
for_each = each.value.type == "redirect" ? [1] : []
336336
content {
337337
type = "redirect"
338338

@@ -342,27 +342,27 @@ resource "aws_lb_listener" "lb_https_listeners" {
342342
port = each.value.port
343343
protocol = each.value.protocol
344344
query = each.value.query
345-
status_code = lookup(each.value, "status_code", "HTTP_301")
345+
status_code = coalesce(each.value.status_code, "HTTP_301")
346346
}
347347
}
348348
}
349349

350350
dynamic "default_action" {
351-
for_each = lookup(each.value, "type", "") == "fixed-response" ? [1] : []
351+
for_each = each.value.type == "fixed-response" ? [1] : []
352352
content {
353353
type = "fixed-response"
354354

355355
fixed_response {
356356
content_type = each.value.content_type
357357
message_body = each.value.message_body
358-
status_code = lookup(each.value, "status_code", "200")
358+
status_code = coalesce(each.value.status_code, "200")
359359
}
360360
}
361361
}
362362

363363
# We fallback to using forward type action if type is not defined
364364
dynamic "default_action" {
365-
for_each = (lookup(each.value, "type", "") == "" || lookup(each.value, "type", "") == "forward") ? [1] : []
365+
for_each = (each.value.type == null || each.value.type == "forward") ? [1] : []
366366
content {
367367
target_group_arn = aws_lb_target_group.lb_https_tgs[each.key].arn
368368
type = "forward"

0 commit comments

Comments
 (0)