次の方法で共有


ARM テンプレートで拡張機能リソースのスコープを設定する方法

Azure Resource Manager (ARM) テンプレートで拡張機能リソースの種類で scope プロパティを使用する方法について説明します。 拡張機能リソースを使用すると、ロールの割り当てやロックの適用など、他のリソースに対する機能を変更または追加できます。

拡張機能リソースは、Azure リソースのアクセス許可、ポリシー、およびその他の設定を管理するための強力な方法です。 完全な一覧については、「 他のリソースの機能を拡張するリソースの種類」を参照してください。

scope プロパティは、拡張機能リソースの種類でのみ使用できます。 拡張機能の種類ではないリソースの種類に別のスコープを指定するには、入れ子になったデプロイまたはリンクされたデプロイを使用します。 詳細については、以下を参照してください。

デプロイ スコープでの適用

ターゲット・デプロイメント・スコープで拡張リソース・タイプを適用するには、他のリソース・タイプの場合と同様に、リソースをテンプレートに追加します。 使用可能なスコープは次のとおりです。

  • リソースグループ
  • サブスクリプション
  • 管理グループ
  • テナント

次のテンプレートは、ロックをデプロイします。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Authorization/locks",
      "apiVersion": "2020-05-01",
      "name": "rgLock",
      "properties": {
        "level": "CanNotDelete",
        "notes": "Resource Group should not be deleted."
      }
    }
  ]
}

リソース・グループにデプロイされると、リソース・グループがロックされます。

az deployment group create \
  --resource-group ExampleGroup \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/scope/locktargetscope.json"

次の例では、ロールを割り当てます。

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "principalId": {
      "type": "string",
      "metadata": {
        "description": "The principal to assign the role to"
      }
    },
    "builtInRoleType": {
      "type": "string",
      "allowedValues": [
        "Owner",
        "Contributor",
        "Reader"
      ],
      "metadata": {
        "description": "Built-in role to assign"
      }
    },
    "roleNameGuid": {
      "type": "string",
      "metadata": {
        "description": "The role assignment name"
      }
    }
  },
  "variables": {
    "roleDefinitionIds": {
      "Owner": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635', subscription().subscriptionId)]",
      "Contributor": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c', subscription().subscriptionId)]",
      "Reader": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7', subscription().subscriptionId)]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "name": "[parameters('roleNameGuid')]",
      "properties": {
        "roleDefinitionId": "[variables('roleDefinitionIds')[parameters('builtInRoleType')]]",
        "principalId": "[parameters('principalId')]"
      }
    }
  ]
}

サブスクリプションにデプロイすると、ロールがサブスクリプションに割り当てられます。

az deployment sub create \
  --name demoSubDeployment \
  --___location centralus \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/scope/roletargetscope.json"

リソースに適用

拡張リソースをリソースに適用するには、 scope プロパティを使用します。 scope プロパティを、拡張機能を追加するリソースの名前に設定します。 scope プロパティは、拡張リソースの種類のルート プロパティです。

次の例では、ストレージ アカウントを作成し、それにロールを適用します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "principalId": {
      "type": "string",
      "metadata": {
        "description": "The principal to assign the role to"
      }
    },
    "builtInRoleType": {
      "type": "string",
      "allowedValues": [
        "Owner",
        "Contributor",
        "Reader"
      ],
      "metadata": {
        "description": "Built-in role to assign"
      }
    },
    "roleNameGuid": {
      "type": "string",
      "defaultValue": "[newGuid()]",
      "metadata": {
        "description": "A new GUID used to identify the role assignment"
      }
    },
    "___location": {
      "type": "string",
      "defaultValue": "[resourceGroup().___location]",
      "metadata": {
        "description": "The ___location for the resources"
      }
    }
  },
  "variables": {
    "roleDefinitionIds": {
      "Owner": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635', subscription().subscriptionId)]",
      "Contributor": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c', subscription().subscriptionId)]",
      "Reader": "[format('/subscriptions/{0}/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7', subscription().subscriptionId)]"
    },
    "storageName": "[format('storage{0}', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-01-01",
      "name": "[variables('storageName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "scope": "[format('Microsoft.Storage/storageAccounts/{0}', variables('storageName'))]",
      "name": "[parameters('roleNameGuid')]",
      "properties": {
        "roleDefinitionId": "[variables('roleDefinitionIds')[parameters('builtInRoleType')]]",
        "principalId": "[parameters('principalId')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
      ]
    }
  ]
}

resourceGroup プロパティと subscription プロパティは、入れ子になったデプロイまたはリンクされたデプロイでのみ使用できます。 これらのプロパティは、個々のリソースでは許可されません。 ネストされたデプロイメントまたはリンクされたデプロイメントは、スコープが別のリソース・グループ内のリソースに設定された拡張リソースをデプロイする場合に使用します。

次のステップ