导入 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 留空的子网指针。- 拉取是导入获取已起草的 Release 所依赖的内容,如同
aggrega pull所做的那样,写入release.lock。Manifest 为命名空间和站点指定的 Recipes 自行写出各自的资源,不获取任何来源,因此source_artifacts为0,拉取不会为任何内容访问目录之外。随后导入尝试渲染该 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,它改为根据供应商的 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,导入就能读取它。