skip to Main Content

I have an app that has a bicep file (to deploy azure app service) which calls a template module(app service template) in another repository. The template module refer multiple modules in same repository. It calls one module for app service plan, one for app insight, one for app service.

Now I would like to have the app service have a connection string setting to be added which I think can be achieved with resource symbolicname ‘Microsoft.Web/sites/config@2022-03-01’ but it needs a parent parameter which is resource name. I am struggling to figure out a way to achieve this.

//main bicep of repository 1

module appServiceTemplateModule '../appservicetemplate.bicep' = {
  name: ''
  params: {    
    aspName: 'aspName'
    aiName: 'aiName'
    asName: 'asName'
    appSettings: [      
      {
        name: ''
        value: 
      }
      {
        name: ''
        value: 
      }
      {
        name: 'ApplicationInsightsAgent_EXTENSION_VERSION'
        value: '~2'
      }
    ]
  }
}

//Template Repo

//paramaters

//variables

module appserviceplanModule 'appserviceplan.bicep' = {
  name: hostingModule
  params: {
  }
}

module appInsightModule 'appinsights.bicep' = {
  name: aiModule
  params: {
  }
}

module appServiceModule 'appservices.bicep' = {
  name: asModule
  params: {
  }
  dependsOn: [ appserviceplanModule, appInsightModule ]
}

Template repo has individual biceps being called.

I want the bicep files to remain generic so I do not want the appservices.bicep file to have Microsoft.Web/sites/config@2022-03-01 as a resource which can select the app service resource as parent. Stuck with how to achieve this.

Please let me know if any additional details are required.

2

Answers


  1. Chosen as BEST ANSWER

    Another way to get this done which I tried as well You can create this object from your calling main bicep that calls template module

    param connectionStringsObj array = []
    
    resource appServiceResource 'Microsoft.Web/sites@2021-02-01' = {
      name: 
      location: 
      
      properties:{
        serverFarmId: 
        siteConfig:{
          alwaysOn: 
          ftpsState: 
          netFrameworkVersion: 
          appSettings: 
        
        connectionStrings: [for connectionstring in connectionStringsObj: {
          name: connectionstring.name
          connectionString: connectionstring.connectionString
          type: connectionstring.type
        }]
      }
        httpsOnly: 
      }
    }
    

    Main bicep that calls can have it declared like this

    param connectionStrings array = [
        {
            name: 'MyDbContext1'
            connectionString: ''
            type: 'SQLAzure'//or anything else tha is offered
        }
        {
            name: 'MyDbContext2'
            connectionString: ''
            type: 'SQLAzure'//or anything else tha is offered
        }
    ]
    

  2. Use a condition to determine whether or not to create it… if(!empty(connectionstrings))

    I’d probably put all the logic in your appservicetemplate.bicep file and add a new connectionstrings parameter.

    param location string = resourceGroup().location
    
    param connectionstrings object = {
      name: {
        value: 'value'
        type: 'SQLAzure'
      }
    }
    
    resource appServicePlan 'Microsoft.Web/serverfarms@2020-12-01' = {
      name: 'plan'
      location: location
      sku: {
        name: 'F1'
        capacity: 1
      }
    }
    
    resource webApplication 'Microsoft.Web/sites@2021-01-15' = {
      name: 'webappname-${uniqueString(resourceGroup().id)}'
      location: location
      tags: {
        'hidden-related:${resourceGroup().id}/providers/Microsoft.Web/serverfarms/appServicePlan': 'Resource'
      }
      properties: {
        serverFarmId: appServicePlan.id
      }
    
      resource config 'config' = if(!empty(connectionstrings)) {
        name: 'connectionstrings'
        properties: {
            name: {
              value: 'value'
              type: 'SQLAzure'
            }
          }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search