3

I am building new Bicep infrastructure, I want to split my resources into separate bicep files and use the module way to have a cleaner structure, this way I will only be deploying main.bicep, but I am having problems passing environment specific parameters file using this approach.

The main.bicep

param location string = resourceGroup().location
param env string = '#{env}#' //this will be replaced by the pipeline 

module logicApp 'logicApps/logicApp.bicep' = {
  name: name
  params: {
    env: env
    location: location
  }
}

And in the logicApp.bicep I want to do this (Note where the error is)

param env string
param location string

var logicappName = 'testlogicapp-${env}'

resource logic 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicappName
  location: location
  properties: {
    definition: loadJsonContent('logicapp.json')
    parameters: loadJsonContent('logicapp.parameters.${dev}.json') <-- Error
  }
}

I would have different parameters files, i.e logicapp.parameters.dev.json logicapp.parameters.tst.json etc..

The loadJsonContent doesn't allow dynamic file name loading which is a must for me.

The other solution I could come up with is

var parDev = loadJsonContent('logicapp.parameters.dev.json')
var parUat = loadJsonContent('logicapp.parameters.uat.json')

var params = (env == 'dev') ? parDev : (env == 'uat' ? parUat : null)


resource logic 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicappName
  location: location
  properties: {
    definition: loadJsonContent('logicapp.json')
    parameters: params
  }
}

But this is really ugly and unnecessary loading files that won't be used, and imagine how this would look like when I have 5 environments to deploy to (dev, tst, uat, preprod, prod)

Is there a better way to achieve this?

Mocas
  • 1,403
  • 13
  • 20
  • have you seen this answer related to logic app deployment ?https://stackoverflow.com/a/68608162/4167200 – Thomas Nov 24 '22 at 20:48
  • @Thomas Thanks, I did see that actually. That won't suit my solution as it deploys one logic app using az deployment giving the parameter files in the command, I will be deploying multiple logic apps where each has its own parameter file, also I am not using az deployment, I am using ARM deployment task from yml pipeline. – Mocas Nov 24 '22 at 21:42
  • @Mocas, did you ever find a solution here? – SeaDude Mar 05 '23 at 01:26
  • 1
    @SeaDude unfortunately no, I went with the ugly solution I suggested in the question, had no other options. – Mocas Mar 05 '23 at 08:11
  • This should now work with User Defined Functions which was released 3 weeks ago in v0.17.1 behind an experimental feature flag. https://github.com/Azure/bicep/releases/tag/v0.17.1 – Josh McKearin May 25 '23 at 19:44
  • @Mocas , maybe it's just a typo but in your code you use the env variable to determine the environment but when you construct the config file name, you use ${dev} (not ${env}). Could that have been the issue? Otherwise I think the solution with the ternary operator is the best you are going to get. – Gisli May 26 '23 at 11:22
  • @Gisli yes, you are correct, that is a typo, meant to be env. Not sure if this is what is causing it, but I am not using this anymore, worth retrying at some point and maybe update the question. Well spotted. – Mocas May 26 '23 at 20:47
  • @JoshMcKearin thanks for the update. Might retry this at some point – Mocas May 26 '23 at 20:47
  • Also not very clean, but I think it's the only option because essentially loading dynamic filenames is not allowed in Bicep. I have all parameters for all environments in 1 JSON file and use the correct environment after loading the file. Like this: `var sharedVariablesFile = loadJsonContent('sharedVars.json')` `var sharedVariables = sharedVariablesFile['${env}']` – notStan Jun 23 '23 at 13:52

1 Answers1

0

According to the comment in pasted code, you run it from the pipeline then you can add a task with the script just before the deployment task that will rename the target environment file to the default name.

For example, if it is a deploy dev stage then it will rename logicapp.parameters.dev.json file to logicapp.parameters.json

Rename-Item -Path "$(ParametersFilePath)\logicapp.parameters.$(EnvironmentType).json" -NewName "logicapp.parameters.json"

Thanks to that you do not need to parametrize anything in your module just always reference the same logicapp.parameters.json.

param env string
param location string

var logicappName = 'testlogicapp-${env}'

resource logic 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicappName
  location: location
  properties: {
    definition: loadJsonContent('logicapp.json')
    parameters: loadJsonContent('logicapp.parameters.json') // DO NOT NEED TO PARAMETRIZE THAT
  }
}
fenrir
  • 246
  • 1
  • 9