在上一篇教程中,你已学习如何使用参数文件来部署 Bicep 文件。 本教程将介绍如何使用 Bicep 模块来封装原始资源声明的复杂细节。 可以在解决方案内部共享和重用这些模块。 完成该过程需要大约 12 分钟。
注意
本文包含 Bicep 示例。 Bicep 目前为预览版。 有关详细信息,请参阅 Bicep 项目。
先决条件
建议完成有关参数文件的教程,但这不是一项要求。
必须有包含 Bicep 扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。 有关详细信息,请参阅 Bicep 工具。
查看 Bicep 文件
在上一篇教程结束时,Bicep 文件包含以下内容:
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
param appServicePlanName string = 'exampleplan'
@minLength(2)
@description('Base name of the resource such as web app name and app service plan.')
param webAppName string
@description('The Runtime stack of current web app.')
param linuxFxVersion string = 'php|7.0'
param resourceTags object = {
Environment: 'Dev'
Project: 'Tutorial'
}
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
var webAppPortalName = '${webAppName}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: uniqueStorageName
location: location
tags: resourceTags
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
resource appPlan 'Microsoft.Web/serverfarms@2016-09-01' = {
name: appServicePlanName
location: location
tags: resourceTags
sku: {
name: 'B1'
tier: 'Basic'
size: 'B1'
family: 'B'
capacity: 1
}
kind: 'linux'
properties: {
perSiteScaling: false
reserved: true
targetWorkerCount: 0
targetWorkerSizeId: 0
}
}
resource site 'Microsoft.Web/sites@2016-08-01' = {
name: webAppPortalName
location: location
tags: resourceTags
kind: 'app'
properties: {
serverFarmId: appPlan.id
siteConfig: {
linuxFxVersion: linuxFxVersion
}
}
}
output storageEndpoint object = stg.properties.primaryEndpoints
此 Bicep 文件可正常运行。 但对于更大的解决方案,需要将 Bicep 文件分解成多个相关模块,以便可以共享和重用这些模块。 当前的 Bicep 文件将部署存储帐户、应用服务计划和网站。 让我们将存储帐户隔离到模块中。
创建 Bicep 模块
每个 Bicep 文件都可用作模块,因此在定义模块方面没有特定的语法。 创建包含以下内容的 storage.bicep 文件:
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string
param resourceTags object
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: uniqueStorageName
location: location
tags: resourceTags
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
output storageEndpoint object = stg.properties.primaryEndpoints
此模块包含存储帐户资源以及相关的参数和变量。 已删除 location 参数和 resourceTags 参数的值。 这些值将从 Bicep 主文件传递。
使用 Bicep 模块
将现有 azuredeploy.bicep 中的存储帐户资源定义替换为以下 Bicep 内容:
module stg './storage.bicep' = {
name: 'storageDeploy'
params: {
storagePrefix: storagePrefix
location: location
resourceTags: resourceTags
}
}
- 模块:关键字。
- 符号名称 (stg):模块的标识符。
- 模块文件:在此示例中,模块的路径是使用相对路径 (./storage.bicep) 指定的。 必须使用正斜杠 (/) 目录分隔符来指定 Bicep 中的所有路径,以确保跨平台编译时的一致性。 不支持 Windows 反斜杠()字符。
若要检索存储终结点,请将 azuredeploy.bicep 中的输出更新为以下 Bicep:
output storageEndpoint object = stg.outputs.storageEndpoint
完成的 azuredeploy.bicep 包含以下内容:
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
param appServicePlanName string = 'exampleplan'
param webAppName string {
minLength: 2
metadata: {
description: 'Base name of the resource such as web app name and app service plan '
}
}
param linuxFxVersion string {
metadata: {
description: 'The Runtime stack of current web app'
}
default: 'php|7.0'
}
param resourceTags object = {
Environment: 'Dev'
Project: 'Tutorial'
}
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
var webAppPortalName = '${webAppName}${uniqueString(resourceGroup().id)}'
module stg './storage.bicep' = {
name: 'storageDeploy'
params: {
storagePrefix: storagePrefix
location: location
resourceTags: resourceTags
}
}
resource appPlan 'Microsoft.Web/serverfarms@2016-09-01' = {
name: appServicePlanName
location: location
tags: resourceTags
sku: {
name: 'B1'
tier: 'Basic'
size: 'B1'
family: 'B'
capacity: 1
}
kind: 'linux'
properties: {
perSiteScaling: false
reserved: true
targetWorkerCount: 0
targetWorkerSizeId: 0
}
}
resource site 'Microsoft.Web/sites@2016-08-01' = {
name: webAppPortalName
location: location
tags: resourceTags
kind: 'app'
properties: {
serverFarmId: appPlan.id
siteConfig: {
linuxFxVersion: linuxFxVersion
}
}
}
output storageEndpoint object = stg.outputs.storageEndpoint
部署模板
使用 Azure CLI 或 Azure PowerShell 来部署模板。
如果尚未创建资源组,请参阅创建资源组。 此示例假定你已按第一篇教程所述将 bicepFile
变量设置为 Bicep 文件的路径。
若要运行此部署 cmdlet,你必须具有 Azure PowerShell 的最新版本。
New-AzResourceGroupDeployment `
-Name addmodule `
-ResourceGroupName myResourceGroup `
-TemplateFile $bicepFile `
-storagePrefix "store" `
-storageSKU Standard_LRS `
-webAppName demoapp
注意
如果部署失败,请使用 verbose
开关获取有关正在创建的资源的信息。 使用 debug
开关获取调试的详细信息。
验证部署
可以通过在 Azure 门户中浏览资源组来验证部署。
- 登录 Azure 门户。
- 在左侧菜单中选择“资源组”。
- 你会看到在本教程中部署的新资源组。
- 选择该资源组,查看部署的资源。 请注意,这些资源与我们在模板文件中指定的值匹配。
清理资源
- 在 Azure 门户上的左侧菜单中选择“资源组” 。
- 在“按名称筛选”字段中输入资源组名称。 如果已完成此系列,则需删除三个资源组:myResourceGroup、myResourceGroupDev 和 myResourceGroupProd 。
- 选择资源组名称。
- 在顶部菜单中选择“删除资源组”。
后续步骤
恭喜!你已完成本简介文章,知道如何将 Bicep 文件部署到 Azure 了。 如果你有任何意见和建议,请在反馈部分告知我们。 谢谢!
下一个教程系列将详细介绍如何部署模板。