Azure 快速入门模板 是社区参与模板的存储库。 可以在模板开发中使用示例模板。 在本教程中,你将找到网站资源定义并将其添加到自己的模板。 此说明需要 12 分钟 才能完成。
先决条件
建议完成 有关导出模板的教程,但这不是必需的。
需要具有 Visual Studio Code,以及 Azure PowerShell 或 Azure Command-Line 接口(CLI)。 有关详细信息,请参阅 模板工具。
查看模板
在上一教程结束时,模板具有以下 JSON 文件:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storagePrefix": {
"type": "string",
"minLength": 3,
"maxLength": 11
},
"storageSKU": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Premium_LRS"
]
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"appServicePlanName": {
"type": "string",
"defaultValue": "exampleplan"
}
},
"variables": {
"uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": "[variables('uniqueStorageName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageSKU')]"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2021-03-01",
"name": "[parameters('appServicePlanName')]",
"location": "[parameters('location')]",
"sku": {
"name": "B1",
"tier": "Basic",
"size": "B1",
"family": "B",
"capacity": 1
},
"kind": "linux",
"properties": {
"perSiteScaling": false,
"reserved": true,
"targetWorkerCount": 0,
"targetWorkerSizeId": 0
}
}
],
"outputs": {
"storageEndpoint": {
"type": "object",
"value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
}
}
}
此模板适用于部署存储帐户和应用服务计划,但可能需要向其添加网站。 可以使用预建模板快速发现部署资源所需的 JSON。
查找模板
打开 Azure 快速入门模板
选择标题为 “部署基本 Linux Web 应用”的磁贴。 如果在找到它时遇到问题,下面是 直接链接。
选择在GitHub上浏览。
选择 azuredeploy.json。
查看模板。 查找
Microsoft.Web/sites
资源。
修改现有模板
将快速入门模板与现有模板合并:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storagePrefix": {
"type": "string",
"minLength": 3,
"maxLength": 11
},
"storageSKU": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Premium_LRS"
]
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"appServicePlanName": {
"type": "string",
"defaultValue": "exampleplan"
},
"webAppName": {
"type": "string",
"metadata": {
"description": "Base name of the resource such as web app name and app service plan "
},
"minLength": 2
},
"linuxFxVersion": {
"type": "string",
"defaultValue": "php|7.0",
"metadata": {
"description": "The Runtime stack of current web app"
}
}
},
"variables": {
"uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]",
"webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": "[variables('uniqueStorageName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageSKU')]"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2021-03-01",
"name": "[parameters('appServicePlanName')]",
"location": "[parameters('location')]",
"sku": {
"name": "B1",
"tier": "Basic",
"size": "B1",
"family": "B",
"capacity": 1
},
"kind": "linux",
"properties": {
"perSiteScaling": false,
"reserved": true,
"targetWorkerCount": 0,
"targetWorkerSizeId": 0
}
},
{
"type": "Microsoft.Web/sites",
"apiVersion": "2021-03-01",
"name": "[variables('webAppPortalName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
],
"kind": "app",
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
"siteConfig": {
"linuxFxVersion": "[parameters('linuxFxVersion')]"
}
}
}
],
"outputs": {
"storageEndpoint": {
"type": "object",
"value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
}
}
}
Web 应用名称需要在 Azure 中是唯一的。 为了防止名称重复,变量 webAppPortalName
将更新 "webAppPortalName": "[concat(parameters('webAppName'), '-webapp')]"
为 "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
。
在定义末尾 Microsoft.Web/serverfarms
添加逗号以将资源定义与 Microsoft.Web/sites
定义分开。
在此新资源中需要注意几个重要功能。
它具有一个名为 dependsOn
的元素,该元素设置为应用服务计划。 此设置是必需的,因为在创建 Web 应用之前,应用服务计划需要存在。 该 dependsOn
元素告知资源管理器如何对资源进行部署排序。
该 serverFarmId
属性使用 resourceId 函数。 此函数获取资源的唯一标识符。 在这种情况下,它获取应用服务计划的唯一标识符。 Web 应用与一个特定的应用服务计划相关联。
部署模板
使用 Azure CLI 或 Azure PowerShell 部署模板。
如果尚未创建资源组,请参阅 “创建资源组”。 本示例假定已将 templateFile 变量设置为模板文件的路径,如 第一个教程所示。
New-AzResourceGroupDeployment `
-Name addwebapp `
-ResourceGroupName myResourceGroup `
-TemplateFile $templateFile `
-storagePrefix "store" `
-storageSKU Standard_LRS `
-webAppName demoapp
注释
如果部署失败,请使用 verbose
开关获取有关要创建的资源的信息。 使用 debug
开关获取调试的详细信息。
清理资源
若要继续学习下一篇教程,则无需删除资源组。
如果您此时停止操作,您可能希望删除资源组。
- 在 Azure 门户中,从左侧菜单中选择 资源组 。
- 在Filter for any field...文本字段中键入资源组名称。
- 选中 myResourceGroup 旁边的框,然后选择 myResourceGroup 或资源组名称。
- 在顶部菜单中选择“删除资源组”。
后续步骤
你了解了如何使用快速入门模板进行模板开发。 在下一教程中,您将向资源添加标签。