terraform variable check for empty object

283 views Asked by At

I have following variable in variable.tf

variable "health_check_config" {
  description = "Health check onfiguration"
  type        = any
  default = {}
}

and this is used in main.tf code as

backend_service_configs = {
    default = {
      backends = [{
      health_checks = ["healthcheck"]
     } }
  health_check_configs = {
    healthcheck = var.health_check_config
  }

and if I define this variable in my .tfvars as below

health_check_config = {
  https               = { port = 443 }
  enable_logging      = true
  healthy_threshold   = 2
  timeout_sec         = 5
  unhealthy_threshold = 2
}

this works fine, but now I want to give this variable empty as

health_check_config = {}

but this failed. How I can define health_check_config empty in .tfvars or what needs to change in main.tf block to accept both empty as well as full health_check_config object

Thanks

1

There are 1 answers

0
Tynchtyk On

It depends on what resource are you using. If 'health_check_config' is a nested block then it is better to use dynamic block. In your case you have to modify 'main.tf' file.

backend_service_configs = {
#...
  health_check_configs = var.health_check_config
}

and now you can make it work in tfvars:

health_check_config = {
  healthcheck = {
    https               = { port = 443 }
    enable_logging      = true
    healthy_threshold   = 2
    timeout_sec         = 5
    unhealthy_threshold = 2
  }
}

if you want to make it empty, just try to put 'null' value in .tfvars:

health_check_config = null