Coder Perfect

Add an item from a different file to the main bicep template.

Problem

Trying to do something, I don’t know if it’s possible, and if it is, I am asking for some help on how to do it.

I have a file “test.bicep” that has an object:

{
  name: 'testRafaelRule'
  priority: 1001
  ruleCollectionType: 'FirewallPolicyFilterRuleCollection'
  action: {
    type: 'Allow'
  }
  rules: [
    {
      name: 'deleteme-1'
      ipProtocols: [
        'Any'
      ]
      destinationPorts: [
        '*'
      ]
      sourceAddresses: [
        '192.168.0.0/16'
      ]
      sourceIpGroups: []
      destinationIpGroups: []
      destinationAddresses: [
        'AzureCloud.EastUS'
      ]            
      ruleType: 'NetworkRule'
      destinationFqdns: []
    }
  ]
}

I also have another file where I’m attempting to input the object from test.bicep into a property called “ruleCollections”:

resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups@2020-11-01' = {  
  name: 'netrules'  
  properties: {
    priority: 200
    ruleCollections: [
      **ADD_OBJECT_FROM_TEST.BICEP_HERE_HOW?**
    ]
  }
}

any suggestions or links to useful documentation would be helpful. I have looked at outputs and parameters, but I am trying to add just an object into an existing property, I am not adding an entire resource on its own, otherwise, I would output the resouce and consume it with the “module” keyword.

Asked by Rafael Ruales

Solution #1

Although it is not obvious, you can use variables or the output of a module.

var RULE = {
  name: 'testRafaelRule'
  priority: 1001
(...)
}

resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups@2020-11-01' = {  
 name 'netrules'
 properties: {
  ruleCollections: [ 
   RULE
  ]
 }
}

or

rule.bicep

output rule object = {
  name: 'testRafaelRule'
  priority: 1001
(...)
}

main.bicep

module fwrule 'rule.bicep' = {
 name: 'fwrule'
}
resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups@2020-11-01' = {  
 name 'netrules'
 properties: {
  ruleCollections: [ 
   fwrule.outputs.rule
  ]
 }
}

Answered by Miq

Post is based on https://stackoverflow.com/questions/67944470/include-an-object-from-another-file-into-main-bicep-template