title: Bicep template causing "Object reference not set to an instance of an object" exception on deployment
body: <p>Below I've added the contents of 3 Bicep files. If you add these to the same directory and run a subscription-based deployment to Azure as follows...</p>
  <p><code>az deployment sub create --name &quot;VerifyBug&quot; --location &quot;northeurope&quot; --template-file .\main.bicep</code></p>
  <p>...you'll get a &quot;Object reference not set to an instance of an object&quot;. I've just spent the best part of 24 hours trying to work out what exactly was causing this exception because it was coming from a much larger deployment that this replicates. There is no indication of where the exception is being thrown.</p>
  <p>What I've found is that the problem is with the <code>serverfarm.outputs.serverfarmId</code> value. See how this is being used in an object which is later unioned with another object. It seems the problem is a combination of using an ARM output inside a union operator, but I'd love a more technical explanation. I'm mainly posting this here to help anyone else avoid similar pain in the future.</p>
  <p>The solution I've found is to break everything after the <code>serverfarm</code> module out of <em>function.bicep</em> into a separate module which takes the server farm ID as a parameter. Everything then works, but I'd love to hear an explanation about why this is.</p>
  <p><strong>main.bicep</strong></p>
  <pre><code>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
    }
  }
  </code></pre>
  <p><strong>function.bicep</strong></p>
  <pre><code>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
  }
  </code></pre>
  <p><strong>appServicePlan.bicep</strong></p>
  <pre><code>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
  </code></pre>