'How to link route table to subnet in Azure Bicep? Code sample given

I have spent days trying to find out how to link a route table to a subnet using Bicep. The amount of documentation I have found online related to this has been zero. Obviously, it's been done before - but what am I doing wrong?

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
  name: 'coolname'
  location: resourceGroup().location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.156.0.0/15' 
      ]
    }
    subnets: [
      {
        name: 'CloudGuardExternal'
        properties: {
          addressPrefix: '10.156.0.0/24'
          routeTable: // <---------- LOOK HERE
        }
      }   
    ]
  }
}

resource routeTableNumberOne 'Microsoft.Network/routeTables@2020-11-01' = {
  name: 'routelol'
  location: 'australiaeast'
  properties: {
    disableBgpRoutePropagation: false
    routes: [
      {
        name: 'dropPacketTestRoute'
        properties: {
          addressPrefix: '10.0.0.0/8'
          nextHopType: 'None'
          hasBgpOverride: false
        }
      }
    ]
  }
}

I've created a resource group. Inside that, created a subnet. I've also created a route table with a sample route inside.

I have spent DAYS trying to find out how to fill in the routeTable property. I don't get an error - nothing happens. No error and when I deploy the code, nothing appears on the portal.

UPDATE

subnets: [
      {
        name: 'CloudGuardExternal'
        properties: {
          addressPrefix: '10.156.0.0/24'
          routeTable: routeTableNumberOne
        }
      }   
    ]

I tried the above through CLI Commands instead of a pipeline, and this time I get an error message stating

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"BadRequest","message":"{\r\n "error": {\r\n "code": "InvalidRequestFormat",\r\n
"message": "Cannot parse the request.",\r\n "details": [\r\n {\r\n "code": "MissingJsonReferenceId",\r\n
"message": "Value for reference id is missing. Path properties.subnets[0].properties.routeTable."\r\n }\r\n ]\r\n }\r\n}"}]}}

Which is really good news for me because atleast I have an error now!

Update #2

    subnets: [
      {
        name: 'CloudGuardExternal'
        properties: {
          addressPrefix: '10.156.0.0/24'
          routeTable: routeTableNumberOne.properties.routes[0]
        }
      }   
    ]

I tried the above, and this time got a different error message:

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"BadRequest","message":"{\r\n "error": {\r\n "code": "InvalidRequestFormat",\r\n
"message": "Cannot parse the request.",\r\n "details": [\r\n {\r\n "code": "InvalidJsonReferenceWrongType",\r\n
"message": "Reference Id /subscriptions/ee8bbd18-7563-42cd-b616-53841d3f3b28/resourceGroups/hub/providers/Microsoft.Network/routeTables/routelol/routes/dropPacketTestRoute is referencing resource of a wrong type. The Id is expected to reference resources of type routeTables. Path properties.subnets[0].properties.routeTable."\r\n }\r\n ]\r\n }\r\n}"}]}}

Update #3

I tried the following

subnets: [
      {
        name: 'CloudGuardExternal'
        properties: {
          addressPrefix: '10.156.0.0/24'
          routeTable: {
            properties: {
              routes: [
                {     
                  properties: {
          
                    addressPrefix: '10.0.0.0/8'
          
                    nextHopType: 'None'
                    
                  }
          
                }
              ]
            }
          }
        }
      }   
    ]

But this time, got the following error message

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"BadRequest","message":"{\r\n "error": {\r\n "code": "InvalidRequestFormat",\r\n
"message": "Cannot parse the request.",\r\n "details": [\r\n {\r\n "code": "MissingJsonReferenceId",\r\n
"message": "Value for reference id is missing. Path properties.subnets[0].properties.routeTable."\r\n }\r\n ]\r\n }\r\n}"}]}}

Update 4

subnets: [
      {
        name: 'CloudGuardExternal'
        properties: {
          addressPrefix: '10.156.0.0/24'
          routeTable: {
            id: 'nameonecool'
            properties: {
              routes: [
                {   
                  properties: {
                    addressPrefix: '10.0.0.0/8'
                    nextHopType: 'None'                    
                  }
          
                }
              ]
            }
          }
        }
      }   
    ]

Now, I get this error:

{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"BadRequest","message":"{\r\n "error": {\r\n "code": "LinkedInvalidPropertyId",\r\n
"message": "Property id 'nameonecool' at path 'properties.subnets[0].properties.routeTable.id' is invalid. Expect fully qualified resource Id that start with '/subscriptions/{subscriptionId}' or '/providers/{resourceProviderNamespace}/'."\r\n }\r\n}"}]}}

Ok, atleast this error looks unique!



Solution 1:[1]

You must use resourceId like this.

subnets: {
      name: subnet.name
      properties: {
        addressPrefix: subnet.addressPrefix
        routeTable: {
          id: resourceId(subnet.routeTable.resourceGroup, 'Microsoft.Network/routeTables', subnet.routeTable.name) ?? null
        }
      }
    }]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman