將資源部署至多個範圍

已完成

有時您需要從一個部署內,將資源部署到階層中的多個層級。 以下是您可能想要執行此動作的一些情況:

  • 您需要跨兩個不同的資源群組部署資源。 例如,您可能想要在共用資源群組中建立網路安全性群組,也想要為您的應用程式在資源群組中部署虛擬機器的網路介面。
  • 您要使用範本來建立資源群組 (即訂用帳戶範圍資源),然後想要使用資源群組範圍部署,將儲存體帳戶和其他 Azure 資源部署到該資源群組。
  • 您要部署管理群組階層,而且也想要部署一些訂用帳戶,這些是租用戶範圍資源。

透過 Bicep,您可以使用 scope 關鍵字來建立可跨範圍運作的部署。

注意

本單元中的命令僅用於示範概念。 請先不要執行命令。 您很快就會在此練習所學到的內容。

指定模組的範圍

您可以使用 Bicep 模組,在不同於檔案中所指定 targetScope 的範圍部署一組資源。 以下是使用 subscriptiontargetScope 來部署的範例 Bicep 檔案,但會使用模組將某些資源部署至資源群組:

targetScope = 'subscription'

module networkModule 'modules/network.bicep' = {
  scope: resourceGroup('ToyNetworking')
  name: 'networkModule'
}

請注意,scope 屬性會使用 Bicep 函式來協助識別要設為目標的範圍。 上述範例會使用 resourceGroup() 函式,並指定要設為目標的資源群組名稱。 您也可以使用 subscription()managementGroup()tenant() 函式。 藉由在 Bicep 檔案上使用 targetScope 關鍵字,以及在模組上使用 scope 關鍵字,您可以為您的部署建立許多不同的範圍組合。

注意

其中一個例外狀況是,resourceGroupsubscriptiontargetScope 的 Bicep 檔案無法包含 managementGroupscope 的模組。

提示

如果使用訂用帳戶範圍的 Bicep 檔案來建立資源群組,您可以使用資源群組的符號名稱做為模組的 scope。 您會在下一個練習中了解如何做到這一點。

跨多個資源群組部署

範圍的常見用法是跨多個資源群組部署資源。 雖然您無法在大部分的 Azure 資源上設定 scope 屬性,但您可以使用模組來告訴 Bicep,應該將一組資源部署到不同的資源群組。

例如,您可能想要建立單一的 Bicep 檔案集,將虛擬網路及其相關聯的資源部署至名為 ToyNetworking的共用資源群組,然後將網路介面部署至不同的資源群組。 以下是 Bicep 檔案看起來的樣子:

module networkModule 'modules/network.bicep' = {
  scope: resourceGroup('ToyNetworking')
  name: 'networkModule'
}

resource networkInterface 'Microsoft.Network/networkInterfaces@2020-11-01' = {
  name: 'production-nic'
  location: resourceGroup().location
  properties: {
    ipConfigurations: [
      {
        name: 'toy-subnet-ip-configuration'
        properties: {
          subnet: {
            id: networkModule.outputs.subnetResourceId
          }
        }
      }
    ]
  }
}

請注意,要部署至 ToyNetworking 資源群組的資源是在模組中定義,而 subnetResourceId 輸出則用於網路介面的資源定義中。

在部署此檔案之後,您可以將另一個名為 ProjectTeddybear 的資源群組設為目標,如下所示:

az deployment group create --resource-group ProjectTeddybear ...
New-AzResourceGroupDeployment -ResourceGroupName ProjectTeddybear ...

雖然部署是以 ProjectTeddybear 資源群組為目標,但虛擬網路資源會部署到 ToyNetworking 資源群組。 網路介面會部署至 ProjectTeddybear 資源群組。

您甚至可以將訂用帳戶識別碼併入 resourceGroup 範圍中,以在另一個訂用帳戶中部署資源群組:

module networkModule 'modules/network.bicep' = {
  scope: resourceGroup('f0750bbe-ea75-4ae5-b24d-a92ca601da2c', 'ToyNetworking')
  name: 'networkModule'
}

同樣地,您可以使用 subscription() 範圍函式,在訂用帳戶範圍內跨多個訂用帳戶部署資源,而且您可以使用 managementGroup() 範圍函式,跨多個管理群組部署資源。 不過,您無法跨多個租用戶部署它們。

