快速入門:使用 REST API 定義和指派 Azure 藍圖

重要

2026 年 7 月 11 日,藍圖(預覽版)即將淘汰。 將現有的藍圖定義和指派移轉至 範本規格部署堆疊。 藍圖成品會轉換成用來定義部署堆疊的 ARM JSON 範本或 Bicep 檔案。 若要瞭解如何將成品撰寫為 ARM 資源,請參閱:

在本教學課程中,您將瞭解如何使用 Azure 藍圖來執行一些與在組織內建立、發佈和指派藍圖相關的一些常見工作。 此技能可協助您定義一般模式,以根據 Azure Resource Manager (ARM) 範本、原則和安全性,開發可重複使用且快速部署的設定。

必要條件

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 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 入口網站右上方功能表列上的 [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 鍵執行程式碼或命令。

開始使用 REST API

如果您不熟悉 REST API,請先檢閱 Azure REST API 參考,特別是有關要求 URI 和要求本文的章節。 本快速入門會使用這些概念來提供使用 Azure 藍圖的指示,並假設他們的工作知識。 ARMClient 之類的工具可以自動處理授權,並建議初學者使用。

如需 Azure 藍圖規格,請參閱 Azure 藍圖 REST API

REST API 和 PowerShell

如果您還沒有進行 REST API 呼叫的工具,請考慮使用這些指示使用 PowerShell。 以下是使用 Azure 進行驗證的範例標頭。 產生驗證標頭,有時稱為 持有人令牌,並提供 REST API URI 以與任何參數或 Request Body連線:

# Log in first with Connect-AzAccount if not using Cloud Shell

$azContext = Get-AzContext
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
    'Content-Type'='application/json'
    'Authorization'='Bearer ' + $token.AccessToken
}

# Invoke the REST API
$restUri = 'https://management.azure.com/subscriptions/{subscriptionId}?api-version=2020-01-01'
$response = Invoke-RestMethod -Uri $restUri -Method Get -Headers $authHeader

在上述$restUri變數中取代 {subscriptionId} ,以取得訂用帳戶的相關信息。 $response變數會保存 Cmdlet 的結果Invoke-RestMethod,您可以使用 ConvertFrom-JsonCmdlet 剖析。 如果 REST API 服務端點需要 Request Body,請將 JSON 格式的變數 -Body 提供給 的 Invoke-RestMethod參數。

建立藍圖

定義合規性標準模式的第一個步驟是從可用的資源撰寫藍圖。 讓我們建立名為 MyBlueprint 的藍圖,以設定訂用帳戶的角色和原則指派。 然後,您會在資源群組上新增資源群組、ARM 範本和角色指派。

注意

當您使用 REST API 時,會 先建立藍圖 物件。 若要新增具有參數的每個 成品 ,您可以事先在初始 藍圖上定義參數。

在每個 REST API URI 中,以您自己的值取代下列變數:

  • {YourMG} - 以管理群組的識別碼取代 。
  • {subscriptionId} - 以您的訂用帳戶識別碼取代 。

注意

您也可以在訂用帳戶層級建立藍圖。 如需詳細資訊,請參閱 在訂用帳戶範例中建立藍圖。

  1. 建立初始 藍圖 物件。 包含 Request Body 藍圖的相關屬性、要建立的任何資源群組,以及所有藍圖層級參數。 您可以在指派期間設定參數,這些參數會由您在後續步驟中新增的成品使用。

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "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",
                      "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"
                      }
                  },
                  "owners": {
                      "type": "array",
                      "metadata": {
                          "description": "List of AAD object IDs that is assigned Owner role at the resource group"
                      }
                  }
              },
              "resourceGroups": {
                  "storageRG": {
                      "description": "Contains the resource template deployment and a role assignment."
                  }
              }
          }
      }
      
  2. 在訂用帳戶上新增角色指派。 會 Request Body 定義成品種類、屬性與角色定義標識符一致,而主體身分識別會以值陣列的形式傳遞。 在下列範例中,授與指定角色的主體身分識別會設定為藍圖指派期間所設定的參數。 此範例會 Contributor 使用內建角色,且 GUID 為 b24988ac-6180-42a0-ab88-20f7382dd24c

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/artifacts/roleContributor?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "kind": "roleAssignment",
          "properties": {
              "roleDefinitionId": "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
              "principalIds": "[parameters('contributors')]"
          }
      }
      
  3. 在訂用帳戶上新增原則指派。 會 Request Body 定義成品種類、屬性與原則或計劃定義一致,並將原則指派設定為在藍圖指派期間使用定義的藍圖參數。 此範例會使用內 Apply tag and its default value to resource groups 建原則,且 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/artifacts/policyTags?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "kind": "policyAssignment",
          "properties": {
              "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')]"
                  }
              }
          }
      }
      
  4. 在訂用帳戶上新增記憶體標籤的另一個原則指派(藉由重複使用 storageAccountType_ parameter)。 這個額外的原則指派成品示範藍圖上定義的參數可供多個成品使用。 在此範例中,您會使用 storageAccountType 來設定資源群組上的標記。 此值提供您在下一個步驟中建立之記憶體帳戶的相關信息。 此範例會使用內 Apply tag and its default value to resource groups 建原則,且 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/artifacts/policyStorageTags?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "kind": "policyAssignment",
          "properties": {
              "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')]"
                  }
              }
          }
      }
      
  5. 在資源群組下新增範本。 Request Body ARM 樣本的 包含範本的一般 JSON 元件,並使用 定義目標資源群組properties.resourceGroup。 此範本也會藉由將每個 參數傳遞至範本, storageAccountType重複使用、 tagNametagValue 藍圖參數。 藍圖參數可藉由定義 properties.parameters範本,並在範本 JSON 內使用索引鍵/值組來插入值。 藍圖和範本參數名稱可以相同,但在這裡不同,以說明每個專案如何從藍圖傳遞至範本成品。

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/artifacts/templateStorage?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "kind": "template",
          "properties": {
              "template": {
                  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                  "contentVersion": "1.0.0.0",
                  "parameters": {
                      "storageAccountTypeFromBP": {
                          "type": "string",
                          "defaultValue": "Standard_LRS",
                          "allowedValues": [
                              "Standard_LRS",
                              "Standard_GRS",
                              "Standard_ZRS",
                              "Premium_LRS"
                          ],
                          "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": "[resourceGroups('storageRG').location]",
                      "sku": {
                          "name": "[parameters('storageAccountTypeFromBP')]"
                      },
                      "kind": "Storage",
                      "properties": {}
                  }],
                  "outputs": {
                      "storageAccountSku": {
                          "type": "string",
                          "value": "[variables('storageAccountName')]"
                      }
                  }
              },
              "resourceGroup": "storageRG",
              "parameters": {
                  "storageAccountTypeFromBP": {
                      "value": "[parameters('storageAccountType')]"
                  },
                  "tagNameFromBP": {
                      "value": "[parameters('tagName')]"
                  },
                  "tagValueFromBP": {
                      "value": "[parameters('tagValue')]"
                  }
              }
          }
      }
      
  6. 在資源群組下新增角色指派。 與先前的角色指派項目類似,下列範例會使用角色的定義標識碼 Owner ,並提供與藍圖不同的參數。 此範例會 Owner 使用內建角色,且 GUID 為 8e3af657-a8ff-443c-a75c-2fe8c4bcb635

    • REST API URI

      PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/artifacts/roleOwner?api-version=2018-11-01-preview
      
    • 要求本文

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

