Last Updated on August 7, 2025 by Arnav Sharma
When working with infrastructure as code using Terraform, often there are scenarios where certain resources need to be created conditionally. This can be due to various reasons such as environment differences, cost considerations, or feature flagging. In Terraform, this conditional logic can be handled elegantly using the count parameter. In this blog post, we will explore how to use conditional statements to control the creation of resources, specifically in Microsoft Azure.
Understanding the count Parameter
The count parameter in Terraform is a meta-argument used to specify the number of instances of a module or resource to create. If count is set to 0, no instances will be created. This makes count a powerful tool for conditional logic.
Example Scenario: Conditional Resource Deployment
Imagine a scenario where we want to create an Azure virtual network and a subnet only if a certain condition is true. This is typically managed using Terraform resource `count` parameter. This could be dependent on whether the deployment is meant for a production environment or not.
Step 1: Define the Variable
First, define a boolean variable that will control whether the resources should be deployed. This could be defined in your variables.tf file:
variable "deploy_production" {
description = "Flag to determine if the resources should be deployed to production"
type = bool
default = false
}
variable "resource_group_name" {
description = "Name of the resource group"
type = string
}
variable "location" {
description = "Azure region for the resource group"
type = string
}
Step 2: Create the Resources Conditionally
In your main.tf file, use the count argument to conditionally create the Azure resources. For example:
resource "azurerm_resource_group" "example" {
count = var.deploy_production ? 1 : 0
name = var.resource_group_name
location = var.location
}
resource "azurerm_virtual_network" "example" {
count = var.deploy_production ? 1 : 0
name = "vnet-example"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example[0].location
resource_group_name = azurerm_resource_group.example[0].name
}
resource "azurerm_subnet" "example" {
count = var.deploy_production ? 1 : 0
name = "subnet-example"
resource_group_name = azurerm_resource_group.example[0].name
virtual_network_name = azurerm_virtual_network.example[0].name
address_prefixes = ["10.0.1.0/24"]
}
In the above example, the Azure resources will only be created if deploy_production is set to true. The count meta-argument uses a simple ternary operation to check the condition: if deploy_production is true, count is set to 1, otherwise it’s 0.
Benefits of Conditional Logic in Terraform
- Cost Efficiency: Only create resources when necessary, avoiding unnecessary costs in development or staging environments.
- Environment Specific Configuration: Tailor your infrastructure for different environments (development, staging, production) without needing multiple configurations.
- Feature Flagging: Gradually roll out features or infrastructure updates in a controlled manner.