Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.22k stars 746 forks source link

Module reference from registry in CLI #9090

Open turbut opened 1 year ago

turbut commented 1 year ago

I want to call specific module from private registry in CLI by providing template path and parameter file Basically consume module without writting module definiton.

Did anybody tried that? I was unsuccessful with various template paths and template-uri's I just assume that it should be pretty much straight forward thing.

Use case: just got annoyed duplicating params in module file and deployment bicep files

StefanIvemo commented 1 year ago

You can't reference a module in a private registry directly from the deployment commands in Azure CLI and Azure Powershell. What you describe sounds like a good use case for template specs, have you looked into them? https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-specs?tabs=azure-powershell

turbut commented 1 year ago

Template specs unfortunately does not work for my use case because of other complexities. In general looks like missing functionality. If I can reference local module through CLI but can't from bicep registry. I would not use registry if module ref would work cross repo like through https or SSH in a first place :)

Ether way Thanks @StefanIvemo

StefanIvemo commented 1 year ago

Not sure I understand what you would like to be able to do. Can you give me an example of what you are doing today? And how you would like to see it work?

turbut commented 1 year ago

Sure, In local development let's assume I have a module and multiple param files like:

#module.bicep
param name string
param location string
param tags object = {}
resource resourceGroup 'Microsoft.Resources/resourceGroups@2019-05-01' = {
  location: location
  name: name
  tags: tags
}
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
            "value": "myrg"
        },
        "location": {
            "value": "eastus"
        },
        "tags": {
            "value": {
                "Owner": "name@company.com"
            }
        }
    }
}

If i would deploy it locally I could just run:

az deployment sub create --location EastUS --template-file module.bicep--parameters @myparameters.json

And do some sort of parallel loop for all my parameter files.

But we need to consume modules across repositories and have them versioned. So the only possible thing to do is publish them in to registry.

az deployment sub create --location EastUS --template-uri <path to BR module>--parameters @myparameters.json

But I can't so I need to introduce another bicep file:

#deployment.bicep
targetScope = 'subscription'
param name string
param location string
param tags object
module resourceGroups 'br/myregistry:/modules/resourcegroups:0.0.1' = {
  scope: subscription()
  name: '${uniqueString(deployment().name, location)}-resourcegroups'
  params: {
    name: name
    location: location
    tags: tags
  }
}

I only need it to reference the BR module path and version, but in order for it to work I need also include all the params determine when they are optional or required etc. In this example it is easy, but we have modules with tens of optional parameters so whenever I add something to parameters.json I need to double check if deployment.bicep really includes that etc.

My point is that having this middle deployment.bicep file just for using module reference from BR does not make our code DRY and function to call template-uri from BR path would be really beneficial.

alex-frankel commented 1 year ago

We've discussed this at least once in the past and it matches the semantics of deploying a template spec or template link directly from CLI.