ARM 模板

模板结构

{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { }, "variables": { }, "resources": [ ], "outputs": { } }

参数

"parameters": { "storageAccountName": { "type": "string", "minLength": 3, "maxLength": 24 }, "environment": { "type": "string", "defaultValue": "dev", "allowedValues": ["dev", "staging", "prod"] }, "adminPassword": { "type": "securestring" } }

变量与函数

"variables": { "storageAccountName": "[toLower(concat('storage', uniqueString(resourceGroup().id)))]", "location": "[resourceGroup().location]", "isProd": "[equals(parameters('environment'), 'prod')]", "vmSize": "[if(variables('isProd'), 'Standard_D4s_v3', 'Standard_B2s')]" } // 常用 ARM 函数: // concat(s1, s2) - 字符串拼接 // uniqueString(str) - 确定性哈希 // resourceId(type, name) - 获取资源 ID // reference(id) - 获取资源属性

资源示例

"resources": [{ "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2023-01-01", "name": "[variables('storageAccountName')]", "location": "[variables('location')]", "sku": {"name": "Standard_LRS"}, "kind": "StorageV2", "properties": { "supportsHttpsTrafficOnly": true, "minimumTlsVersion": "TLS1_2" } }]

输出与部署

"outputs": { "storageAccountId": { "type": "string", "value": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]" } } # 部署模板 az deployment group create \ --resource-group myRG \ --template-file template.json \ --parameters @params.dev.json # 预览变更(what-if) az deployment group what-if \ --resource-group myRG \ --template-file template.json