练习 - 更新和删除部署堆栈
你正处于新存款应用程序开发的第二个冲刺阶段。 你想通过在 Bicep 文件中定义 Azure SQL 数据库来测试更新部署堆栈。 你想验证堆栈是否正在管理新定义的资源。
在本练习中,你将更新先前创建的 Bicep 文件以包含新资源。 你还要更新和删除管理资源的部署堆栈。
- 更新 Bicep 文件以包含 Azure SQL 数据库。
 - 更新部署堆栈以部署新数据库。
 - 验证部署堆栈的托管资源。
 - 删除部署堆栈和托管资源。
 - 验证是否已删除部署堆栈和托管资源。
 
更新 Bicep 文件以包含 Azure SQL 数据库
打开 Visual Studio Code 中的 main.bicep 文件。
将突出显示的代码添加到文件的 parameters 部分:
// 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 - 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' } }保存对文件所做的更改。
更新部署堆栈
更新 Bicep 文件后,我们希望更新部署堆栈,以便将新的 Azure SQL 数据库作为托管资源部署到环境中。
若要更新部署堆栈,请从 Visual Studio Code 中的终端运行以下命令。
az stack group create \ --name stack-deposits \ --resource-group rg-depositsApplication \ --template-file ./main.bicep \ --action-on-unmanage deleteAll \ --deny-settings-mode none你会收到一条消息,指出该堆栈已存在于当前订阅中。 如果 action on unmanage 参数的值发生更改,则会出现警告,提醒你注意新值。 依次按
y、Enter 键。
              
              
            
系统会提示你输入
sqlServerAdminUserName的值。 为 SQL 服务器管理员创建一个名称,然后按 Enter。
              
              
            
系统会提示你输入
sqlServerAdminPassword的值。 为 SQL 服务器管理员创建一个复杂密码,然后按 Enter。
              
              
            
等待更新操作完成,然后继续执行下一个任务。
更新 Bicep 文件后,我们希望更新部署堆栈,以便将新的 Azure SQL 数据库作为托管资源部署到环境中。
若要更新部署堆栈,请从 Visual Studio Code 中的终端运行以下命令。
Set-AzResourceGroupDeploymentStack ` -Name stack-deposits ` -ResourceGroupName rg-depositsApplication ` -TemplateFile ./main.bicep ` -ActionOnUnmanage DeleteAll ` -DenySettingsMode none系统会提示你输入
sqlServerAdminUserName的值。 为 SQL 服务器管理员创建一个名称,然后按 Enter。
              
              
            
系统会提示你输入
sqlServerAdminPassword的值。 为 SQL 服务器管理员创建一个复杂密码,然后按 Enter。
              
              
            
等待更新操作完成,然后继续执行下一个任务。
验证对部署堆栈和托管资源的更新
更新完成后,我们希望验证部署堆栈是否正在管理 Azure SQL 数据库。 若要查看部署堆栈的配置,请从 Visual Studio Code 中的终端运行以下命令。
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"
}
请注意输出的 resources 部分。 现在,我们会看到 SQL 服务器和 SQL 数据库已作为托管资源列出。
更新完成后,我们希望验证部署堆栈是否正在管理 Azure SQL 数据库。 若要查看部署堆栈的配置,请从 Visual Studio Code 中的终端运行以下命令。
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               ""
请注意输出的 resources 部分。 现在,我们会看到 SQL 服务器和 SQL 数据库已作为托管资源列出。
删除部署堆栈
若要删除部署堆栈及其托管资源,请从 Visual Studio Code 中的终端运行以下命令。
az stack group delete \ --name stack-deposits \ --resource-group rg-depositsApplication \ --action-on-unmanage deleteAll系统会要求你确认是否要删除堆栈和指定的资源。
              
              
            
删除操作完成后,打开 Azure 门户并验证部署堆栈及其资源是否已删除。
若要删除这些练习中使用的资源组,请从 Visual Studio Code 中的终端运行以下命令。
az group delete \ -name rg-depositsApplication系统会要求你确认是否要删除资源组。 依次按 Y、Enter 键。
              
              
            
若要删除部署堆栈及其托管资源,请从 Visual Studio Code 中的终端运行以下命令。
Remove-AzResourceGroupDeploymentStack ` -Name stack-deposits ` -ResourceGroupName rg-depositsApplication ` -ActionOnUnmanage DeleteAll系统会要求你确认是否要删除堆栈和指定的资源。
              
              
            
删除操作完成后,打开 Azure 门户并验证部署堆栈及其资源是否已删除。
若要删除这些练习中使用的资源组,请从 Visual Studio Code 中的终端运行以下命令。
Remove-AzResourceGroup ` -Name rg-depositsApplication系统会要求你确认是否要删除资源组。 依次按 Y、Enter 键。