Bicep用于App Service中的连接字符串

huangapple go评论73阅读模式
英文:

Bicep for connection string in App service

问题

我有一个应用程序,其中有一个 bicep 文件(用于部署 Azure 应用程序服务),该文件调用另一个存储库中的模板模块(应用程序服务模板)。该模板模块引用同一存储库中的多个模块。它调用一个模块用于应用程序服务计划,一个用于应用程序洞察,一个用于应用程序服务。

现在,我想让应用程序服务添加一个连接字符串设置,我认为可以通过资源符号名称 'Microsoft.Web/sites/config@2022-03-01' 来实现,但它需要一个父参数,即资源名称。我正在努力找出实现这一目标的方法。

// 存储库 1 的主要 bicep

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

// 模板存储库

// 参数

// 变量

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

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

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

模板存储库有单独的 bicep 文件被调用。

我希望 bicep 文件保持通用,因此我不希望 appservices.bicep 文件作为一个资源具有 'Microsoft.Web/sites/config@2022-03-01',它可以选择应用程序服务资源作为父资源。我被困在如何实现这一目标上。

如果需要额外的详细信息,请告诉我。

英文:

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.

答案1

得分: 1

使用条件来确定是否创建它... if(!empty(connectionstrings))

我可能会将所有逻辑放在您的appservicetemplate.bicep文件中,并添加一个新的connectionstrings参数。

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'
        }
      }
  }
}
英文:

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

答案2

得分: 0

另一种完成此任务的方法是我也尝试过的。您可以从调用模板模块的主 bicep 中创建此对象。

主要调用可以这样声明:

param connectionStrings array = [
    {
        name: 'MyDbContext1'
        connectionString: ''
        type: 'SQLAzure' // 或者任何其他可用的类型
    }
    {
        name: 'MyDbContext2'
        connectionString: ''
        type: 'SQLAzure' // 或者任何其他可用的类型
    }
]

您可以创建此对象,该对象将从您的主 bicep 调用模板模块:

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: 
  }
}
英文:

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

huangapple
  • 本文由 发表于 2023年7月4日 23:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定