更新和删除部署堆栈

已完成

通过部署堆栈,可以使用标准流程和模板管理 Azure 资源。 随着应用程序发生更改,构成应用程序的资源也会更改。 添加和删除托管资源是生命周期管理的关键部分。

使用部署堆栈完成了存款应用程序的冲刺 1 和初始部署。 现在,需要准备冲刺 2 向应用程序引入新服务。 你想要详细了解如何更新并删除部署堆栈和托管资源。

在本单元中,你会了解如何将资源添加到 Bicep 文件以更新部署堆栈。 此外,你还会了解如何删除部署堆栈及其托管资源。

注意

本单元中显示的命令用于说明概念。 请暂时不要运行这些命令。 稍后你将练习在此处学到的知识。

更新部署堆栈

随着应用程序不断演变,其资源也会演变。 添加新服务和功能时,如何更新部署堆栈及其托管资源? 哪些情况需要我们更新部署堆栈? 添加新资源或更改现有托管资源的属性需要更新部署堆栈。

更新堆栈意味着更新 ARM JSON 模板、Bicep 文件或模板规格中定义的资源。对模板文件进行更改后,我们会使用相应的命令更新部署堆栈。

假设我们的存款应用程序需要添加新的 Azure SQL 数据库,并且我们希望部署堆栈以管理数据库。 为了添加新数据库,我们会更新 Bicep 文件以定义新的 Azure SQL Server 和数据库。

// Parameters
@description('The location for all resources.')
param location string = 'eastus'

@description('The name of the SQL database.')
param sqlDatabaseName string = 'sqldb-${uniqueString(resourceGroup().id)}'

@description('The password of the admin user.')
param sqlServerAdminUserName string

@description('The name of the admin user.')
@secure()
param sqlServerAdminPassword string

@description('The name of the SQL server.')
param sqlServerName string = 'sql-${uniqueString(resourceGroup().id)}'

@description('The name of the web application.')
param webApplicationName string = 'webapp-${uniqueString(resourceGroup().id)}'

// Variables
@description('The name of the app service plan.')
var appServicePlanName = 'plan-deposits'

// Resource - App Service Plan
resource appServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = {
  name: appServicePlanName
  location: location
  sku: {
    name: 'F1'
    capacity: 1
  }
}

// Resource - Web App
resource webApplication 'Microsoft.Web/sites@2023-12-01' = {
  name: webApplicationName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
  }
}

// Resource - SQL Server
resource sqlServer 'Microsoft.Sql/servers@2021-11-01' ={
  name: sqlServerName
  location: location
  properties: {
    administratorLogin: sqlServerAdminUserName
    administratorLoginPassword: sqlServerAdminPassword
  }
}

// Resource - SQL Database
resource sqlServerDatabase 'Microsoft.Sql/servers/databases@2021-11-01' = {
  parent: sqlServer
  name: sqlDatabaseName
  location: location
  sku: {
    name: 'Standard'
    tier: 'Standard'
  }
}

突出显示的代码为存款应用程序定义了新的 Azure SQL 数据库。 定义新资源后,需要更新部署堆栈。

要使用 Azure CLI 更新部署堆栈,请使用az stack group create命令。

az stack group create \
    --name stack-deposits \
    --resource-group rg-depositsApplication \
    --template-file ./main.bicep \
    --action-on-unmanage detachAll \
    --deny-settings-mode none

注意

Azure CLI 没有用于更新部署堆栈的专用命令。 使用 create 命令更新堆栈。

在堆栈上执行更新时,你会收到一条消息,指出堆栈已存在于当前订阅中。 如果action on unmanage参数的值发生更改,则会出现警告,提醒你注意新值。

显示 Azure CLI 命令行的屏幕截图,其中显示了部署堆栈已存在的警告。

要验证堆栈是否正在管理新资源,请使用az stack group show命令。

