CodexBloom - Programming Q&A Platform

Terraform Resource Count implementation guide with Conditional Logic for Azure VM Scale Sets

๐Ÿ‘€ Views: 157 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-07-15
terraform azure vm-scale-set HCL

Does anyone know how to I've been banging my head against this for hours... I've been struggling with this for a few days now and could really use some help. I'm having trouble with my Terraform code where I want to dynamically adjust the number of instances in an Azure VM Scale Set based on a variable input. I set up a conditional count based on an input variable, but it seems that Terraform does not recognize the changes correctly, and I'm getting the following behavior when trying to apply my changes: `behavior: Invalid count argument`. Hereโ€™s the relevant part of my Terraform configuration: ```hcl variable "instance_count" { description = "Number of instances in the scale set" type = number default = 3 } resource "azurerm_virtual_machine_scale_set" "example" { name = "example-vmss" location = var.location resource_group_name = azurerm_resource_group.example.name sku = "Standard_DS1_v2" instances = var.instance_count upgrade_policy { mode = "Automatic" } // Additional configuration omitted for brevity } ``` I tried changing the `instances` attribute to use a conditional expression, but the errors continue. Hereโ€™s what I attempted: ```hcl instances = var.instance_count > 0 ? var.instance_count : 1 ``` However, when I apply this, I still get errors indicating that `count` must be a positive integer. Iโ€™ve verified that the variable is being set correctly in `terraform.tfvars`, and even tried hardcoding the value to test, but nothing seems to work. Could someone guide to understand why Terraform isn't applying the instance count as expected? Is there a better way to handle this with Azure Scale Sets in Terraform? For context: I'm using Hcl on Ubuntu. Is there a better approach? This is part of a larger web app I'm building. This is part of a larger CLI tool I'm building. The project is a REST API built with Hcl.