Share via


快速入門:使用 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。 App Service 方案名稱在訂用帳戶中的資源群組內必須是唯一的。
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 屬性需要資源識別碼。 因為受控資源群組是在部署期間建立的,因此屬性沒有可用的資源識別碼。

檢視結果

在部署服務類別目錄受控應用程式之後,您會有兩個新的資源群組。 一個資源群組包含受控應用程式。 另一個資源群組包含已部署的受控資源。 在此範例中,App Service、App Service 方案和儲存體帳戶。

受控應用程式

部署完成後,您可以檢查受控應用程式的狀態。

執行下列命令來檢查受控應用程式的狀態。

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 的資源群組。

下一步