Mastering Variables in ARM Templates: A Comprehensive Guide

Learn how to use variables in ARM templates!

Anders Moth Falk

Variables in ARM templates help you manage values consistently throughout your template by storing reusable data like calculated names, settings, and resource properties. Using variables improves readability and allows you to perform complex calculations.

Why use Variables in ARM templates

  • Simplifies Maintenance: Reduces duplication, making it easy to update values.
  • Enhances Readability: Shortens lengthy expressions by storing complex logic in a single place.
  • Supports Complex Logic: Allows the use of ARM functions to calculate values dynamically.
  • Types of Variables and Their Uses

  • Basic Variable:
  • Stores a simple string, integer, or Boolean value for straightforward reuse.
    "variables": {
    	"resourceLocation": "West US"
    }
    This approach ensures unique names while keeping them consistent across template deployments.
  • Calculated Variables
  • Using ARM template functions, you can compute values based on parameters or other data.
    "variables": {
    	"storageAccountName": "[concat('storage', uniqueString(resourceGroup().id))]"
    }
  • Array Variables
  • Arrays are useful for storing lists of data, such as subnet prefixes, in a reusable format.
    "variables": {
    	"subnetPrefixes": ["10.0.1.0/24", "10.0.2.0/24"]
    }
  • Object Variables
  • Objects are useful for more complex data structures, allowing you to store multiple properties in a single variable for defining configurations.
    "variables": {
        "vmConfig": {
            "vmSize": "Standard_B2ms",
            "osDiskSizeGB": 64
        }
    }

    Examples of Using Functions in Variables

    Functions let you dynamically generate values within variables. Some commonly used functions include:
  • concat: Combines strings, useful for naming.
  • uniqueString: Generates a consistent but unique string from input values.
  • resourceId: Retrieves the ID of a resource based on inputs like the resource type and name.
  • reference: Allows you to pull properties from an existing resource within the template.
  • Example: Unique Naming Using concat and uniqueString
    "variables": {
        "uniqueStorageName": "[concat('storage', uniqueString(resourceGroup().id))]"
    }
    Ensures unique naming, which is crucial for resources such as storage accounts that require globally unique names.

    Practical Tips for Working with Variables

  • Use Variables for Dynamic Calculations: When defining properties like storage account names, IP ranges, or VM settings, using variables enables quick changes across your template.
  • Leverage Objects and Arrays for Configurations: Organizing settings in arrays and objects allows you to define complex configurations in a single place, simplifying management.
  • Avoid Hardcoding Values: Use variables for any value that might change, enabling flexibility and reducing the risk of errors during deployments.
  • Using a Parameter in a Variable

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "baseStorageAccountName": {
                "type": "string",
                "defaultValue": "mystorage",
                "metadata": {
                    "description": "Base name for the storage account."
                }
            }
        },
        "variables": {
            "uniqueStorageAccountName": "[concat(parameters('baseStorageAccountName'), uniqueString(resourceGroup().id))]"
        },
        "resources": [
            {
                "name": "[variables('uniqueStorageAccountName')]",
                "type": "Microsoft.Storage/storageAccounts",
                "apiVersion": "2021-01-01",
                "location": "[resourceGroup().location]",
                "sku": {
                    "name": "Standard_LRS"
                },
                "kind": "StorageV2",
                "properties": {}
            }
        ],
        "outputs": {}
    }
  • Parameter: baseStorageAccountName is a user-defined parameter where the base name for the storage account is provided.
  • Variable: uniqueStorageAccountName combines the baseStorageAccountName parameter with a unique suffix using uniqueString().
  • Usage in Resource: The uniqueStorageAccountName variable is then used in the resource name, creating a globally unique storage account name based on the base name and resource group ID.
  • Using variables efficiently in ARM templates leads to more modular, reusable, and maintainable code. They enable dynamic configurations that adapt to different environments, making deployments smoother and reducing manual updates.