在 Bicep 中設定延伸模組資源的範圍

延伸模組資源是會修改其他資源的資源。 例如,您可以將角色指派給資源。 角色指派是一種延伸模組資源類型。

如需完整的延伸模組資源類型清單,請參閱擴充其他資源功能的資源類型 (部分機器翻譯)。

此文章說明如何在使用 Bicep 檔案部署時,設定延伸模組資源類型的範圍。 其描述在套用到資源時,適用於延伸模組資源的範圍屬性。

注意

範圍屬性僅適用於延伸模組資源類型。 若要針對非延伸模組類型的資源類型指定不同的範圍,請使用模組 (部分機器翻譯)。

訓練資源

如果您比較想要透過逐步指導來了解延伸模組資源,請參閱使用 Bicep 部署子資源和延伸模組資源

在部署範圍內套用

若要在目標部署範圍內套用延伸模組資源類型,請將資源新增至範本,就像使用任何其他資源類型一樣。 可用的範圍包括資源群組 (部分機器翻譯)、訂用帳戶 (部分機器翻譯)、管理群組 (部分機器翻譯) 和租用戶 (部分機器翻譯)。 部署範圍必須支援資源類型。

部署至資源群組時,下列範本會將鎖定新增至該資源群組。

resource createRgLock 'Microsoft.Authorization/locks@2016-09-01' = {
  name: 'rgLock'
  properties: {
    level: 'CanNotDelete'
    notes: 'Resource group should not be deleted.'
  }
}

下一個範例會將角色指派給其部署所在的訂用帳戶。

targetScope = 'subscription'

@description('The principal to assign the role to')
param principalId string

@allowed([
  'Owner'
  'Contributor'
  'Reader'
])
@description('Built-in role to assign')
param builtInRoleType string

var role = {
  Owner: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635'
  Contributor: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'
  Reader: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7'
}

resource roleAssignSub 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  name: guid(subscription().id, principalId, role[builtInRoleType])
  properties: {
    roleDefinitionId: role[builtInRoleType]
    principalId: principalId
  }
}

套用到資源

若要將延伸模組資源套用到資源,請使用 scope 屬性。 在 scope 屬性中,參考您要新增延伸模組的資源。 您可以透過提供資源的符號名稱來參考資源。 範圍屬性是延伸模組資源類型的根屬性。

下列範例會建立儲存體帳戶,並將角色套用到該帳戶。

@description('The principal to assign the role to')
param principalId string

@allowed([
  'Owner'
  'Contributor'
  'Reader'
])
@description('Built-in role to assign')
param builtInRoleType string

param location string = resourceGroup().location

var role = {
  Owner: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635'
  Contributor: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'
  Reader: '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7'
}
var uniqueStorageName = 'storage${uniqueString(resourceGroup().id)}'

resource demoStorageAcct 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'Storage'
  properties: {}
}

resource roleAssignStorage 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  name: guid(demoStorageAcct.id, principalId, role[builtInRoleType])
  properties: {
    roleDefinitionId: role[builtInRoleType]
    principalId: principalId
  }
  scope: demoStorageAcct
}

您可以將延伸模組資源套用到現有資源。 下列範例會將鎖定新增至現有的儲存體帳戶。

resource demoStorageAcct 'Microsoft.Storage/storageAccounts@2021-04-01' existing = {
  name: 'examplestore'
}

resource createStorageLock 'Microsoft.Authorization/locks@2016-09-01' = {
  name: 'storeLock'
  scope: demoStorageAcct
  properties: {
    level: 'CanNotDelete'
    notes: 'Storage account should not be deleted.'
  }
}

將目標設定為與部署目標範圍不同的範圍時,相同的需求也會套用到延伸模組資源作為其他資源。 若要了解如何部署至一個以上的範圍,請參閱:

resourceGroup 和訂用帳戶屬性只能在模組上使用。 個別資源上不允許這些屬性。 如果您想要使用範圍設定將延伸模組資源部署至不同資源群組中的資源,請使用模組。

下列範例示範如何在位於不同資源群組的儲存體帳戶上套用鎖定。

  • main.bicep

    param resourceGroup2Name string
    param storageAccountName string
    
    module applyStoreLock './storageLock.bicep' = {
      name: 'addStorageLock'
      scope: resourceGroup(resourceGroup2Name)
      params: {
        storageAccountName: storageAccountName
      }
    }
    
  • storageLock.bicep:

    param storageAccountName string
    
    resource storage 'Microsoft.Storage/storageAccounts@2021-09-01' existing = {
      name: storageAccountName
    }
    
    resource storeLock 'Microsoft.Authorization/locks@2017-04-01' = {
      scope: storage
      name: 'storeLock'
      properties: {
        level: 'CanNotDelete'
        notes: 'Storage account should not be deleted.'
      }
    }
    

下一步

如需完整的延伸模組資源類型清單,請參閱擴充其他資源功能的資源類型 (部分機器翻譯)。