Skip to content

Error: Invalid count argument in Terraform v1.3.0 #31852

@SimonG-Kontentai

Description

@SimonG-Kontentai

Terraform Version

Terraform: 1.3
AzureRM: 3.23.0
AzureAD: 2.28.1
Fastly: 2.3.2

Terraform Configuration Files

simplified/cropped configuration

// Event Hub
variable "event_hub_names" {
  type        = list(string)
  default     = []
}

variable "event_hub" {
  type = object({
    partition_count   = optional(number, 2)
    message_retention = optional(number, 1)
  })
  default = {
    partition_count   = 2
    message_retention = 1
  }
}

// Event Hub Consumer Group Name
variable "consumer_group_names" {
  type        = list(string)
  default     = []
}

locals {
  consumer_groups = flatten([
    for event_hub in azurerm_eventhub.default : [
      for consumer_group_name in var.consumer_group_names : [
        {
          event_hub_name      = event_hub.name
          consumer_group_name = consumer_group_name
        }
      ]
    ]
  ])
}

resource "azurerm_eventhub_namespace" "default" {
  name                = var.event_hub_namespace.name
  location            = var.resource_group.location
  resource_group_name = var.resource_group.name

  auto_inflate_enabled     = var.autoscaling.enabled
  maximum_throughput_units = var.autoscaling.max_units

  sku      = var.event_hub_namespace.sku
  capacity = var.event_hub_namespace.capacity

  lifecycle {
    ignore_changes = [
      capacity
    ]
  }
}

resource "azurerm_eventhub" "default" {
  count = length(var.event_hub_names)

  name                = element(var.event_hub_names, count.index)
  namespace_name      = azurerm_eventhub_namespace.default.name
  resource_group_name = var.resource_group.name

  partition_count   = var.event_hub.partition_count
  message_retention = var.event_hub.message_retention
}

resource "azurerm_eventhub_consumer_group" "default" {
  **count = length(local.consumer_groups)** <- this is the problem

  name                = element(local.consumer_groups, count.index).consumer_group_name
  resource_group_name = var.resource_group.name
  namespace_name      = azurerm_eventhub_namespace.default.name
  eventhub_name       = element(local.consumer_groups, count.index).event_hub_name
}

Debug Output

None

Expected Behavior

Everything is fine with Terraform 1.2.9

Actual Behavior

Error: Invalid count argument

on ....\modules_azure_metaResources\EventHub\eventHub.tf line 47, in resource "azurerm_eventhub_consumer_group" "default":
47: count = length(local.consumer_groups)

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

Steps to Reproduce

A new environment with few resources that must be first imported

Terraform import

Additional Context

No response

References

Metadata

Metadata

Assignees

Labels

bugconfirmeda Terraform Core team member has reproduced this issueexplaineda Terraform Core team member has described the root cause of this issue in code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions