Introduction to ARM Templates
Azure Resource Manager (ARM) templates are JSON files that define the infrastructure and configuration for your Azure deployment. They enable you to consistently and reliably deploy your applications across different environments.
With ARM templates, you can:
- Declaratively define your infrastructure: Describe what you want to deploy, not how to deploy it.
- Automate deployments: Integrate templates into your CI/CD pipelines for repeatable deployments.
- Ensure consistency: Guarantee that your environments are deployed in the same way every time.
- Manage dependencies: Define relationships between resources for ordered deployment.
Key Benefits
Using ARM templates helps achieve:
- Idempotency
- Reusability
- Scalability
- Version Control
Core Concepts
Understanding these core concepts is crucial for effective ARM template authoring:
- Resource: Any Azure service that can be managed, deployed, and protected, such as a virtual machine, storage account, or virtual network.
- Resource Provider: A service that offers Azure resources and provides the RESTful operations to interact with those resources.
- API Version: Specifies the version of the resource provider's API to use when deploying a resource.
- Deployment Scope: Templates can be deployed at the subscription, resource group, or management group level.
Template Structure
An ARM template is a JSON file with the following top-level elements:
$schema: Specifies the URI of the JSON schema file that defines the ARM template structure.contentVersion: A version number for the template.apiProfile: (Optional) Specifies the API profile for the deployment.parameters: (Optional) Input values that you provide when deploying the template.variables: (Optional) Values that can be used in the template to simplify expressions.resources: The core of the template, defining the Azure resources to deploy.outputs: (Optional) Values that are returned after the deployment is complete.
Parameters
Parameters allow you to customize deployments by passing in values. They are defined in the parameters section and referenced throughout the template.
Each parameter can have a:
type(e.g., string, int, bool, object, array)defaultValueallowedValuesminValue,maxValueminLength,maxLengthmetadata(for display name and description)
Parameter Example:
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Premium_LRS"
],
"metadata": {
"description": "The SKU of the storage account."
}
}
}
Variables
Variables are used to define reusable values within your template. They can simplify complex expressions and make your templates more readable.
Variable Example:
"variables": {
"storageAccountNamePrefix": "[concat(uniqueString(resourceGroup().id), 'storage')]",
"location": "[resourceGroup().location]",
"storageAccountName": "[variables('storageAccountNamePrefix')]"
}
Resources
The resources section defines the Azure resources to be deployed. Each resource has a type, apiVersion, name, and properties.
type: The type of resource (e.g.,Microsoft.Storage/storageAccounts).apiVersion: The version of the resource provider's API.name: The name of the resource.location: The Azure region for the resource.properties: Configuration details specific to the resource type.dependsOn: (Optional) Specifies dependencies on other resources in the template.
Resource Example (Storage Account):
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "[variables('storageAccountName')]",
"location": "[variables('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "StorageV2"
}
Outputs
Outputs are used to return values from your deployment, such as connection strings or resource IDs. These can be used by subsequent deployments or for informational purposes.
Output Example (Storage Account Name):
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[variables('storageAccountName')]"
}
}
Deployment
You can deploy ARM templates using various tools:
- Azure Portal
- Azure CLI
- Azure PowerShell
- REST API
- Azure SDKs
Example Azure CLI deployment:
az deployment group create --resource-group myResourceGroup --template-file mainTemplate.json --parameters storageAccountType=Standard_GRS
Best Practices
Follow these best practices for robust and maintainable ARM templates:
- Use parameters effectively: Don't hardcode values.
- Leverage variables: For repeated values and complex expressions.
- Use
dependsOncarefully: Let Azure infer dependencies where possible. - Structure your templates: Break down complex deployments into smaller, linked templates.
- Use naming conventions: For resources and parameters.
- Validate your templates: Use tools like the ARM template validator.
- Keep API versions up-to-date: To use the latest features and security updates.
Examples
Explore our collection of pre-built ARM template examples for common scenarios:
- Deploying a Web App
- Deploying a SQL Server Database
- Deploying a Virtual Machine
- Deploying an Azure Kubernetes Service (AKS) Cluster
You can also find a comprehensive library of templates in the Azure Quickstart Templates repository.
Get Started with ARM Templates