azure - 二头肌模板导致部署时出现 "Object reference not set to an instance of an object"异常

标签 azure azure-bicep

下面我添加了 3 个 Bicep 文件的内容。如果将它们添加到同一目录并运行基于订阅的 Azure 部署,如下所示...

az 部署子创建 --name "VerifyBug"--location "northeurope"--template-file .\main.bicep

...您将得到“对象引用未设置到对象实例”。我刚刚花费了 24 小时的大部分时间试图找出到底是什么导致了这个异常,因为它来自于一个更大的复制部署。没有任何迹象表明异常是在哪里抛出的。

我发现问题出在 serverfarm.outputs.serverfarmId 值上。了解如何在稍后与另一个对象联合的对象中使用它。问题似乎是在联合运算符内使用 ARM 输出的组合,但我希望有更技术性的解释。我在这里发布此内容主要是为了帮助其他人避免将来遇到类似的痛苦。

我找到的解决方案是将 function.bicepserverfarm 模块之后的所有内容分解为一个单独的模块,该模块将服务器场 ID 作为参数。然后一切都会正常,但我很想听听关于为什么会这样的解释。

ma​​in.bicep

targetScope = 'subscription'

var location = 'northeurope'

var resourceNamePrefix = 'test-resource'

resource resourceGroup 'Microsoft.Resources/resourceGroups@2020-10-01' = {
  name: '${resourceNamePrefix}-rg'
  location: location
}

module func 'function.bicep' = {
  name: '${deployment().name}-Func'
  scope: resourceGroup
  params: {
    resourceNamePrefix: resourceNamePrefix
    location: location
  }
}

函数.bicep

param location string
param resourceNamePrefix string
param networking object = {}

module serverfarm 'appServicePlan.bicep' = {
  name: '${deployment().name}-AppSvcPlan'
  params: {
    resourceNamePrefix: resourceNamePrefix
    location: location
  }
}

var siteConfig = {
  linuxFxVersion: 'DOTNET-ISOLATED|6.0'
  http20Enabled: true
  alwaysOn: true
  ftpsState: 'Disabled'
  functionAppScaleLimit: 1
  minimumElasticInstanceCount: 1
  vnetRouteAllEnabled: !empty(networking)
}

var basicProperties = {
  serverFarmId: serverfarm.outputs.serverfarmId
  httpsOnly: true
  redundancyMode: 'None'
  reserved: true
  siteConfig: siteConfig
}

var networkingProperties = empty(networking) ? {} : {
  virtualNetworkSubnetId: networking.subnetResourceId
}
var functionProperties = union(basicProperties, networkingProperties)

resource function 'Microsoft.Web/sites@2021-02-01' = {
  name: '${resourceNamePrefix}-fn'
  location: location
  kind: 'functionapp,linux'
  properties: functionProperties
}

appServicePlan.bicep

param resourceNamePrefix string
param location string

resource serverfarm 'Microsoft.Web/serverfarms@2021-02-01' = {
  kind: 'linux'
  name: '${resourceNamePrefix}-appserviceplan'
  location: location
  sku: {
    name: 'S1'
    tier: 'Standard'
  }
  properties: {
    reserved: true 
    maximumElasticWorkerCount: 1
  }
}

output serverfarmId string = serverfarm.id

最佳答案

这是因为“资源管理器在开始部署操作之前解析变量”。 Reference .

在以下部分中,资源管理器尝试解析 serverFarmId: serverfarm.outputs.serverfarmId,但它尚不存在。

var basicProperties = {
  serverFarmId: serverfarm.outputs.serverfarmId
  httpsOnly: true
  redundancyMode: 'None'
  reserved: true
  siteConfig: siteConfig
}

修改后的函数.bicep

在这里,我删除了变量并重建了资源部署,然后将参数从虚拟网络对象更改为带有子网 ID 的字符串。我使用空字符串比使用对象得到了更好的结果。我运行了部署,它运行正常,没有错误。

param location string
param resourceNamePrefix string
param subnetResourceId string = ''

module serverfarm 'appServicePlan.bicep' = {
  name: '${deployment().name}-AppSvcPlan'
  params: {
    resourceNamePrefix: resourceNamePrefix
    location: location
  }
}

resource function 'Microsoft.Web/sites@2021-02-01' = {
  name: '${resourceNamePrefix}-fn'
  location: location
  kind: 'functionapp,linux'
  properties: {
    serverFarmId: serverfarm.outputs.serverfarmId
    httpsOnly: true
    redundancyMode: 'None'
    reserved: true
    siteConfig: {
      linuxFxVersion: 'DOTNET-ISOLATED|6.0'
      http20Enabled: true
      alwaysOn: true
      ftpsState: 'Disabled'
      functionAppScaleLimit: 1
      minimumElasticInstanceCount: 1
      vnetRouteAllEnabled: !empty(subnetResourceId)
    }
    virtualNetworkSubnetId: !empty(subnetResourceId) ? subnetResourceId : null
  }
}

关于azure - 二头肌模板导致部署时出现 "Object reference not set to an instance of an object"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72461613/

相关文章:

azure - 二头肌模板在存储帐户中创建容器

azure - Bicep 使用 URI 发布 Runbook

json - Azure Bicep/Json - 无法正确解析 JSON

azure - microsoft azure创建虚拟网络中资源创建的幂等性

azure - 根据 Azure ML Studio 中的条件替换列中的值

azure - 用户选择取消订阅服务后,如何从 azure AD 中删除 Multi-Tenancy 应用程序的服务主体实例

azure - Cosmos DB 中的事件 ID 是什么?

azure - 如何在 Azure Synapse Analytics 中为管道设置白天和夜间触发器

azure - 将视频上传到azure媒体服务后如何获取发布网址?

azure - 迭代现有资源以获取其 MSI 主体 ID