How to Create ARM Template for Azure Infrastructure
To create an
ARM template, write a JSON file that defines Azure resources and their properties using the ARM schema. This template can then be deployed to Azure to create or update resources consistently and repeatedly.Syntax
An ARM template is a JSON file with these main parts:
- $schema: URL defining the template schema version.
- contentVersion: Template version for tracking changes.
- parameters: Inputs to customize deployment.
- variables: Values used inside the template for reuse.
- resources: List of Azure resources to create or update.
- outputs: Values returned after deployment.
json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [],
"outputs": {}
}Example
This example creates a simple Azure Storage Account with a parameter for the account name.
json
{
"$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"
}
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[parameters('storageAccountName')]",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountId": {
"type": "string",
"value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
}
}
}Output
Deploying this template creates a Storage Account with the specified name in the East US region and outputs its resource ID.
Common Pitfalls
Common mistakes when creating ARM templates include:
- Missing or incorrect
$schemaURL causing validation errors. - Using hardcoded values instead of parameters, reducing template reusability.
- Incorrect resource
apiVersionleading to deployment failures. - Forgetting to specify required properties for resources.
- Not validating the template before deployment.
Always validate your template with Azure CLI or PowerShell before deploying.
json
{
"parameters": {
"storageAccountName": {
"type": "string"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "invalid-version",
"name": "fixedname",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {}
}
]
}
// Corrected version:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[parameters('storageAccountName')]",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {}
}
]
}Quick Reference
- $schema: Always use the latest ARM template schema URL.
- parameters: Use to make templates flexible and reusable.
- apiVersion: Use the latest stable API version for each resource type.
- Validate: Use
az deployment group validatebefore deploying. - Outputs: Use to return useful info after deployment.
Key Takeaways
An ARM template is a JSON file defining Azure resources declaratively.
Use parameters to customize deployments and avoid hardcoding values.
Always specify the correct $schema and apiVersion for resources.
Validate your template before deployment to catch errors early.
Outputs help retrieve important information after deployment.