Mocas
Mocas

Reputation: 1660

How to provide different param files depends on environment deployment in Bicep?

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?

Upvotes: 3

Views: 1089

Answers (1)

fenrir
fenrir

Reputation: 383

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
  }
}

Upvotes: 0

Related Questions