Is it possible to create resource contents conditionally in terraform?
Let's say I want the default_action for this aws_lb_listener to be have "type" set to "forward" if "default" variable is set to 1; otherwise, set "type" to "fixed-response" and add additional "fixed_response" part like below:
resource "aws_lb_listener" "listener_https" {
load_balancer_arn = aws_lb.lb.arn
certificate_arn = aws_acm_certificate.certificate.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
default_action {
if var.default == 1 { # pseudocode, does not work with terraform!
target_group_arn = aws_lb_target_group.tg.arn
type = "forward"
} else { # pseudocode, does not work with terraform!
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "not found"
status_code = "404"
}
}
}
}