skip to Main Content

I’m creating bicep module and by specific condition I need to check if I should create connection string:

param connectionStringName string = 'TestConnectionString'
param connectionStringValue string = ''

resource connectionstring 'Microsoft.Web/sites/config@2023-01-01' = if (addConnectionString) {
  name: 'connectionstrings'
  parent: appServiceFunction
  properties: {
    connectionStringName: {
      value: connectionStringValue
      type: 'SQLServer'
    }
  }
}

Is it possible to dynamically create connection string name, so when I build bicep file to get TestConnectionString instead of connectionStringName, connectionStringName property is ConnStringValueTypePair type, and I didn’t find any way to explicitly define name for such property.

2

Answers


  1. I guess you can do this by using a condition block. You can use the if expression inside the condition block to set different values based on your condition.

    param connectionStringName string = 'TestConnectionString'
    param connectionStringValue string = ''
    param addConnectionString bool = true
    
    var dynamicConnectionStringName = addConnectionString ? connectionStringName : 'DefaultConnectionString'
    
    resource connectionstring 'Microsoft.Web/sites/config@2023-01-01' = {
      name: 'connectionstrings'
      parent: appServiceFunction
      properties: {
        connectionStringName: dynamicConnectionStringName
        type: 'SQLServer'
      }
      condition: addConnectionString
    }

    like this above we have made dynamicConnectionStringName is dynamically set based on the condition. If addConnectionString is true, it uses the connectionStringName parameter; else, it uses a default value (in this case, ‘DefaultConnectionString’).

    Login or Signup to reply.
  2. You can use string interpolation, that will use the connectionStringName parameter value as the property name:

    resource connectionstring 'Microsoft.Web/sites/config@2023-01-01' = if (addConnectionString) {
      name: 'connectionstrings'
      parent: appServiceFunction
      properties: {
        '${connectionStringName}': {
          value: connectionStringValue
          type: 'SQLServer'
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search