简体   繁体   中英

Terraform "ocasional" variable

I have built the following Route 53 module to create a weighted record along with healthcheck:

resource "aws_route53_health_check" "health_check" {
  for_each              = var.weighted-records
  ip_address            = each.value.ip_address
  fqdn                  = each.value.fqdn
  port                  = each.value.port
  type                  = each.value.type
  resource_path         = each.value.resource_path
  failure_threshold     = each.value.failure_threshold
  request_interval      = each.value.request_interval
  search_string         = each.value.search_string
  measure_latency       = each.value.measure_latency
  invert_healthcheck    = each.value.invert_healthcheck

  tags = {
    Name = format("chk-%s", each.value.name)
  }
}
resource "aws_route53_record" "weighted_record" {
  for_each        = var.weighted-records
  zone_id         = each.value.zone_id
  name            = each.value.dns_name
  type            = each.value.dns_type
  ttl             = each.value.ttl
  health_check_id = aws_route53_health_check.health_check[each.key].id

  weighted_routing_policy {
    weight = each.value.weight
  }

  set_identifier = each.value.name
  records        = [each.value.ip_address]
}

In most cases, my Health check IP is the same as the destination IP, so I use the same VAR: each.value.ip_address. However, in some cases, I need to specify a different IP for a health check. Is there a way to avoid introducing a new VAR for health check IP and put it in every record I build? I would like to have the VAR specified only if required.

I was hoping to use either "dynamic" or "try" functions.

Usually you specify optimal variables, by creating some default value for them:

variable "new_ip" {
   default = ""
}

Then you check if the new_ip was provided or not:

ip_address  = var.new_ip != "" ? var.new_ip : each.value.ip_address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM