How to pass empty/null from variable.tf and check in main.tf

I am using cloud-foundation-fabric/modules/net-lb-app-int/main.tf at master · GoogleCloudPlatform/cloud-foundation-fabric · GitHub

In my local 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

Hi @ajitAcc,

It’s hard to tell what type this input variable is supposed to have because you used any instead of writing the type you expect.

However, from your examples it seems like you intend this to be an object that is optional. The typical way to represent that is to set the type to an object type constraint and then set the default to null:

variable "health_check_config" {
  description = "Health check onfiguration"
  type = object({
    enable_logging = bool
    # etc, all of the other expected attributes
  })
  default = null
}

You can then either omit this argument altogether in the module call, or set it to null explicitly, with equivalent effect.