Conditional Resource Deployment
What you'll learn: How to deploy Azure resources only when specific conditions are met using the condition property in ARM templates.
Sometimes you want a resource to exist only in certain scenarios—like creating a public IP address only when a parameter says the VM needs one, or deploying a dev environment database differently than production. Azure ARM templates let you make resources optional using conditional deployment.
The Core Idea
In ARM templates, you add a condition property to any resource. This property evaluates a boolean expression (true/false). If the condition evaluates to true, ARM deploys the resource. If false, ARM skips it entirely—as if it never existed in your template.
Think of it like an "if-statement" for infrastructure: "If the user wants a public IP, create one; otherwise, don't."
How It Works
You've already learned about parameters, variables, and functions. Now combine them with the condition property:
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2021-02-01",
"name": "myPublicIP",
"condition": "[parameters('deployPublicIP')]",
"location": "[resourceGroup().location]",
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
}
Here, deployPublicIP is a boolean parameter. If someone deploys your template with deployPublicIP set to false, this public IP resource won't be created at all.
You can write more complex conditions using comparison operators:
"condition": "[equals(parameters('environment'), 'production')]"
This creates the resource only when environment equals 'production'.
Use Cases
- Deploy monitoring tools only in production
- Create dev resources differently than prod resources
- Make expensive components optional for testing
- Handle multi-region deployments selectively
Key Takeaway: Use the condition property to control whether individual resources deploy based on parameter values, making your templates flexible and reusable across different scenarios without maintaining multiple template files.