Conditional empty object in resource

I’m trying to add an ON/OFF toggle for an object value passed as variable with Terraform v0.12.30:

module "my_module" {
  log_configuration = local.booleanValue ? {} : {
      logDriver = "awslogs"
      options = {
        awslogs-group         = "/ecs/${var.environment}/my_module"
        awslogs-region        = data.aws_region.current.name
        awslogs-stream-prefix = "profile"
      }
      secretOptions = []
    }
  }
[...]

but it errors with:

The true and false result expressions must have consistent types. The given expressions are object and object, respectively.

which seems to be related with this issue: Inconsistent conditional result types fails for complex types · Issue #22405 · hashicorp/terraform · GitHub

I’ve tried to wrap the two objects in a jsonencode function but it won’t work either: The given value is not suitable for child module variable "log_configuration" defined at

The use case looks common to me (decide to either pass a null kind of value or not depending on other configuration) but I don’t understand how I’m supposed to achieve that.

Thanks for the help

Without seeing your module code, I can’t be certain of the right solution here. Can you share more about your module, the source of this configuration, and what you’re trying to achieve?

In Terraform, there is no type which is equivalent to an arbitrary nested structure (e.g. a Ruby hash, a Python dict, or a JavaScript object). Instead, we have the object type, which is more akin to a struct in languages like C, Go, or Rust.

Any two object values of the same type must have the same attribute names and types. An empty object can only be substituted for another empty object, and cannot be used in place of an object with attributes.

The null value for any given object type is null. You can change your code to pass null instead of the full object, and it should work:

module "my_module" {
  log_configuration = local.booleanValue ? null : {
      logDriver = "awslogs"
      options = {
        awslogs-group         = "/ecs/${var.environment}/my_module"
        awslogs-region        = data.aws_region.current.name
        awslogs-stream-prefix = "profile"
      }
      secretOptions = []
    }
  }
[...]

If you do this, your module will have to be updated to cope with a null value for the log_configuration value. How to do that depends on how the configuration is used in the module.

It’s possible that your use case might be met by the optional attributes experiment, combined with the defaults function. To be able to suggest how to do that, I’d need to know more about your entire configuration—it’s not a drop-in solution for this exact block of code.

First of all thanks a lot @alisdair for the thorough explanation about this, and apologies for not adding all the bits in the first place.

Actually the module is public on GitHub and if I understand it correctly the above toggled configuration is supposed to populate this variable here:

I’m following your use null suggestion and it definitely seems to work all right, thanks again.