指定單一資源的範圍

您可以在 scope 其他幾個特定資源類型上使用 關鍵詞,而不只是模組。 延伸模組資源會使用 scope 關鍵字來指定要套用它們的資源。 此外,租用戶範圍資源可以使用 scope 關鍵字,讓您可以從任何範本部署它們。

例如,您可能使用 Bicep 檔案來建立管理群組階層,如下列範例所示:

targetScope = 'managementGroup'

resource parentManagementGroup 'Microsoft.Management/managementGroups@2020-05-01' = {
  scope: tenant()
  name: 'NonProduction'
  properties: {
    displayName: 'Non-production'
  }
}

resource childManagementGroup 'Microsoft.Management/managementGroups@2020-05-01' = {
  scope: tenant()
  name: 'SecretRND'
  properties: {
    displayName: 'Secret R&D Projects'
    details: {
      parent: {
        id: parentManagementGroup.id
      }
    }
  }
}

請注意,此範例會在範本檔案中使用 targetScope = 'managementGroup',但接著會在 tenant() 範圍內部署管理群組。

注意

上述範例說明如何使用 Bicep 來建立管理群組階層。 NonProduction 管理群組將會是根管理群組的子系,而 SecretRND 管理群組將會是 NonProduction 管理群組的子系。

建立管理群組和訂用帳戶階層

現在,您知道如何在各種範圍部署許多不同的資源,並知道如何使用 Bicep 模組和 scope 關鍵字,來部署資源的組合。 讓我們將所有新知識套用到上述範例中的擴充管理群組階層。 現在,階層也會包含「訂用帳戶別名」,這是建立新 Azure 訂用帳戶的租用戶範圍資源:

resource subscription 'Microsoft.Subscription/aliases@2020-09-01' = {
  scope: tenant()
  name: subscriptionAliasName
  properties: {
    // ...
  }
}

注意

當您建立訂用帳號別名時,也會指定一些其他屬性,例如計費範圍。 為了清楚起見,我們省略了它們。

然後,您可以將訂用帳戶與管理群組建立關聯,這需要您部署稱為 Microsoft.Management/managementGroups/subscriptions 的資源類型。 由於此資源的運作方式,您可以在模組中進行宣告。 例如,以下是名為 modules/mg-subscription-association.bicep 的檔案:

targetScope = 'tenant'

@description('The name of the management group that should contain the subscription.')
param managementGroupName string

@description('The subscription ID to place into the management group.')
param subscriptionId string

resource managementGroup 'Microsoft.Management/managementGroups@2021-04-01' existing = {
  name: managementGroupName
}

resource subscriptionAssociation 'Microsoft.Management/managementGroups/subscriptions@2021-04-01' = {
  parent: managementGroup
  name: subscriptionId
}

請注意,管理群組是透過 existing 關鍵字參考的。

然後,主要 Bicep 檔案可以藉由包含模組來建立關聯。 以下是整個 Bicep 檔案:

targetScope = 'managementGroup'

@description('The name of the subscription alias to deploy.')
param subscriptionAliasName string

resource parentManagementGroup 'Microsoft.Management/managementGroups@2020-05-01' = {
  scope: tenant()
  name: 'NonProduction'
  properties: {
    displayName: 'Non-production'
  }
}

resource childManagementGroup 'Microsoft.Management/managementGroups@2020-05-01' = {
  scope: tenant()
  name: 'SecretRND'
  properties: {
    displayName: 'Secret R&D Projects'
    details: {
      parent: {
        id: parentManagementGroup.id
      }
    }
  }
}

resource subscription 'Microsoft.Subscription/aliases@2020-09-01' = {
  scope: tenant()
  name: subscriptionAliasName
  properties: {
    // ...
  }
}

module subscriptionAssociation 'modules/mg-subscription-association.bicep' = {
  name: 'subscriptionAssociation'
  scope: tenant()
  params: {
    managementGroupName: childManagementGroup.name
    subscriptionId: subscription.properties.subscriptionId
  }
}

如您所見,您可以一起使用所有範圍和 Bicep 語言功能,來建立整個 Azure 基礎結構的複雜部署。