クイック スタート: PowerShell で Azure ブループリントを定義して割り当てる

重要

2026 年 7 月 11 日に、Blueprints (プレビュー) は非推奨になります。 既存のブループリントの定義と割り当てを Template Specsデプロイ スタックに移行します。 ブループリント アーティファクトは、デプロイ スタックの定義に使用される ARM JSON テンプレートまたは Bicep ファイルに変換されます。 アーティファクトを ARM リソースとして作成する方法については、次を参照してください。

このチュートリアルでは、Azure Blueprints を使用して、ご自分の組織内のブループリントの作成、発行、および割り当てに関連する一般的ないくつかのタスクを実行する方法について説明します。 このスキルは、Azure Resource Manager (ARM) テンプレート、ポリシー、セキュリティに基づいて、再利用可能で迅速にデプロイできる構成を開発するための一般的なパターンを定義するために役立ちます。

前提条件

  • Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • まだインストールされていない場合は、「Az.Blueprint モジュールを追加する」の手順に従って、PowerShell ギャラリーから Az.Blueprint モジュールをインストールして検証します。
  • 過去に Azure Blueprints を使用したことがない場合は、Azure PowerShell から Register-AzResourceProvider -ProviderNamespace Microsoft.Blueprint を使用してリソース プロバイダーを登録してください。

Azure Cloud Shell

Azure では、ブラウザーを介して使用できる対話型のシェル環境、Azure Cloud Shell がホストされています。 Cloud Shell で Bash または PowerShell を使用して、Azure サービスを操作できます。 ローカル環境に何もインストールしなくても、Cloud Shell にプレインストールされているコマンドを使用して、この記事のコードを実行できます。

Azure Cloud Shell を開始するには、以下のようにします。

オプション 例とリンク
コードまたはコマンド ブロックの右上隅にある [使ってみる] を選択します。 [使ってみる] を選択しても、コードまたはコマンドは Cloud Shell に自動的にはコピーされません。 Screenshot that shows an example of Try It for Azure Cloud Shell.
https://shell.azure.com に移動するか、[Cloud Shell を起動する] ボタンを選択して、ブラウザーで Cloud Shell を開きます。 Button to launch Azure Cloud Shell.
Azure portal の右上にあるメニュー バーの [Cloud Shell] ボタンを選択します。 Screenshot that shows the Cloud Shell button in the Azure portal

Azure Cloud Shell を使用するには、以下のようにします。

  1. Cloud Shell を開始します。

  2. コード ブロック (またはコマンド ブロック) の [コピー] ボタンを選択し、コードまたはコマンドをコピーします。

  3. Windows と Linux では Ctrl+Shift+V キーを選択し、macOS では Cmd+Shift+V キーを選択して、コードまたはコマンドを Cloud Shell セッションに貼り付けます。

  4. Enter キーを選択して、コードまたはコマンドを実行します。

ブループリントを作成する

コンプライアンスの標準的なパターンを定義する最初のステップは、使用可能なリソースからブループリントを作成することです。 MyBlueprint という名前のブループリントを作成して、サブスクリプションのロールとポリシーの割り当てを構成してみましょう。 その後、リソース グループ、ARM テンプレート、およびそのリソース グループに対するロールの割り当てを追加します。

Note