發佈藍圖

現在您已將成品新增至藍圖,現在可以發佈它。 發佈可讓藍圖指派給訂用帳戶。

  • REST API URI

    PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint/versions/{BlueprintVersion}?api-version=2018-11-01-preview
    

的值 {BlueprintVersion} 是字母、數位和連字元的字串(不含空格或其他特殊字元)。 長度上限為 20 個字元。 使用唯一和信息的東西,例如 v20180622-135541

指派藍圖

使用 REST API 發佈藍圖之後,即可將藍圖指派給訂用帳戶。 將您建立的藍圖指派給管理群組階層下的其中一個訂用帳戶。 如果藍圖儲存至訂用帳戶,則只能指派給該訂用帳戶。 Request Body指定要指派的藍圖,並將名稱和位置提供給藍圖定義中的任何資源群組。 Request Body 也提供藍圖上定義並供一或多個附加成品使用的所有參數。

在每個 REST API URI 中,以您自己的值取代下列變數:

  • {tenantId} - 以您的租使用者識別碼取代 。
  • {YourMG} - 以管理群組的識別碼取代 。
  • {subscriptionId} - 以您的訂用帳戶識別碼取代 。
  1. 提供 Azure 藍圖服務主體 Owner 目標訂用帳戶上的角色。 AppId是靜態的 (f71766dc-90d9-4b7d-bd9d-4499c4331c3f),但服務主體標識符會因租用戶而異。 使用下列 REST API 來要求租使用者的詳細數據。 它會使用 具有不同授權的 Azure Active Directory 圖形 API

    • REST API URI

      GET https://graph.windows.net/{tenantId}/servicePrincipals?api-version=1.6&$filter=appId eq 'f71766dc-90d9-4b7d-bd9d-4499c4331c3f'
      
  2. 將藍圖部署指派給訂用帳戶,以執行藍圖部署。 contributors由於 和 owners 參數需要授與角色指派的主體陣列objectIds,因此請使用 Azure Active Directory Graph API 收集 ,以供objectIds您自己的使用者、群組或服務主體使用Request Body

    • REST API URI

      PUT https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Blueprint/blueprintAssignments/assignMyBlueprint?api-version=2018-11-01-preview
      
    • 要求本文

      {
          "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"
      }
      
    • 使用者指派的受控識別

      藍圖指派也可以使用 使用者指派的受控識別。 在此情況下, identity 要求本文的部分會變更,如下所示。 將和 {userIdentity} 分別取代{yourRG}為您的資源組名和使用者指派的受控識別名稱。

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

      使用者指派的受控識別可以位於指派藍圖的使用者具有許可權的任何訂用帳戶和資源群組中。

      重要

      Azure 藍圖不會管理使用者指派的受控識別。 用戶負責指派足夠的角色和許可權,否則藍圖指派將會失敗。

清除資源

取消指派藍圖

您可以從訂用帳戶移除藍圖。 不再需要成品資源時,通常會移除。 拿掉藍圖時,指派為該藍圖一部分的成品會留下。 若要移除藍圖指派,請使用下列 REST API 作業:

  • REST API URI

    DELETE https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Blueprint/blueprintAssignments/assignMyBlueprint?api-version=2018-11-01-preview
    

刪除藍圖

若要移除藍圖本身,請使用下列 REST API 作業:

  • REST API URI

    DELETE https://management.azure.com/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint?api-version=2018-11-01-preview
    

下一步

在本快速入門中,您已使用 REST API 建立、指派及移除藍圖。 若要深入瞭解 Azure 藍圖,請繼續進行藍圖生命週期一文。