0
votes

Trying to run modules conditionally.

Expectation : Run module only when env is not equal to prd

    module "database_diagnostic_eventhub_setting" {
      count       = var.env != "prd" ? 1 : 0     // run block if condition is satisfied 
      source      = "git::https://git_url//modules/...."
      target_ids  =  [
        "${data.terraform_remote_state.database.outputs.server_id}"
      ]
      environment = "${var.environment}-database-eventhub"
      destination = data.azurerm_eventhub_namespace_authorization_rule.event_hub.id
      eventhub_name = var.eventhub_name
      logs = [
        "PostgreSQLLogs",
        "QueryStoreWaitStatistics"
      ]
    }

Error:

The name "count" is reserved for use in a future version of Terraform.
1
Which version of TF do you have installed? Support for conditional module creation via count was added in 0.13, see top of terraform.io/docs/language/meta-arguments/count.htmlkost
@kost That should most likely go as an answerGrzegorz Oledzki

1 Answers

1
votes

You need to use Terraform v0.13 or later in order to use count or for_each inside a module block.

If you can't upgrade from Terraform v0.12 then the old approach, prior to support for module repetition, was to add a variable to your module to specify the object count:

variable "instance_count" {
  type = number
}

...and then inside your module add count to each of the resources:

resource "example" "example" {
  count = var.instance_count
}

However, if you are able to upgrade to Terraform v0.13 now then I would strongly suggest doing so rather than using the above workaround, because upgrading to use module-level count later, with objects already created, is quite a fiddly process involving running terraform state mv for each of your resource in that module.