PowerShell を使用するときは、最初に "ブループリント" オブジェクトを作成します。 追加する各 "成果物" でパラメーターを持つものについては、最初の "ブループリント" でパラメーターを事前に定義する必要があります。

  1. 初期 "ブループリント" オブジェクトを作成します。 BlueprintFile パラメーターは、ブループリントに関するプロパティ、作成するリソース グループ、ブループリント レベルのすべてのパラメーターが含まれている JSON ファイルを受け取ります。 パラメーターは割り当て時に設定し、後の手順で追加する成果物によって使用されます。

    • JSON ファイル - blueprint.json

      {
          "properties": {
              "description": "This blueprint sets tag policy and role assignment on the subscription, creates a ResourceGroup, and deploys a resource template and role assignment to that ResourceGroup.",
              "targetScope": "subscription",
              "parameters": {
                  "storageAccountType": {
                      "type": "string",
                      "defaultValue": "Standard_LRS",
                      "allowedValues": [
                          "Standard_LRS",
                          "Standard_GRS",
                          "Standard_ZRS",
                          "Premium_LRS"
                      ],
                      "metadata": {
                          "displayName": "storage account type.",
                          "description": null
                      }
                  },
                  "tagName": {
                      "type": "string",
                      "metadata": {
                          "displayName": "The name of the tag to provide the policy assignment.",
                          "description": null
                      }
                  },
                  "tagValue": {
                      "type": "string",
                      "metadata": {
                          "displayName": "The value of the tag to provide the policy assignment.",
                          "description": null
                      }
                  },
                  "contributors": {
                      "type": "array",
                      "metadata": {
                          "description": "List of AAD object IDs that is assigned Contributor role at the subscription",
                          "strongType": "PrincipalId"
                      }
                  },
                  "owners": {
                      "type": "array",
                      "metadata": {
                          "description": "List of AAD object IDs that is assigned Owner role at the resource group",
                          "strongType": "PrincipalId"
                      }
                  }
              },
              "resourceGroups": {
                  "storageRG": {
                      "description": "Contains the resource template deployment and a role assignment."
                  }
              }
          }
      }
      
    • PowerShell コマンド

      # Login first with Connect-AzAccount if not using Cloud Shell
      
      # Get a reference to the new blueprint object, we'll use it in subsequent steps
      $blueprint = New-AzBlueprint -Name 'MyBlueprint' -BlueprintFile .\blueprint.json
      

      Note

      ブループリントの定義をプログラムで作成する場合は、blueprint.json というファイル名を使用します。 このファイル名は、Import-AzBlueprintWithArtifact を呼び出すときに使用されます。

      既定では、ブループリント オブジェクトが既定のサブスクリプションに作成されます。 管理グループを指定するには、ManagementGroupId パラメーターを使用します。 サブスクリプションを指定するには、SubscriptionId パラメーターを使用します。

  2. サブスクリプションでロールの割り当てを追加します。 ArtifactFile では成果物の種類が定義されていて、プロパティはロール定義の識別子と整合しており、プリンシパルの ID は値の配列として渡されます。 次の例では、指定されたロールを付与されたプリンシパル ID が、ブループリントの割り当て中に設定されるパラメーターに対して構成されます。 この例では、GUID が b24988ac-6180-42a0-ab88-20f7382dd24cContributor 組み込みロールを使用しています。

    • JSON ファイル - \artifacts\roleContributor.json

      {
          "kind": "roleAssignment",
          "properties": {
              "roleDefinitionId": "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
              "principalIds": "[parameters('contributors')]"
          }
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'roleContributor' -ArtifactFile .\artifacts\roleContributor.json
      
  3. サブスクリプションでポリシーの割り当てを追加します。 ArtifactFile では成果物の種類が定義されていて、プロパティはポリシーまたはイニシアティブの定義と整合しており、ブループリントの割り当て中に定義済みのブループリント パラメーターを使用するようにポリシー割り当てを構成します。 この例では、GUID が 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 組み込みポリシーを使用しています。

    • JSON ファイル - \artifacts\policyTags.json

      {
          "kind": "policyAssignment",
          "properties": {
              "displayName": "Apply tag and its default value to resource groups",
              "description": "Apply tag and its default value to resource groups",
              "policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71",
              "parameters": {
                  "tagName": {
                      "value": "[parameters('tagName')]"
                  },
                  "tagValue": {
                      "value": "[parameters('tagValue')]"
                  }
              }
          }
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'policyTags' -ArtifactFile .\artifacts\policyTags.json
      
  4. サブスクリプションでストレージ タグに対する別のポリシー割り当てを (storageAccountType_ parameter を再利用して) 追加します。 この追加のポリシー割り当て成果物は、ブループリントで定義されたパラメーターを複数の成果物で使用できることを示します。 この例では、storageAccountType を使用して、リソース グループにタグを設定します。 この値によって、次のステップで作成するストレージ アカウントに関する情報が提供されます。 この例では、GUID が 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 組み込みポリシーを使用しています。

    • JSON ファイル - \artifacts\policyStorageTags.json

      {
          "kind": "policyAssignment",
          "properties": {
              "displayName": "Apply storage tag to resource group",
              "description": "Apply storage tag and the parameter also used by the template to resource groups",
              "policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71",
              "parameters": {
                  "tagName": {
                      "value": "StorageType"
                  },
                  "tagValue": {
                      "value": "[parameters('storageAccountType')]"
                  }
              }
          }
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'policyStorageTags' -ArtifactFile .\artifacts\policyStorageTags.json
      
  5. リソース グループにテンプレートを追加します。 ARM テンプレートの TemplateFile には、テンプレートの標準的な JSON コンポーネントが含まれています。 また、テンプレートでは、それぞれをテンプレートに渡すことで、storageAccountTypetagNametagValue ブループリント パラメーターを再利用します。 ブループリント パラメーターは、TemplateParameterFile パラメーターを使用してテンプレートで使用でき、テンプレート JSON 内ではそのキーと値のペアを使用して値を挿入します。 ブループリントとテンプレート パラメーターの名前は同じでもかまいません。

    • JSON ARM テンプレート ファイル - \artifacts\templateStorage.json

      {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
              "storageAccountTypeFromBP": {
                  "type": "string",
                  "metadata": {
                      "description": "Storage Account type"
                  }
              },
              "tagNameFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag name from blueprint"
                  }
              },
              "tagValueFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag value from blueprint"
                  }
              }
          },
          "variables": {
              "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
          },
          "resources": [{
              "type": "Microsoft.Storage/storageAccounts",
              "name": "[variables('storageAccountName')]",
              "apiVersion": "2016-01-01",
              "tags": {
                  "[parameters('tagNameFromBP')]": "[parameters('tagValueFromBP')]"
              },
              "location": "[resourceGroup().location]",
              "sku": {
                  "name": "[parameters('storageAccountTypeFromBP')]"
              },
              "kind": "Storage",
              "properties": {}
          }],
          "outputs": {
              "storageAccountSku": {
                  "type": "string",
                  "value": "[variables('storageAccountName')]"
              }
          }
      }
      
    • JSON ARM テンプレート パラメーター ファイル - \artifacts\templateStorageParams.json

      {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
              "storageAccountTypeFromBP": {
                  "value": "[parameters('storageAccountType')]"
              },
              "tagNameFromBP": {
                  "value": "[parameters('tagName')]"
              },
              "tagValueFromBP": {
                  "value": "[parameters('tagValue')]"
              }
          }
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Type TemplateArtifact -Name 'templateStorage' -TemplateFile .\artifacts\templateStorage.json -TemplateParameterFile .\artifacts\templateStorageParams.json -ResourceGroupName storageRG
      
  6. リソース グループにロールの割り当てを追加します。 前のロール割り当てエントリと同様に、次の例では、Owner ロールに対する定義識別子を使用して、ブループリントとは異なるパラメーターを指定します。 この例では、GUID が 8e3af657-a8ff-443c-a75c-2fe8c4bcb635Owner 組み込みロールを使用しています。

    • JSON ファイル - \artifacts\roleOwner.json

      {
          "kind": "roleAssignment",
          "properties": {
              "resourceGroup": "storageRG",
              "roleDefinitionId": "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635",
              "principalIds": "[parameters('owners')]"
          }
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'roleOwner' -ArtifactFile .\artifacts\roleOwner.json
      

ブループリントを発行する

これで成果物がブループリントに追加されたので、発行することができます。 発行すると、ブループリントをサブスクリプションに割り当てることができるようになります。

# Use the reference to the new blueprint object from the previous steps
Publish-AzBlueprint -Blueprint $blueprint -Version '{BlueprintVersion}'

{BlueprintVersion} の値は、文字、数字、ハイフンの (スペースまたは他の特殊文字を含まない) 文字列です。 最大長は 20 文字です。 一意で内容がわかるものを使用します (例: v20180622-135541)。

ブループリントを割り当てる

PowerShell を使用してブループリントを発行したら、それをサブスクリプションに割り当てることができます。 作成したブループリントを、ご自分の管理グループ階層下のいずれかのサブスクリプションに割り当てます。 ブループリントは、サブスクリプションに保存された場合、そのサブスクリプションに対してのみ割り当てることができます。 割り当てるブループリントは、Blueprint パラメーターで指定します。 namelocationidentitylockblueprint パラメーターを指定するには、New-AzBlueprintAssignment コマンドレットで対応する PowerShell パラメーターを使用するか、AssignmentFile パラメーターの JSON ファイルでそれらを指定します。

  1. サブスクリプションに割り当てることにより、ブループリントのデプロイを実行します。 contributors および owners パラメーターにはロールの割り当てを付与するプリンシパルの objectIds の配列が必要なので、Azure Active Directory Graph API を使用して、独自のユーザー、グループ、またはサービス プリンシパルに対する AssignmentFile で使用するための objectIds を収集します。

    • JSON ファイル - blueprintAssignment.json

      {
          "properties": {
              "blueprintId": "/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint",
              "resourceGroups": {
                  "storageRG": {
                      "name": "StorageAccount",
                      "location": "eastus2"
                  }
              },
              "parameters": {
                  "storageAccountType": {
                      "value": "Standard_GRS"
                  },
                  "tagName": {
                      "value": "CostCenter"
                  },
                  "tagValue": {
                      "value": "ContosoIT"
                  },
                  "contributors": {
                      "value": [
                          "7be2f100-3af5-4c15-bcb7-27ee43784a1f",
                          "38833b56-194d-420b-90ce-cff578296714"
                      ]
                  },
                  "owners": {
                      "value": [
                          "44254d2b-a0c7-405f-959c-f829ee31c2e7",
                          "316deb5f-7187-4512-9dd4-21e7798b0ef9"
                      ]
                  }
              }
          },
          "identity": {
              "type": "systemAssigned"
          },
          "location": "westus"
      }
      
    • PowerShell コマンド

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintAssignment -Blueprint $blueprint -Name 'assignMyBlueprint' -AssignmentFile .\blueprintAssignment.json
      
    • ユーザー割り当てマネージド ID

      ブループリントの割り当てでは、ユーザー割り当てマネージド ID を使用することもできます。 この場合、JSON 割り当てファイルの identity の部分が次のように変わります。 {tenantId}{subscriptionId}{yourRG}{userIdentity} をそれぞれ実際のテナント ID、サブスクリプション ID、リソース グループ名、ユーザー割り当てマネージド ID の名前に置き換えます。

      "identity": {
          "type": "userAssigned",
          "tenantId": "{tenantId}",
          "userAssignedIdentities": {
              "/subscriptions/{subscriptionId}/resourceGroups/{yourRG}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{userIdentity}": {}
          }
      },
      

      ユーザー割り当てマネージド ID は、ブループリントを割り当てるユーザーにアクセス許可があれば、どのサブスクリプションおよびどのリソース グループに属していてもかまいません。

      重要

      Azure Blueprints は、ユーザー割り当てのマネージド ID を管理しません。 ユーザーが各自で十分なロールとアクセス許可を割り当てる必要があります。そうしないと、ブループリントの割り当てに失敗します。

リソースをクリーンアップする

ブループリントは、サブスクリプションから削除することができます。 多くの場合、アーティファクトのリソースが不要になったときは削除するようにします。 ブループリントを削除しても、そのブループリントの一部として割り当てられている成果物は後に残されます。 ブループリントの割り当てを解除するには、Remove-AzBlueprintAssignment コマンドレットを使用します。

assignMyBlueprint

Remove-AzBlueprintAssignment -Name 'assignMyBlueprint'

次のステップ

このクイックスタートでは、PowerShell を使用して、ブループリントを作成、割り当て、削除しました。 Azure Blueprints の詳細については、ブループリントのライフサイクルに関する記事に進んでください。