你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Bicep 部署 Azure 托管应用程序定义

本快速入门介绍如何使用 Bicep 从服务目录部署 Azure 托管应用程序定义。 你的服务目录中的定义可供组织成员使用。

若要从服务目录部署托管应用程序定义,请执行以下任务:

  • 使用 Bicep 开发一个模板来部署托管应用程序定义。
  • 为部署创建参数文件。
  • 从服务目录部署托管应用程序定义。

先决条件

若要完成本文中的任务,需要准备好以下项:

获取托管应用程序定义

若要使用 Azure PowerShell 获取托管应用程序的定义,请运行以下命令。

在 Visual Studio Code 中,打开新的 PowerShell 终端并登录到 Azure 订阅。

Connect-AzAccount

此命令会打开默认浏览器,并提示登录到 Azure。 有关详细信息,请转到使用 Azure PowerShell 登录

从 Azure PowerShell 获取托管应用程序的定义。 在此示例中,使用部署托管应用程序定义时创建的资源组名称 bicepDefinitionRG

Get-AzManagedApplicationDefinition -ResourceGroupName bicepDefinitionRG

Get-AzManagedApplicationDefinition 列出了指定资源组中的所有可用定义,例如 sampleBicepManagedApplication

以下命令分析输出以仅显示定义名称和资源组名称。 你在部署托管应用程序时会使用这些名称。

Get-AzManagedApplicationDefinition -ResourceGroupName bicepDefinitionRG | Select-Object -Property Name, ResourceGroupName

创建 Bicep 文件

打开 Visual Studio Code 并创建名为 deployServiceCatalog.bicep 的文件。 将以下代码复制并粘贴到文件中并保存。

@description('Region where the resources are deployed.')
param location string = resourceGroup().location

@description('Resource group name where the definition is stored.')
param definitionRG string

@description('Name of the service catalog definition.')
param definitionName string

// Parameters for the managed application's resource deployment
@description('Name of the managed application.')
param managedAppName string

@description('Name for the managed resource group.')
param mrgName string

@maxLength(40)
@description('Service plan name with maximum 40 alphanumeric characters and hyphens. Must be unique within a resource group in your subscription.')
param appServicePlanName string

@maxLength(47)
@description('Globally unique across Azure. Maximum of 47 alphanumeric characters or hyphens.')
param appServiceNamePrefix string

@maxLength(11)
@description('Use only lowercase letters and numbers and a maximum of 11 characters.')
param storageAccountNamePrefix string

@allowed([
  'Premium_LRS'
  'Standard_LRS'
  'Standard_GRS'
])
@description('The options are Premium_LRS, Standard_LRS, or Standard_GRS')
param storageAccountType string

@description('Resource ID for the managed application definition.')
var appResourceId = resourceId('${definitionRG}', 'Microsoft.Solutions/applicationdefinitions', '${definitionName}')

@description('Creates the path for the managed resource group. The resource group is created during deployment.')
var mrgId = '${subscription().id}/resourceGroups/${mrgName}'

resource bicepServiceCatalogApp 'Microsoft.Solutions/applications@2021-07-01' = {
  name: managedAppName
  kind: 'ServiceCatalog'
  location: location
  properties: {
    applicationDefinitionId: appResourceId
    managedResourceGroupId: mrgId
    parameters: {
      appServicePlanName: {
        value: appServicePlanName
      }
      appServiceNamePrefix: {
        value: appServiceNamePrefix
      }
      storageAccountNamePrefix: {
        value: storageAccountNamePrefix
      }
      storageAccountType: {
        value: storageAccountType
      }
    }
  }
}

有关资源类型的详细信息,请转到 Microsoft.Solutions/applications

创建参数文件

打开 Visual Studio Code 并创建名为 deployServiceCatalog.parameters.json 的参数文件。 将以下代码复制并粘贴到文件中并保存。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "definitionName": {
      "value": "sampleBicepManagedApplication"
    },
    "definitionRG": {
      "value": "bicepDefinitionRG"
    },
    "managedAppName": {
      "value": "sampleBicepManagedApp"
    },
    "mrgName": {
      "value": "<placeholder for managed resource group name>"
    },
    "appServicePlanName": {
      "value": "demoAppServicePlan"
    },
    "appServiceNamePrefix": {
      "value": "demoApp"
    },
    "storageAccountNamePrefix": {
      "value": "demostg1234"
    },
    "storageAccountType": {
      "value": "Standard_LRS"
    }
  }
}