az stack group show \
    --resource-group rg-depositsApplication
    --name stack-deposits
{
  "actionOnUnmanage": {
    "managementGroups": "detach",
    "resourceGroups": "detach",
    "resources": "detach"
  },
  "bypassStackOutOfSyncError": null,
  "correlationId": ".",
  "debugSetting": null,
  "deletedResources": [],
  "denySettings": {
    "applyToChildScopes": false,
    "excludedActions": null,
    "excludedPrincipals": null,
    "mode": "none"
  },
  "deploymentId": "/subscriptions/././rg-depositsApplication/./Microsoft.Resources/deployments/stack-deposits",
  "deploymentScope": null,
  "description": null,
  "detachedResources": [],
  "duration": "PT2M53.2734284S",
  "error": null,
  "failedResources": [],
  "id": "/subscriptions/././rg-depositsApplication/./Microsoft.Resources/deploymentStacks/stack-deposits",
  "location": null,
  "name": "stack-deposits",
  "outputs": null,
  "parameters": {
    "sqlServerAdminPassword": {
      "reference": null,
      "type": "securestring",
      "value": ""
    },
    "sqlServerAdminUserName": {
      "reference": null,
      "type": "string",
      "value": "sqladmin"
    }
  },
  "parametersLink": null,
  "provisioningState": "succeeded",
  "resourceGroup": "rg-depositsApplication",
  "resources": [
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq/databases/sqldb-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././serverfarms/plan-deposits",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/./resourceGroups/rg-depositsApplication/././sites/webapp-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    }
  ],
  "systemData": {
    "createdAt": "2024-01-01T00:00:01.000000+00:00",
    "createdBy": "depositsapplication@contoso.com",
    "createdByType": "User",
    "lastModifiedAt": "2024-01-01T00:00:01.000000+00:00",
    "lastModifiedBy": "depositsapplication@contoso.com",
    "lastModifiedByType": "User"
  },
  "tags": {},
  "template": null,
  "templateLink": null,
  "type": "Microsoft.Resources/deploymentStacks"
}

突出显示的代码显示了由部署堆栈托管的新 Azure SQL Server 和 Azure SQL 数据库。

要使用 Azure PowerShell 更新部署堆栈,请使用Set-AzResourceGroupDeploymentStack命令。

Set-AzResourceGroupDeploymentStack `
    -Name stack-deposits `
    -ResourceGroupName rg-depositsApplication `
    -TemplateFile ./main.bicep `
    -ActionOnUnmanage DetachAll `
    -DenySettingsMode None

要验证堆栈是否正在管理新资源,请使用Get-AzResourceGroupDeploymentStack命令。

Get-AzResourceGroupDeploymentStack `
    -ResourceGroupName rg-depositsApplication `
    -Name stack-deposits
Id                            : /subscriptions/././rg-depositsApplication/././deploymentStacks/stack-deposits
Name                          : stack-deposits
ProvisioningState             : succeeded
resourcesCleanupAction        : detach
resourceGroupsCleanupAction   : detach
managementGroupsCleanupAction : detach
CorrelationId                 : .
DenySettingsMode              : none
CreationTime(UTC)             : 1/01/2024 0:00:01 AM
DeploymentId                  : /subscriptions/././rg-depositsApplication/././deployments/stack-deposits
Resources                     : /subscriptions/././rg-depositsApplication/././serverfarms/plan-deposits
                                /subscriptions/././rg-depositsApplication/././sites/webapp-eque2jlrboltq
                                /subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq
                                /subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq/databases/sqldb-eque2jlrboltq
Parameters                    : 
                                Name                      Type                       Value     
                                ========================  =========================  ==========
                                sqlServerAdminUserName    string                     "sqladmin"
                                sqlServerAdminPassword    securestring               ""

突出显示的代码显示了由部署堆栈托管的新 Azure SQL Server 和 Azure SQL 数据库。

在下一个模块中,你会致力于管理资源生命周期,包括添加、更新、分离和删除托管资源。

删除部署堆栈

可靠的资源清理是部署堆栈的关键功能。 删除部署堆栈时,还可以删除托管资源、资源组和管理组。 使用单个 API 调用无需了解依赖项。 与创建和更新部署堆栈一样,action on unmanage参数确定了 Azure 如何处理分离的资源。

注意

在本模块中,我们将使用 resrouce 组范围的部署堆栈。 在这种情况下,资源组不受堆栈管理。 action on unmanage参数的“删除全部”值不会对堆栈所在的资源组进行解码。 删除堆栈及其资源后,必须删除资源组。

要使用 Azure CLI 删除部署堆栈,请使用az stack group delete命令。 --action-on-unmanaged参数确定了 Azure 如何处理分离的资源、资源组和管理组。

az stack group delete \
    --name stack-deposits \
    --resource-group rg-depositsApplication \
    --action-on-unmanage deleteAll

要使用 Azure PowerShell 删除部署堆栈,请使用Remove-AzResourceGroupDeploymentStack命令。 -ActionOnUnmanage参数确定了 Azure 如何处理分离的资源、资源组和管理组。

Remove-AzResourceGroupDeploymentStack `
    -Name stack-deposits `
    -ResourceGroupName rg-depositsApplication `
    -ActionOnUnmanage -DeleteAll