你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
快速入门:使用 Bicep 创建和部署部署堆栈
本快速入门介绍如何创建部署堆栈。
先决条件
- 具有活动订阅的 Azure 帐户。 免费创建帐户。
- Azure PowerShell 12.0.0 版或更高版本或 Azure CLI 2.61.0 版或更高版本。
- 带有 Bicep 扩展的 Visual Studio Code。
创建 Bicep 文件
创建 Bicep 文件以创建存储帐户和虚拟网络。
param resourceGroupLocation string = resourceGroup().location
param storageAccountName string = 'store${uniqueString(resourceGroup().id)}'
param vnetName string = 'vnet${uniqueString(resourceGroup().id)}'
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
name: storageAccountName
location: resourceGroupLocation
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' = {
name: vnetName
location: resourceGroupLocation
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
subnets: [
{
name: 'Subnet-1'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
{
name: 'Subnet-2'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
]
}
}
将 Bicep 文件另存为 main.bicep。
创建部署堆栈
在本快速入门中,你将在资源组范围内创建部署堆栈。 也可以在订阅范围或管理组范围内创建部署堆栈。 有关详细信息,请参阅创建部署堆栈。
az group create \
--name 'demoRg' \
--location 'centralus'
az stack group create \
--name demoStack \
--resource-group 'demoRg' \
--template-file './main.bicep' \
--action-on-unmanage 'detachAll' \
--deny-settings-mode 'none'
有关 action-on-unmanage
和 deny-setting-mode
的详细信息,请参阅部署堆栈。
验证部署
若要在资源组级别列出已部署的部署堆栈,请执行以下操作:
az stack group show \
--resource-group 'demoRg' \
--name 'demoStack'
输出显示两个托管资源 - 一个存储帐户和一个虚拟网络:
{
"actionOnUnmanage": {
"managementGroups": "detach",
"resourceGroups": "detach",
"resources": "detach"
},
"debugSetting": null,
"deletedResources": [],
"denySettings": {
"applyToChildScopes": false,
"excludedActions": null,
"excludedPrincipals": null,
"mode": "none"
},
"deploymentId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Resources/deployments/demoStack-24051714epybc",
"deploymentScope": null,
"description": null,
"detachedResources": [],
"duration": "PT32.5330364S",
"error": null,
"failedResources": [],
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Resources/deploymentStacks/demoStack",
"location": null,
"name": "demoStack",
"outputs": null,
"parameters": {},
"parametersLink": null,
"provisioningState": "succeeded",
"resourceGroup": "demoRg",
"resources": [
{
"denyStatus": "none",
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Network/virtualNetworks/vnetthmimleef5fwk",
"resourceGroup": "demoRg",
"status": "managed"
},
{
"denyStatus": "none",
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Storage/storageAccounts/storethmimleef5fwk",
"resourceGroup": "demoRg",
"status": "managed"
}
],
"systemData": {
"createdAt": "2024-05-17T14:50:18.382948+00:00",
"createdBy": "johndoe@contoso.com",
"createdByType": "User",
"lastModifiedAt": "2024-05-17T14:50:18.382948+00:00",
"lastModifiedBy": "johndoe@contoso.com",
"lastModifiedByType": "User"
},
"tags": {},
"template": null,
"templateLink": null,
"type": "Microsoft.Resources/deploymentStacks"
}
还可以通过列出部署堆栈中的托管资源来验证部署:
az stack group show \
--name 'demoStack' \
--resource-group 'demoRg' \
--output 'json'
输出类似于:
{
"actionOnUnmanage": {
"managementGroups": "detach",
"resourceGroups": "detach",
"resources": "detach"
},
"debugSetting": null,
"deletedResources": [],
"denySettings": {
"applyToChildScopes": false,
"excludedActions": null,
"excludedPrincipals": null,
"mode": "none"
},
"deploymentId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Resources/deployments/demoStack-24051714epybc",
"deploymentScope": null,
"description": null,
"detachedResources": [],
"duration": "PT32.5330364S",
"error": null,
"failedResources": [],
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Resources/deploymentStacks/demoStack",
"location": null,
"name": "demoStack",
"outputs": null,
"parameters": {},
"parametersLink": null,
"provisioningState": "succeeded",
"resourceGroup": "demoRg",
"resources": [
{
"denyStatus": "none",
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Network/virtualNetworks/vnetthmimleef5fwk",
"resourceGroup": "demoRg",
"status": "managed"
},
{
"denyStatus": "none",
"id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Storage/storageAccounts/storethmimleef5fwk",
"resourceGroup": "demoRg",
"status": "managed"
}
],
"systemData": {
"createdAt": "2024-05-17T14:50:18.382948+00:00",
"createdBy": "johndoe@contoso.com",
"createdByType": "User",
"lastModifiedAt": "2024-05-17T14:50:18.382948+00:00",
"lastModifiedBy": "johndoe@contoso.com",
"lastModifiedByType": "User"
},
"tags": {},
"template": null,
"templateLink": null,
"type": "Microsoft.Resources/deploymentStacks"
}
创建堆栈后,可以通过 Azure 门户访问和查看堆栈本身以及与堆栈关联的托管资源。 导航到已部署堆栈的资源组,你可以访问所有相关信息和设置。
更新部署堆栈
若要更新部署堆栈,可以修改基础 Bicep 文件并重新运行 create deployment stack 命令。
编辑“main.bicep”,将 sku 名称从 Standard_GRS
更改为 Standard_LRS
:
运行以下命令:
az stack group create \
--name 'demoStack' \
--resource-group 'demoRg' \
--template-file './main.bicep' \
--action-on-unmanage 'detachAll' \
--deny-settings-mode 'none'
从 Azure 门户中,检查存储帐户的属性以确认更改。
使用相同的方法,可以将资源添加到部署堆栈,或者从部署堆栈中删除托管资源。 有关详细信息,请参阅将资源添加到部署堆栈和从部署堆栈中删除托管资源。
删除部署堆栈
若要删除部署堆栈和托管资源,请执行以下操作:
az stack group delete \
--name 'demoStack' \
--resource-group 'demoRg' \
--action-on-unmanage 'deleteAll'
删除部署堆栈,但保留托管资源:
az stack group delete \
--name 'demoStack' \
--resource-group 'demoRg' \
--action-on-unmanage 'detachAll'
有关详细信息,请参阅删除部署堆栈。
remove 命令专门删除托管资源和托管资源组。 你仍负责删除不由部署堆栈管理的资源组。
清理资源
删除非托管资源组。
az group delete \
--name 'demoRg'