匯入 Azure Resource Manager 範本
本逐步說明匯入一個小型 Azure Resource Manager 範本——透過 Azure Verified Modules 模組部署的 Event Hubs 命名空間、執行容器的 App Service 網站、一個靜態 Web 應用程式,以及該網站打算加入的虛擬網路——並把結果建置為一個 Azure Resource Manager 範本。匯入會草擬 Aggregate、Release 與 Manifest:命名空間與網站成為對標準程式庫 Components 的引用,沒有任何 Component 對應的靜態 Web 應用程式則由範本草擬,範本以 Release 能接受的形式宣告的值會被帶入。少數並未以這種形式宣告的值留給你處理,本頁會指出是哪些以及在哪裡。以下每個檔案與每段輸出都來自一次真實執行;範本與逐步說明的每個階段都位於 import-arm 範例中,其中的檢查指令碼會再次執行匯入,把它寫入的內容、印出的內容以及本頁引用的各段內容與保留在那裡的檔案比較,確認草稿在本頁所說的位置中止,並建置檢閱後的 Release。
草擬範例
你可以用本頁的命令執行這個匯入,或是讓你的 AI 助理替你執行。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example import-arm接著它會印出下一步要執行的命令。第一個切換到新資料夾;其後的那一個(見下方)切換到它的 source 資料夾,其中存放本頁所匯入的內容,也是匯入執行的位置:
cd import-arm範本
main.json 是範本的編譯形式:Bicep 建置寫出的 JSON,由 Azure Resource Manager 部署。它的參數是區域,開頭是一個虛擬網路,其中包含網站所加入的子網路:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location": { "type": "string", "defaultValue": "westeurope" } }, "resources": [ { "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2023-11-01", "name": "vnet-notes", "location": "[parameters('location')]", "properties": { "addressSpace": { "addressPrefixes": [ "10.20.0.0/16" ] }, "subnets": [ { "name": "snet-app", "properties": { "addressPrefix": "10.20.1.0/24", "delegations": [ { "name": "web", "properties": { "serviceName": "Microsoft.Web/serverFarms" } } ] } } ] } },[… omitted: the rest of the template]命名空間透過 Azure Verified Modules 模組 avm/res/event-hub/namespace 0.15.3 部署:這是一個 Microsoft.Resources/deployments 資源,其 template 成員存放該模組自己的編譯範本(超過 3,700 行),其 parameters 是範本為該模組設定的值:
{ "type": "Microsoft.Resources/deployments", "apiVersion": "2022-09-01", "name": "ehModule", "properties": { "mode": "Incremental", "expressionEvaluationOptions": { "scope": "inner" }, "parameters": { "name": { "value": "evh-notes" }, "location": { "value": "[parameters('location')]" }, "skuName": { "value": "Standard" }, "skuCapacity": { "value": 2 }, "publicNetworkAccess": { "value": "Enabled" }, "enableTelemetry": { "value": true } }, "template": { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",[… omitted: the module's compiled template, and the rest of the resource]該模組的編譯範本正是 Microsoft Container Registry 為該版本提供的那一份,採用 MIT 授權,範例的 README 附有聲明。匯入讀取它來辨識該模組:其遙測前綴與版本指明了 Azure Verified Modules 模組及其部署的資源類型。
網站是一個一般資源。它執行在一個已經存在的 App Service 方案上,加入範本的子網路,執行 linuxFxVersion 中指定的容器,讀取模組的 name 輸出,並再設定一個變數:
{ "type": "Microsoft.Web/sites", "apiVersion": "2023-12-01", "name": "notes-api", "location": "[parameters('location')]", "kind": "app,linux,container", "dependsOn": [ "[resourceId('Microsoft.Resources/deployments', 'ehModule')]", "[resourceId('Microsoft.Network/virtualNetworks', 'vnet-notes')]" ], "properties": { "serverFarmId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-platform/providers/Microsoft.Web/serverfarms/plan-notes", "virtualNetworkSubnetId": "[resourceId('Microsoft.Network/virtualNetworks/subnets', 'vnet-notes', 'snet-app')]", "siteConfig": { "linuxFxVersion": "DOCKER|ghcr.io/example/notes-api:2.3.0", "appSettings": [ { "name": "EVENTHUB_NAMESPACE", "value": "[reference('ehModule').outputs.name.value]" }, { "name": "EVENTHUB_NAME", "value": "notes" } ] } } },靜態 Web 應用程式同樣是一個一般資源。範本旁邊有一個參數檔,提供唯一的參數:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "location": { "value": "northeurope" } }}這兩個檔案位於資料夾 notes 中,該資料夾旁的 .aggrega-config.yaml 使此目錄成為工作區,因此匯入把草稿寫在那裡:
# The workspace key marks this directory as the workspace root, so an import run# here writes its drafts beside the notes folder.workspace: {}# The registry the importer is fetched from the first time an import needs it.default_registry: hub.aggrega.ai匯入把範本及其參數檔案當作帶註解的 JSON 讀取,依 $schema 與 resources 辨識範本,依 $schema 與 parameters 辨識參數檔案。它把每個資源與工作區目錄中的 Components 比較權衡,讀取方式如匯入如何執行所述,而且不讀取任何雲端帳戶:範本在部署時計算的值,會以計算它的運算式原樣帶入,絕不去查詢。
切換到 source:
cd source執行匯入
指定存放範本的資料夾。匯入器依內容辨識範本及其參數檔案:
aggrega import notes沒有指定 --target。標準程式庫針對 Azure Resource Manager 的 Recipes 繫結到該目標,因此匯入器宣告 arm 為其預設目標,匯入據此為它草擬一份 Manifest;以 --target 指定了目標的匯入則改為對應該目標草擬。匯入寫在草稿旁的記錄會保留該目標,--sync 會重複使用它。
在標準輸出上,它會列出所寫出的內容:
wrote aggregate.yamlwrote release.yamlwrote manifest.yamlwrote catalog/component/imported/compute/microsoft-web-staticsites/0.1.0/component.yamlwrote catalog/recipe/arm/microsoft-web-staticsites/0.1.0/recipe.yamlwrote .aggrega-import.json在標準錯誤上,其中 <workspace> 代表你執行它所在的資料夾:
[… omitted: a warning that the importer runs unverified, printed where its module is read from beside its Plugin entity in a catalog folder rather than fetched]read as the arm format, with drafted paths relative to <workspace>info: the drafts are for the target "arm", the importer's declared default; an import naming a target drafts for that oneinfo: ehmodule deploys the Azure Verified Modules resource module avm/res/event-hub/namespace, of telemetry prefix 46d3xbcp.res.962075c, version 0.15.3, named "Event Hub Namespaces" in its metadata: it is one node of the resource type Microsoft.EventHub/namespaces, the parameters it sets read as the module's, and the module's telemetry is not carried, as whether a Recipe sends telemetry is the Recipe's to setwarn: notes-api sets the variable EVENTHUB_NAMESPACE at properties.siteConfig.appSettings.0.value from ehmodule's outputs.name.value, and the drafted Aggregate does not wire it: ehmodule publishes no provides key for it; the instance notes-api is connected to ehmodule for the dependency alone, a comment above the connection saying why; write the value where the instance is configuredinfo: the resource notes-site of the type Microsoft.Web/staticSites@2023-01-01 is drafted from the source as the Component imported/compute/microsoft-web-staticsites and the Recipe microsoft-web-staticsites on arm, carrying the properties the source writes; the decision's swap hint names the import drafting it from the vendor's schemawarn: the source creates substrate the draft leaves to the operator and drafts nothing for: vnet-notes (Microsoft.Network/virtualNetworks), of which the format says the network a component runs in is the operator's: a component of the catalog takes it by its resource ID under infra.overrides, reading the parameter location at location; provide each where the Release deploys, and fill the pointers the Release leaves for themwarn: notes-api sets the variable EVENTHUB_NAME in the environment of the instance notes-api, and the draft delivers an instance's environment from its connections alone, so the variable is not carried: the instance's Release entry names it, for the author to set where the instance is configuredmapped ehmodule (Microsoft.EventHub/namespaces) to core/messaging/azure-event-hubs as a reference, HIGH at 0.8656mapped notes-site (Microsoft.Web/staticSites) to imported/compute/microsoft-web-staticsites as a draft, NONE at 0.05made no choice for vnet-notes (Microsoft.Network/virtualNetworks)mapped notes-api (Microsoft.Web/sites) to core/compute/server as a reference, HIGH at 0.9made no choice for resourceId('Microsoft.Network/virtualNetworks/subnets', 'vnet-notes', 'snet-app') ()[… omitted: what the pull could not render yet, a stop of the App Service Recipe's assertion that the Release names the subnet, which the sections below explain] external inputs: dependency pins were written and are complete; 0 external input(s) pinned, and any this Release also needs are still unpinned external inputs: a build of this Release halts naming the unpinned URL; fixing what stopped the render and resolving again pins itPull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)External inputs were not fully discovered: dependency pins are complete, external-input pins may not be (see the diagnostics above)info行說明匯入器不經詢問就做出的決定。第一行說明草稿是針對arm。第二行辨識出該模組:資源ehmodule是 Event Hubs 命名空間的 Azure Verified Modules 模組,範本為它設定的參數讀起來就是該模組自己的參數,因此草稿把它視為一個命名空間,而不是需要讀取的巢狀範本。第三行說明靜態 Web 應用程式由範本草擬。warn行指出草稿沒有帶入的內容。第一行是網站從模組的name輸出讀取的變數:草稿只為 Component 發布的內容連接,而命名空間沒有為其名稱發布鍵,因此該連線只為相依性而存在。第二行指出範本建立的虛擬網路,草稿把它留給你處理,如草稿留給你的部分所述。第三行指出網站所設定、但草稿中沒有任何連線傳遞的變數。mapped行說明每個資源變成了什麼。ehmodule是標準程式庫core/messaging/azure-event-hubs的一個實例,notes-api是通用伺服器core/compute/server的一個實例,兩者都在高層級比對並被引用。notes-site不符合任何 Component,因此是一份沒有比對結果的草稿。made no choice行指出草稿對哪些內容不做任何決定:虛擬網路,以及 Release 留空的子網路指標。- pull 是匯入取得已草擬的 Release 所相依的內容,如同
aggrega pull所做的那樣,寫入release.lock。Manifest 為命名空間與網站指定的 Recipes 自行寫出各自的資源,不取得任何來源,因此source_artifacts為0,pull 不會為任何內容到目錄之外。隨後匯入嘗試渲染該 Release,以找出建置將要擷取的輸入,而 App Service Recipe 使其中止:草稿還沒有指定子網路。匯入仍然結束,鎖定檔已寫入,並說明外部輸入尚未被完全找出;這次中止就是接下來驗證所回報的那一次。
它寫出什麼
Aggregate、Release 與 Manifest 都命名為 imported/notes,取自匯入所讀取的資料夾——以檔案匯入的範本則依檔名命名,即 main——位於匯入器的 target_namespace 設定所設定的命名空間下,除非你另行設定,否則為 imported(設定)。
aggregate.yaml——每個資源一個項目,以及網站所畫出的連線:
# Sources read: main.json, main.parameters.json.# Substrate the source creates, left to the operator: vnet-notes (Microsoft.Network/virtualNetworks).apiVersion: aggrega.ai/v1beta1kind: Aggregatemetadata: name: imported/notes version: 0.1.0 description: Draft Aggregate imported from a source in the arm format.spec: components: ehmodule: type: core/messaging/azure-event-hubs notes-site: type: imported/compute/microsoft-web-staticsites values: name: "notes-site" notes-api: type: core/compute/server uses: # Not wired: notes-api sets the variable EVENTHUB_NAMESPACE at properties.siteConfig.appSettings.0.value from ehmodule's outputs.name.value; ehmodule publishes no provides key for it. ehmodule: ehmodule: target: ehmodule靜態 Web 應用程式是匯入草擬的一種型別 imported/compute/microsoft-web-staticsites 的元件。網站到命名空間的連線沒有 inject 區塊,因為只有當 Component 發布某個值時,草稿才會透過連線傳遞變數,而它上方的註解說明了它沒有連接哪個變數以及原因。
release.yaml——環境,以及每個資源的設定:
apiVersion: aggrega.ai/v1beta1kind: Releasemetadata: name: imported/notes # environment is a drafted default, since no source names the environment this Release deploys to; change it to name the one it deploys to. environment: development version: 0.1.0 description: Draft Release imported from a source in the arm format.spec: aggregate: imported/notes manifest: imported/notes components: # Module parameters the source sets and the draft does not translate: location, name, publicNetworkAccess, skuCapacity, skuName. ehmodule: {} notes-site: infra: # From the template parameter location, the value the parameters file supplies. location: "northeurope" overrides: appLocation: "/" outputLocation: "dist" name: "Free" tier: "Free" # Properties the source writes and the draft does not place: kind, properties.siteConfig.linuxFxVersion. # Environment variables the source sets and no connection delivers: EVENTHUB_NAME = notes. notes-api: infra: overrides: service_plan_id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-platform/providers/Microsoft.Web/serverfarms/plan-notes" # From the template parameter location, the value the parameters file supplies. location: "northeurope" # Left for the Release, since the source cannot supply it: infra.overrides.virtual_network_subnet_id, the pointer to the resource "resourceId('Microsoft.Network/virtualNetworks/subnets', 'vnet-notes', 'snet-app')", which the draft leaves to the operator and the source names as "[resourceId('Microsoft.Network/virtualNetworks/subnets', 'vnet-notes', 'snet-app')]"; the catalog Recipe takes the resource's literal identifier: write it virtual_network_subnet_id: ""環境 development 是匯入寫入的預設值,因為範本沒有指定環境。範本的 location 參數以參數檔提供的值 northeurope 傳到靜態 Web 應用程式與網站的項目。網站的 service_plan_id 依範本所寫的字面識別碼帶入。命名空間的項目是空的,它上方的註解列出了草稿不轉換的模組參數。
有一個鍵是空的,其註解說明了原因:virtual_network_subnet_id 是指向某個資源的指標,標準程式庫針對 Azure Resource Manager 的 Recipes 將它當作資源的字面 /subscriptions/… 識別碼接收。範本把網站的子網路寫成一個 resourceId 運算式,指向範本自己的某個資源,而運算式不是 Recipe 能接收的識別碼,因此草稿把該鍵留空,並在註解中寫出該運算式。相較之下,草擬出的資源會完整保留範本的每個運算式。
容器映像檔不在該項目中。範本把它寫在 linuxFxVersion 裡,草稿不放置這個屬性,項目上方的註解把 kind 與 linuxFxVersion 列在它不放置的屬性之中。
manifest.yaml——在 arm 上實現每個元件所用的 Recipe:
apiVersion: aggrega.ai/v1beta1kind: Manifestmetadata: name: imported/notes version: 0.1.0spec: mappings: components: type: {} name: ehmodule: arm/azure-event-hubs notes-site: arm/microsoft-web-staticsites notes-api: arm/server-app-service靜態 Web 應用程式在標準程式庫中沒有對應的 Component,因此匯入在工作區的 catalog 資料夾下草擬一個 Component 及其 Recipe:Component imported/compute/microsoft-web-staticsites 宣告範本賦予該網站的 name、一個 location,以及一個 overrides 物件,其中以廠商自己的名稱存放範本寫出的每個屬性;Recipe microsoft-web-staticsites 寫出一個該類型與 API 版本的 Microsoft.Web/staticSites 資源。這些草稿是可供你閱讀與編輯的一般檔案:
apiVersion: aggrega.ai/v1beta1kind: Componentmetadata: name: imported/compute/microsoft-web-staticsites version: 0.1.0 description: "Draft Component of the resource type Microsoft.Web/staticSites@2023-01-01, imported from a source in the arm format." labels: component.aggrega.ai/type: resource component.aggrega.ai/category: compute component.aggrega.ai/hosting: managedspec: schema: values: type: object properties: # identity: the resource's own name or identifier # The source writing the resource gives no description of name. name: type: string required: true additionalProperties: false infra: type: object properties: # placement: where the resource is deployed # The source writing the resource gives no description of location. location: type: string required: true # override: every writable property no other rule places, each under the vendor's own name overrides: type: object description: "Writable settings of Microsoft.Web/staticSites@2023-01-01, each under the vendor's own name, which the Recipe passes to the resource as written; its header lists the names it reads." additionalProperties: true additionalProperties: false[… omitted: the values the Component publishes, id and name]release.lock 依摘要記錄 Release 所解析到的目錄實體;.aggrega-import.json 是匯入所寫內容的記錄,之後的 aggrega import --sync 會讀取它。
驗證
aggrega validate --release release.yaml驗證中止了。草稿沒有為網站指定子網路,而 App Service Recipe 只透過識別碼讓網站加入你自己網路的某個子網路:
[CATALOG_ERROR E0033] Recipe/FeatureRecipe assertions[… omitted: the line beginning "caused by: Recipe "server-app-service" instance "notes-api" assertion "placement_supplied"", which ends with the Recipe's message: … infra.overrides.virtual_network_subnet_id is "", and this Recipe takes the subnet the app joins by virtual network integration as the subnet's resource ID: set it to /subscriptions/<subscription>/resourceGroups/<group>/providers/Microsoft.Network/virtualNetworks/<network>/subnets/<subnet>, a subnet delegated to Microsoft.Web/serverFarms, or remove it]release dependency validation failed: 1 diagnostic(s)拒絕訊息會在你要修改的文件中指出要改什麼以及在哪裡。這裡是 release.yaml 的 notes-api 項目,在子網路之後,建置還會指出第二個值:容器映像檔。範本把它寫在 linuxFxVersion 中,草稿不放置該屬性,而通用伺服器針對 App Service 的 Recipe 本身不帶映像檔,因此建置會中止,並給出訊息說明該 Release 沒有設定 infra.image。映像檔需由你在 Release 中提供。
命名空間的註解列出了 skuName、skuCapacity 與 location,模組從範本接收它們,草稿同樣沒有放置。沒有它們時,Recipe 本身的預設值生效;infra.overrides.sku_name、infra.overrides.sku_capacity 與 infra.overrides.location 攜帶範本中的值。範例的 reviewed 資料夾中的 release.yaml 寫入了全部這些值:
# Module parameters the source sets and the draft does not translate: location, name, publicNetworkAccess, skuCapacity, skuName. ehmodule: infra: overrides: # Where the template sets skuName and skuCapacity, which the draft places nowhere: the namespace's tier and its throughput units. sku_name: "Standard" sku_capacity: 2 # Where the template sets location, which the draft places nowhere on a module: the namespace's region. location: "northeurope"以及網站的項目,包含映像檔與子網路:
# Properties the source writes and the draft does not place: kind, properties.siteConfig.linuxFxVersion. # Environment variables the source sets and no connection delivers: EVENTHUB_NAME = notes. notes-api: infra: # The template writes the container image in properties.siteConfig.linuxFxVersion, which the draft places nowhere: the image the site runs. image: "ghcr.io/example/notes-api:2.3.0" overrides: service_plan_id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-platform/providers/Microsoft.Web/serverfarms/plan-notes" # From the template parameter location, the value the parameters file supplies. location: "northeurope" # The subnet the template's virtual network holds, as the literal ID the Recipe takes, in the resource group the network is deployed to. virtual_network_subnet_id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-network/providers/Microsoft.Network/virtualNetworks/vnet-notes/subnets/snet-app"範例的檢查指令碼會建置這個 Release,並確認草稿以上面的訊息中止。
建置
切換到存放檢閱後 Release 的資料夾,或以同樣的方式編輯 source 中的 release.yaml,然後建置:
aggrega validate --release release.yamlaggrega build --release release.yaml --auto-pull該 Release 與匯入寫入的 release.lock 已不一致,因此建置需要 --auto-pull,它會重新解析;沒有它,建置會中止並指名 aggrega pull。驗證印出:
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)而建置(其中 <workspace> 代表你執行它所在的資料夾)會印出:
[… omitted: the pull's line reporting the lockfile it wrote][… omitted: a total build time, which differs per run]Build completed: 1 file written to <workspace>/dist閱讀 arm-template.json
建置會寫出一個檔案 dist/arm/arm-template.json:也就是組裝好的範本,語言版本為 2.0,resources 之下每個元件所寫出的每個資源各有一個項目,項目名稱由該元件的實例 id 摺疊成單詞而成:
Ehmodule the Event Hubs namespace, in a nested templateNotesApi the App Service siteNotesApiFtpPublishing the site's FTP publishing policy, which the Recipe turns offNotesApiScmPublishing the site's SCM publishing policy, which the Recipe turns offNotesSite the static web app, the resource the draft Recipe writes命名空間的項目包含 Recipe 寫出的命名空間,帶有 Release 所攜帶的設定——層級、輸送量單位與區域——其餘則由 Recipe 本身決定:
"Ehmodule": { "apiVersion": "2025-04-01", "metadata": { "aggrega.ai/provenance": { "aggrega.ai/aggregate-name": "imported/notes", "aggrega.ai/boundary-path": "default",[… omitted: a compiler-version entry, which moves with the build] "aggrega.ai/component-id": "ehmodule", "aggrega.ai/environment": "development", "aggrega.ai/provider": "arm",[… omitted: a provider-version entry, which moves with the build] "aggrega.ai/recipe-uri": "azure-event-hubs:1.0.0" } }, "name": "[format('Ehmodule-{0}', uniqueString(deployment().name))]", "properties": { "expressionEvaluationOptions": { "scope": "inner" }, "mode": "Incremental", "template": { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "outputs": { "serviceBusEndpoint": { "type": "string", "value": "[reference(resourceId('Microsoft.EventHub/namespaces', 'ehmodule'), '2026-01-01').serviceBusEndpoint]" } }, "resources": [ { "apiVersion": "2026-01-01", "location": "northeurope", "name": "ehmodule", "properties": { "disableLocalAuth": true, "isAutoInflateEnabled": false, "minimumTlsVersion": "1.2", "publicNetworkAccess": "Enabled" }, "sku": { "capacity": 2, "name": "Standard", "tier": "Standard" }, "type": "Microsoft.EventHub/namespaces" } ] } }, "type": "Microsoft.Resources/deployments" },網站的項目帶有方案、子網路、作為網站所執行容器的映像檔,以及通用伺服器宣告的連接埠:
"NotesApi": { "apiVersion": "2025-03-01", "kind": "app,linux,container", "location": "northeurope", "metadata": { "aggrega.ai/provenance": { "aggrega.ai/aggregate-name": "imported/notes", "aggrega.ai/boundary-path": "default",[… omitted: a compiler-version entry, which moves with the build] "aggrega.ai/component-id": "notes-api", "aggrega.ai/environment": "development", "aggrega.ai/provider": "arm",[… omitted: a provider-version entry, which moves with the build] "aggrega.ai/recipe-uri": "server-app-service:1.0.0" } }, "name": "notes-api", "properties": { "clientAffinityEnabled": false, "httpsOnly": true, "publicNetworkAccess": "Disabled", "serverFarmId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-platform/providers/Microsoft.Web/serverfarms/plan-notes", "siteConfig": { "alwaysOn": true, "appSettings": [ { "name": "WEBSITES_PORT", "value": "8080" } ], "ftpsState": "Disabled", "linuxFxVersion": "DOCKER|ghcr.io/example/notes-api:2.3.0", "minTlsVersion": "1.2", "vnetRouteAllEnabled": true }, "virtualNetworkSubnetId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/notes-network/providers/Microsoft.Network/virtualNetworks/vnet-notes/subnets/snet-app" }, "type": "Microsoft.Web/sites" },網站只接收 WEBSITES_PORT,沒有接收範本設定的任何內容:EVENTHUB_NAMESPACE 與 EVENTHUB_NAME 是草稿沒有傳遞的變數,warn 行與註解都指明了它們。命名空間的名稱是 ehmodule,也就是元件的名稱,而範本中它名為 evh-notes:模組的 name 由 Recipe 設定,項目上方的註解將其列在草稿不轉換的參數之中。
部署
此範本是資源群組範本。建置不會寫出參數檔,也不會對 Azure 執行任何動作:這些命令由你自己執行。先預覽範本將變更什麼,再部署:
az deployment group what-if --resource-group <group> --template-file dist/arm/arm-template.jsonaz deployment group create --resource-group <group> --name <name> --template-file dist/arm/arm-template.jsonARM 提供者頁面說明了這些命令、對停止管理的內容絕不刪除的堆疊,以及每個旗標所決定的內容。範本部署之前,Release 指名但範本不部署的資源必須已經存在:網站所加入的 App Service 方案與子網路,該子網路已委派給 Microsoft.Web/serverFarms。
搭配 AI 助理
設定了 Aggrega 工具的 AI 助理也能做同樣的事:把這段提示貼給它。
只使用 Aggrega 工具,不要自行建立任何資料夾。如果 Workspace 資料夾中還沒有 `import-arm` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `import-arm`,且不設 `dir`:它會把範例寫入新的 `import-arm` 資料夾。接著呼叫 `import` 工具,將 `workspace_root` 設為 `import-arm/source`,`source` 設為 `notes`:它會把草稿寫入 `import-arm/source`,並拉取草稿所需的內容。接著呼叫 `build` 工具,將 `workspace_root` 設為 `import-arm/reviewed`,`release` 設為 `release.yaml`,`auto_pull` 設為 true,`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `import-arm/reviewed/dist/` 之下。reviewed 資料夾包含已寫入這些值的草稿,這些值見驗證中的列舉;若要檢閱你自己的草稿,請先請助理驗證 import-arm/source,並告訴你它在哪裡停止。
在 VS Code 中開啟
安裝 Visual Studio Code 的 Aggrega 擴充功能後,在 VS Code 中開啟匯入寫入草稿的資料夾 import-arm/source。該資料夾只包含一個 Release,也就是草擬的 release.yaml,因此編輯器會為你選取它,並在你編輯草稿時依該 Release 檢查草稿,在檔案中與「問題」面板中標出發現的內容。命令選擇區中的 Aggrega: Show topology graph 會繪製該 Release 的元件及其之間的連線。Aggrega: Build、Aggrega: Preview build 與 Aggrega: Pull 作用於同一個 Release,且每個命令也是活動列中 Aggrega 檢視的 Commands 面板上的一個按鈕;Build 會將檔案寫入該資料夾的 dist/ 中。其餘內容見在 Visual Studio Code 中使用 Aggrega。
範本變更時
將 .aggrega-import.json 保留在草稿旁邊,範本變更後執行:
aggrega import notes --sync它讀取記錄所保存的目標 arm,並且只變更匯入寫入的內容。在 main.json 中,靜態 Web 應用程式的 outputLocation 從 dist 變為 build 後,匯入會輸出:
changed spec.components.notes-site.infra.overrides.outputLocation in release.yaml並寫出 release.yaml,其中只有那一行被變更;你寫在它旁邊的值維持不變。來源未更動的部分會保持磁碟上的現狀,無論是否被編輯過;如果來源修改了你編輯過的部分,它會停止、指明該部分且不寫入任何內容,由你手動協調。aggrega import 完整描述了這些規則與每個旗標。
連結相鄰範本的範本
一個範本可以透過 Microsoft.Resources/deployments 資源部署另一個範本,該資源的 templateLink.relativePath 指向它旁邊的一個檔案。匯入會讀取被匯入目錄中彼此連結的範本:它從一個範本(即根範本)開始,讀取根範本所連結的每個範本,並將它們全部的資源草擬為一個 Aggregate。
包含多個範本的目錄透過彼此連結的關係來決定其根:根是沒有被任何其他範本連結的那個範本。包含兩個彼此沒有連結的範本的目錄沒有唯一的根,匯入會停止並指明這兩個範本:
halt: the sources network.json, site.json are each linked by no other source, and the root setting names none of them: set root to the path of the one to start fromroot 設定在工作區設定中該匯入器的項目裡,指定從哪個範本開始。uri 透過匯入器的身分與你所執行的編譯器的版本來指定匯入器,這正是 aggrega plugin add importer 的寫法(哪個匯入器將來源轉換為草稿):
plugins: importer: - uri: importer/arm:<version> with: settings: root: site.json設定 root 後,匯入會讀取 site.json 及其所連結的範本,而不讀取 network.json。
離開被匯入目錄的連結——../shared/dns.json——不會被讀取:匯入從不開啟所給目錄之外的檔案。它會發出警告,指明資源與路徑,並將該資源草擬為獨立的資源,把範本所讀取的、該範本的每個輸出,都作為在範本部署時對其求值的運算式攜帶過來:
warn: dns is a Microsoft.Resources/deployments resource of main.json whose template is linked by a relative path leaving the imported directory, which the import does not read: no resource of that template is drafted, and each of its outputs the template reads is carried as text evaluated when the template deploys; to draft its resources, place the linked template inside the imported directory and link it by templateLink.relativePath把被連結的範本放進被匯入的目錄中,下一次匯入就會讀取它。
名稱已被佔用時
草稿將其作為 Component 的資源,會依其型別採用匯入為它選擇的名稱:靜態 Web 應用程式為 imported/compute/microsoft-web-staticsites。如果該名稱的 Component 已存在於工作區的某個目錄資料夾中或 hub 中,而且不是這次匯入草擬的,匯入不會覆寫它,也不會猜測。它會保留該資源,既不草擬也不參照,並發出警告。在第一次匯入將 Component 寫入工作區的 catalog 資料夾之後,使用 --output-dir second 將該範例第二次匯入到同一工作區的另一個資料夾,會輸出:
warn: notes-site (Microsoft.Web/staticSites) is held back: the catalog holds the Component imported/compute/microsoft-web-staticsites a draft of it takes, which this import did not draft, so the resource is neither drafted nor referenced; to reference it, write in the importer's settings the entry {"mapping_overrides":{"arm":{"Microsoft.Web/staticSites":{"component":"imported/compute/microsoft-web-staticsites","category":"compute","traits":{"component.aggrega.ai/hosting":"managed","component.aggrega.ai/type":"resource"},"vendor":"staticsites"}}}}, then fill the instance's infra in the Release匯入器項目的 mapping_overrides 設定以格式 arm 為鍵,再以資源型別為鍵,將該型別對應到已存在的 Component;警告所印出的項目就是要寫入的那一個(設定)。有了它,下一次匯入就會參照該 Component:notes-site 被對應為參照,它在 Release 中的項目除了一則指明範本所寫屬性的註解外,不含任何鍵:
notes-site: {}Component 宣告了必填的 name 與 location,而草稿兩者都沒有寫入,因此匯入在寫出草稿後,會回報這兩項都缺少並結束。請依 Component 的宣告,將它們以及註解中列出的屬性寫入 Release。
草稿留給你的內容
匯入會草擬範本所陳述的內容與你的目錄所決定的內容,並說明它在哪裡停止。對於範本,它會留下以下內容:
- 範本為服務其他資源而建立的底層設施。 虛擬網路
vnet-notes及其子網路存在於範本中,供網站使用。草稿不建立網路:目錄中的 Component 透過資源 ID 在infra.overrides之下取得其執行所在的網路,而該網路由你負責。warn行會指明每個此類資源,原先參照它的指標則成為 Release 中的空鍵。Release 部署的位置上,網路與子網路已經存在,或者由你用匯入的範本來部署。 - 草稿不放置的值。 Release 中每個項目的註解都會列出草稿不放置的模組參數與資源屬性,此處包括命名空間的層級、輸送量單位、區域與名稱,以及網站的
kind與linuxFxVersion,這樣範本的設定會被明確說明,而不是被悄悄丟棄。如果 Component 為其中某項提供了位置,就由你來寫入,正如驗證一節所做的那樣。 - 沒有任何連線傳遞的環境變數。 網站的
EVENTHUB_NAMESPACE與EVENTHUB_NAME在 Aggregate 與 Release 的註解中被指明,並各有一行warn。網站在 Release 中設定時,由你來設定它們。 - 容器映像檔。 只有當範本在草稿所放置的屬性中指定了映像檔時,草稿才會攜帶映像檔。此範本把它寫在
linuxFxVersion中,因此草稿不攜帶任何映像檔,建置會中止並指明infra.image:映像檔需由你在 Release 中提供。
標準程式庫沒有為其對應任何內容的模組會被完整保留。當範本部署的是某個 Azure Verified Modules 模組,其資源型別在匯入器中沒有任何一列與之對應時,匯入會草擬一個以 oci://mcr.microsoft.com/bicep/<path> 作為該模組來源、並關閉遙測的 Component 與 Recipe,由機密提供的模組參數則被省略並發出警告。型別沒有任何列與之對應的資源,會像靜態 Web 應用程式一樣根據範本草擬;草稿的替換提示指明了 aggrega import --format arm-schema,它改為根據廠商的結構描述草擬同一個 Component,詳見部署目錄中沒有的服務。
跨行撰寫的字串
JSON 不允許在字串中出現原始換行,而匯入器將範本作為 JSON 讀取。包含換行的範本——例如在 scriptContent 值中跨行撰寫的指令碼——會因讀取失敗而無法被辨識:
[E0350] no importer recognises the source (source "main.json"). Pass --format to name it explicitly (for example --format agent-framework, --format arm-schema, --format arm, --format cloudformation-schema, --format compose, --format crewai, --format dify, --format flowise, --format google-adk, --format langflow, --format langgraph, --format mcp, --format n8n or --format terraform-schema)而使用 --format arm 時,匯入會中止並指明出錯位置:
halt: could not parse source "main.json" as arm: json parse: invalid character '\n' in string literal at line 14, column 35將每個字串寫在一行中,在文字換行處使用 \n,匯入就能讀取它。