azure - 嵌套 ARM 模板并依赖于另一个资源组

标签 azure azure-resource-manager azure-function-app

我正在尝试在 ARM 中创建一个具有 VNET 集成的功能应用程序。我已经在一个主模板中使所有这些工作都很好。

现在我有一个新要求,其中 VNET 需要位于另一个 RG 中,从而与 Func 应用程序 RG 分开,但 Func 应用程序仍然需要将 VNET 集成到另一个 RG 中的 VNET。

我正在努力如何定义 ARM 模板,以便我在一个 RG 中部署 Func 应用程序,在另一个 RG 中部署 VNET。困难的部分是如何定义它,以便 Func 应用程序使用嵌套模板集成到同一 ARM 模板中另一个 RG 中的 VNET。

这是我的 ARM 模板:

"resources": [{
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2019-10-01",
        "name": "nestedTemplate",
        "resourceGroup": "[parameters('VNETPeered_RG_Name')]",
        "subscriptionId": "0a2009c0-e2ae-4991-aa0e-5c34c141e4cb",
        "properties": {
            "mode": "Incremental",
            "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {},
                "variables": {},
                "resources": [{
                        "comments": "Virtual Network for VNET integration feature in the Premium Plan for the Function App",
                        "type": "Microsoft.Network/virtualNetworks",
                        "apiVersion": "2019-11-01",
                        "name": "[variables('virtual_network_name')]",
                        "location": "[resourceGroup().location]",
                        "properties": {
                            "addressSpace": {
                                "addressPrefixes": [
                                    "[parameters('vnetAddressPrefix')]"
                                ]
                            },
                            "subnets": [{
                                    "name": "[variables('subnet_name')]",
                                    "properties": {
                                        "addressPrefix": "[parameters('subnet1Prefix')]",
                                        "serviceEndpoints": [{
                                                "service": "Microsoft.Storage",
                                                "locations": [
                                                    "[resourceGroup().location]"
                                                ]
                                            }
                                        }
                                    }]
                            }
                        }]
                }
            }
        },
        {
            "comments": "Function App to host the functions themselves. Integrates into a VNET and makes use of Azure DNS Private Zones.",
            "type": "Microsoft.Web/sites",
            "apiVersion": "2019-08-01",
            "name": "[variables('function_app_name')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "nestedTemplate",
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storage_account_name'))]",
                "[resourceId('Microsoft.Web/serverfarms', variables('app_service_plan_name'))]"
            ],
            "kind": "functionapp",
            "identity": {
                "type": "SystemAssigned"
            },
            "properties": {
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('app_service_plan_name'))]",
                "siteConfig": {
                    "appSettings": [{
                        "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                        "value": "[reference(variables('application_insights_resourceId'), '2018-05-01-preview').InstrumentationKey]"
                    }]
                },
                "clientAffinityEnabled": true
            },
            "resources": [{
                "type": "networkConfig",
                "apiVersion": "2019-08-01",
                "name": "virtualNetwork",
                "dependsOn": [
                    "[resourceId('Microsoft.Web/sites', variables('function_app_name'))]"
                ],
                "properties": {
                    "subnetResourceId": "[reference(resourceId('RG-FunctionsGroup','Microsoft.Network/virtualNetworks/subnets', 'vn-MY-VNET', 'sn-MY-SUBNET'),'2020-05-01')]",
                    "isSwift": true
                }
            }]
        ]
    }

当我尝试使用 az cli 中的 az 部署组命令进行部署时,出现以下错误:

Deployment failed. Correlation ID: 39b0173b-8a51-42c5-a796-1d3427556194. {
  "error": {
    "code": "InternalServerError",
    "message": "There was an unexpected InternalServerError.  Please try again later.  x-ms-correlation-request-id: 844e9f35-2e9c-411a-817d-9045511558cb"
  }
}

最佳答案

reference() 可以工作,但是 TLDR;有点重量级

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#resourceid

在这种情况下,

就是您所需要的。要“引用”ARM 中的任何资源,您将使用 ResourceId - 有一些函数可以提供帮助,但如果您了解 ResourceId 的基础知识,它确实会很有帮助,总结如下:

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#return-value-6

当您想要同一部署中的资源的resourceId(与同一模板不同)时,您可以使用速记版本

resourceId({namespace/resourceType}, {resourceName})

如果它在不同的 RG 中,您需要添加 RG 参数,如果它在不同的子中,您也需要添加它。我无法确定您的代码片段,但看起来您的情况所需的就是这个(假设 vnet 和 fn 应用程序位于同一订阅中):

"subnetResourceId": "[resourceId(parameters('VNETPeered_RG_Name'), 'Microsoft.Network/virtualNetworks/subnets', variables('virtual_network_name'), variables('subnet_name'))]"

有帮助吗?

关于azure - 嵌套 ARM 模板并依赖于另一个资源组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64278551/

相关文章:

Azure DevOps 作业忽略失败

amazon-web-services - 使用相同代码库管理多个静态网站的解决方案

azure - 如果模板中未引用现有 key ,ARM 模板会重新生成 key

python - 从 Azure Function APP 运行 Azure CLI 命令

Azure函数应用部署错误 "Encountered an error (ServiceUnavailable) from host runtime. (CODE: 400)"

升级到 2.0 后,Azure CloudStorageAccount.SetConfigurationSettingPublisher() 无效

azure - 用于对话数据提取的 Luis 或文本分析

azure - 如何从 Azure 负载均衡器中删除入站规则

json - Azure 资源管理器 : Web App Slots Config: App Service Authentication

Azure Functions : Querying endpoint throws error 500. 37 - ANCM 无法在启动时间限制内启动