Overview
Azure extensions provide additional capabilities to Azure services such as Azure Virtual Machines, Azure Functions, and Azure Kubernetes Service (AKS). They enable you to manage custom scripts, monitoring agents, and third‑party tools directly from the Azure portal or through Azure CLI/PowerShell.
Key benefits:
- Plug‑and‑play functionality.
- Consistent deployment across environments.
- Seamless integration with Azure Resource Manager (ARM) templates.
Installing Extensions
Extensions can be installed via the Azure portal, Azure CLI, PowerShell, or ARM templates.
Azure CLI
az vm extension set \
--publisher Microsoft.Azure.Extensions \
--name CustomScript \
--resource-group MyResourceGroup \
--vm-name MyVM \
--settings '{"fileUris":["https://example.com/script.sh"],"commandToExecute":"sh script.sh"}'
PowerShell
Set-AzVMExtension `
-ResourceGroupName "MyResourceGroup" `
-VMName "MyVM" `
-Name "CustomScriptExtension" `
-Publisher "Microsoft.Azure.Extensions" `
-ExtensionType "CustomScript" `
-TypeHandlerVersion "2.0" `
-Setting @{ "fileUris" = @("https://example.com/script.ps1"); "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File script.ps1" }
Managing Extensions
Use the following commands to list, update, or remove extensions.
Action | CLI Command |
---|---|
List Extensions | az vm extension list --resource-group MyRG --vm-name MyVM |
Update Extension | az vm extension set ... |
Remove Extension | az vm extension delete --resource-group MyRG --vm-name MyVM --name CustomScript |
Sample Code: Deploying a Monitoring Agent
The example below deploys the Azure Monitor Linux Agent to a VM using an ARM template.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(parameters('vmName'), '/OmsAgentForLinux')]",
"apiVersion": "2021-03-01",
"location": "[resourceGroup().location]",
"properties": {
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"type": "OmsAgentForLinux",
"typeHandlerVersion": "1.13",
"autoUpgradeMinorVersion": true,
"settings": {
"workspaceId": "[parameters('logAnalyticsWorkspaceId')]"
},
"protectedSettings": {
"workspaceKey": "[listKeys(resourceId('Microsoft.OperationalInsights/workspaces', parameters('logAnalyticsWorkspaceName')), '2015-11-01-preview').primarySharedKey]"
}
}
}
]
}
API Reference
For programmatic access, use the Azure REST API Virtual Machine Extensions API.
- GET
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/extensions?api-version=2022-08-01
- PUT
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/extensions/{extensionName}?api-version=2022-08-01
- DELETE
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/extensions/{extensionName}?api-version=2022-08-01