次の方法で共有


ARM テンプレートでの拡張リソースのスコープの設定

拡張リソースは、別のリソースを変更するリソースです。 たとえば、リソースにロールを割り当てることができます。 ロールの割り当ては、拡張機能のリソースの種類です。

拡張リソースの種類の完全な一覧については、「 他のリソースの機能を拡張するリソースの種類」を参照してください。

この記事では、Azure Resource Manager テンプレート (ARM テンプレート) を使用してデプロイするときに、拡張リソースの種類のスコープを設定する方法について説明します。 これは、リソースに適用するときに拡張リソースで使用できる scope プロパティについて説明します。

scope プロパティは、拡張リソースの種類でのみ使用できます。 拡張機能の種類ではないリソースの種類に別のスコープを指定するには、入れ子になったデプロイまたはリンクされたデプロイを使用します。 詳細については、「 リソース グループのデプロイ」、「 サブスクリプションのデプロイ」、「 管理グループのデプロイ」、および 「テナントのデプロイ」を参照してください。

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

ターゲット・デプロイメント・スコープで拡張リソース・タイプを適用するには、他のリソース・タイプの場合と同様に、リソースをテンプレートに追加します。 使用可能なスコープは、 リソース グループサブスクリプション管理グループテナントです。 デプロイ スコープは、リソースの種類をサポートする必要があります。

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

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/locks",
      "apiVersion": "2016-09-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.1",
  "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": {
    "Owner": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')]",
    "Contributor": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]",
    "Reader": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]"
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2020-10-01-preview",
      "name": "[parameters('roleNameGuid')]",
      "properties": {
        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
        "principalId": "[parameters('principalId')]"
      }
    }
  ],
  "outputs": {}
}

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

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]"
    }
  },
  "variables": {
    "Owner": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')]",
    "Contributor": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]",
    "Reader": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]",
    "storageName": "[concat('storage', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-04-01",
      "name": "[variables('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2020-10-01-preview",
      "name": "[parameters('roleNameGuid')]",
      "scope": "[concat('Microsoft.Storage/storageAccounts', '/', variables('storageName'))]",
      "dependsOn": [
        "[variables('storageName')]"
      ],
      "properties": {
        "roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
        "principalId": "[parameters('principalId')]"
      }
    }
  ]
}

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

次のステップ