ARM template parameters and variables in Azure - Commands & Configuration
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"metadata": {
"description": "Name of the storage account"
}
},
"location": {
"type": "string",
"defaultValue": "eastus",
"metadata": {
"description": "Location for all resources"
}
}
},
"variables": {
"storageAccountType": "Standard_LRS",
"storageAccountId": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[variables('storageAccountType')]"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountResourceId": {
"type": "string",
"value": "[variables('storageAccountId')]"
}
}
}parameters: Define inputs you provide when deploying, like storage account name and location.
variables: Store fixed or computed values used inside the template, like the storage account type and resource ID.
resources: Define the Azure resources to create, using parameters and variables for configuration.
outputs: Return useful information after deployment, here the resource ID of the storage account.
az deployment group create --resource-group example-rg --template-file azuredeploy.json --parameters storageAccountName=examplestorage123
--resource-group - Specifies the Azure resource group to deploy to--template-file - Specifies the ARM template file to use--parameters - Sets the input parameters for the templateaz deployment group show --resource-group example-rg --name deployment1
--resource-group - Specifies the resource group of the deployment--name - Specifies the deployment name to queryIf you remember nothing else from this pattern, remember: parameters let you customize your template inputs, and variables let you reuse fixed or computed values inside the template.
Practice
What is the main purpose of parameters in an ARM template?
Solution
Step 1: Understand parameters role
Parameters let users provide input values at deployment time, making templates flexible.Step 2: Differentiate from variables
Variables store fixed or computed values inside the template, not input values.Final Answer:
To allow input values when deploying the template -> Option BQuick Check:
Parameters = input values [OK]
- Confusing parameters with variables
- Thinking parameters define resources
- Assuming parameters are fixed values
Which of the following is the correct syntax to declare a variable named storageAccountName in an ARM template?
{
"variables": {
"storageAccountName": "myStorage"
}
}Solution
Step 1: Identify variable declaration section
Variables are declared inside the "variables" section as key-value pairs.Step 2: Match syntax with example
The example shows "variables": { "storageAccountName": "myStorage" }, which matches "variables": { "storageAccountName": "myStorage" }.Final Answer:
"variables": { "storageAccountName": "myStorage" } -> Option CQuick Check:
Variables declared under "variables" [OK]
- Putting variables inside parameters
- Confusing variables with outputs
- Using wrong JSON section names
Given this ARM template snippet, what will be the value of variables('fullName')?
{
"parameters": {
"firstName": { "type": "string", "defaultValue": "John" },
"lastName": { "type": "string", "defaultValue": "Doe" }
},
"variables": {
"fullName": "[concat(parameters('firstName'), ' ', parameters('lastName'))]"
}
}Solution
Step 1: Understand concat function in variables
The variable "fullName" uses concat to join firstName, a space, and lastName.Step 2: Substitute default parameter values
firstName = "John", lastName = "Doe", so concat("John", " ", "Doe") = "John Doe".Final Answer:
John Doe -> Option DQuick Check:
concat with space joins names [OK]
- Taking concat as literal string
- Ignoring space between names
- Confusing parameters with variables
Identify the error in this ARM template snippet:
{
"parameters": {
"location": { "type": "string" }
},
"variables": {
"region": "parameters('location')"
}
}Solution
Step 1: Check variable referencing syntax
Variables referencing parameters must use expression syntax with square brackets: "[parameters('location')]".Step 2: Identify missing brackets
The variable "region" is assigned "parameters('location')" as a string, not an expression.Final Answer:
Missing square brackets around the parameter reference in variable -> Option AQuick Check:
Parameter references need [ ] in variables [OK]
- Omitting square brackets for expressions
- Thinking variables can't use parameters
- Wrong parameter types for variables
You want to create an ARM template that deploys a storage account with a name based on a parameter prefix and a variable uniqueString. Which is the best way to combine them in the template?
{
"parameters": {
"prefix": { "type": "string" }
},
"variables": {
"uniqueString": "[uniqueString(resourceGroup().id)]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "???",
"apiVersion": "2022-09-01",
"location": "[resourceGroup().location]"
}
]
}Choose the correct expression to use for the storage account name property.
Solution
Step 1: Understand ARM template expression syntax
Expressions must be inside square brackets and use functions like concat() to join strings.Step 2: Check correct usage of parameters and variables
Use parameters('name') and variables('name') inside concat() within brackets.Step 3: Evaluate options
"[concat(parameters('prefix'), variables('uniqueString'))]" uses correct syntax: "[concat(parameters('prefix'), variables('uniqueString'))]".Final Answer:
"[concat(parameters('prefix'), variables('uniqueString'))]" -> Option AQuick Check:
Use [concat(parameters('x'), variables('y'))] for combined names [OK]
- Using + instead of concat inside brackets
- Omitting square brackets for expressions
- Wrong syntax for parameters or variables
Ready to prove it?
Pass the test and this pattern moves unknown