需要提供几个参数来部署托管应用程序:

参数
definitionName 服务目录定义的名称。 此例使用 sampleBicepManagedApplication
definitionRG 存储定义的资源组名称。 此示例使用 bicepDefinitionRG
managedAppName 部署的托管应用程序的名称。 此示例使用 sampleBicepManagedApp
mrgName 包含应用程序已部署资源的托管资源组的唯一名称。 资源组是在部署托管应用程序时创建的。 若要创建托管资源组名称,可以运行此参数列表后面的命令。
appServicePlanName 创建计划名称。 最多 40 个字母数字字符和连字符。 例如 demoAppServicePlan。 应用服务计划名称在订阅中的资源组中必须是唯一的。
appServiceNamePrefix 为计划名称创建前缀。 最多 47 个字母数字字符或连字符。 例如 demoApp。 在部署期间,前缀与唯一字符串连接,以创建在 Azure 中全局唯一的名称。
storageAccountNamePrefix 只能使用小写字母和数字,最多 11 个字符。 例如 demostg1234。 在部署期间,前缀与唯一字符串连接,以创建在 Azure 中全局唯一的名称。
storageAccountType 选项包括 Premium_LRS、Standard_LRS 和 Standard_GRS。

可以运行以下命令来为托管资源组创建名称。

$mrgprefix = 'mrg-sampleBicepManagedApplication-'
$mrgtimestamp = Get-Date -UFormat "%Y%m%d%H%M%S"
$mrgname = $mrgprefix + $mrgtimestamp
$mrgname

$mrgprefix$mrgtimestamp 变量连接并存储在变量 $mrgname 中。 变量的值采用 mrg-sampleBicepManagedApplication-20230512103059 格式。 在部署托管应用程序时使用 $mrgname 变量的值。

部署托管应用程序

使用 Azure PowerShell 或 Azure CLI 创建资源组并部署托管应用程序。

New-AzResourceGroup -Name bicepAppRG -Location westus3

New-AzResourceGroupDeployment `
  -ResourceGroupName bicepAppRG `
  -TemplateFile deployServiceCatalog.bicep `
  -TemplateParameterFile deployServiceCatalog.parameters.json

部署可能会显示 Bicep linter 警告,指出属性 managedResourceGroupId 需要资源 ID。 由于托管资源组是在部署期间创建的,因此没有可用于该属性的资源 ID。

查看结果

部署服务目录托管应用程序后,可获得两个新资源组。 一个资源组包含托管应用程序。 另一个资源组包含已部署的受管理资源。 在此示例中,即应用服务、应用服务计划和存储帐户。

托管应用程序

部署完成后,可以检查托管应用程序的状态。

运行以下命令以检查托管应用程序的状态。

Get-AzManagedApplication -Name sampleBicepManagedApp -ResourceGroupName bicepAppRG

展开属性,以便更轻松地阅读 Properties 信息。

Get-AzManagedApplication -Name sampleBicepManagedApp -ResourceGroupName bicepAppRG | Select-Object -ExpandProperty Properties

托管的资源

可以查看部署到受管理资源组的资源。

若要显示受管理资源组的资源,请运行以下命令。 你在创建参数时创建了 $mrgname 变量。

Get-AzResource -ResourceGroupName $mrgname

若要显示受管理资源组的所有角色分配。

Get-AzRoleAssignment -ResourceGroupName $mrgname

在快速入门文章中创建的托管应用程序定义使用了具有所有者角色分配的组。 可以使用以下命令查看组。

Get-AzRoleAssignment -ResourceGroupName $mrgname -RoleDefinitionName Owner

还可以列出受管理资源组的拒绝分配。

Get-AzDenyAssignment -ResourceGroupName $mrgname

清理资源

处理完托管应用程序后,可以删除资源组,这将删除你创建的所有资源。 例如,你创建了资源组 bicepAppRG 和前缀为 mrg-bicepManagedApplication 的受管理资源组。

删除 bicepAppRG 资源组时,将删除托管应用程序、受管理资源组和所有 Azure 资源。

命令会提示你确认删除资源组。

Remove-AzResourceGroup -Name bicepAppRG

如果想要删除托管应用程序定义,请删除创建的名为 packageStorageRGbicepDefinitionRG 的资源组。

后续步骤