Bicep 用作部署应用服务和相关资源的模板。

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

Bicep to act as a template for deploying app service and related resources

问题

以下是您要翻译的内容:

"I have individual biceps for app insight, app service plan, and, app service.

I am creating a template bicep where I will call each of these as modules to deploy one app service.
And now whenever for any of my solutions I need to deploy an app service, I will just call this template with minimum parameters so it can deploy an app service with all required supporting resources like app service plan and app insight.

The situation I am stuck with now is I need one app service plan to host two app services one for web and the other for api and each of them should have their own app insight.

The individual modules are very simple, I am still giving a sample for app service plan below:


resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = { name: planName, location: planLocation, sku: { name: planSku tier: planTier } }

output appPlanId string = appServicePlan.id ``` Below I have the app service template where all the individual biceps are being called as modules, shown below:

``` //astemplate.bicep param rgLocation string = resourceGroup().location param name string param planSku string param planTier string param appSettings array = [] param netFrameworkVersion string

var appInsightModule = '${name}-AIModule' var appServicePlanModule = '${name}-ASPModule' var appServiceModule = '${name}-ASModule'

module aspModule ''={ name: appServicePlanModule params: { planName: name planLocation: rgLocation planSku: planSku planTier: planTier } }

module aiAppModule '' = { name: '${appInsightModule}-forApp' params: { name: '${name}-ai-app' location: rgLocation } }

module aiApiModule '' = { name: '${appInsightModule}-forApi' params: { name: '${name}-ai-api' location: rgLocation } }

module asAppModule '' = { name: '${appServiceModule}-app' params: { name: '${name}-app' rgLocation: rgLocation appInsightsKey: aiAppModule.outputs.instrumentationkey appServicePlanId: aspModule.outputs.appPlanId appSettings: appSettings netFrameworkVersion: netFrameworkVersion } dependsOn: [aspModule, aiAppModule] }

module asApiModule '' = { name: '${appServiceModule}-api' params: { name: '${name}-api' rgLocation: rgLocation appInsightsKey: aiApiModule.outputs.instrumentationkey appServicePlanId: aspModule.outputs.appPlanId appSettings: appSettings netFrameworkVersion: netFrameworkVersion } dependsOn: [aspModule, aiApiModule] } ``` What I am trying to achieve is a way to have to make just one call to app insight module and one to app service module instead of two calls each? Because I want this template to satisfy all permutation and combination. If I decide to have one app insight for all app services or one for each.

For App Insight, I was thinking of passing names app insight resources in an array and use for loop at the individual bicep level(resource) but then I got stuck with assigning the appropriate app insight to its related app service.

I hope the question is clear, please let me know if more details or other individual bicep is also required?"

<details>
<summary>英文:</summary>

I have individual biceps for app insight, app service plan, and, app service.

I am creating a template bicep where I will call each of these as modules to deploy one app service.
And now whenever for any of my solutions I need to deploy an app service, I will just call this template with minimum parameters so it can deploy an app service with all required supporting resources like app service plan and app insight.

The situation I am stuck with now is I need one app service plan to host two app services one for web and the other for api and each of them should have their own app insight.

The individual modules are very simple, I am still giving a sample for app service plan below:

//asp.bicep
param planName string
param planLocation string = resourceGroup().Location
param planSku string
param planTier string

resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = { name: planName, location: planLocation, sku: {
name: planSku
tier: planTier
]}

output appPlanId string = appServicePlan.id

Below I have the app service template where all the individual biceps are being called as modules, shown below:

//astemplate.bicep
param rgLocation string = resourceGroup().location
param name string
param planSku string
param planTier string
param appSettings array = []
param netFrameworkVersion string

var appInsightModule = '${name}-AIModule'
var appServicePlanModule = '${name}-ASPModule'
var appServiceModule = '${name}-ASModule'

module aspModule ''={
name: appServicePlanModule
params: {
planName: name
planLocation: rgLocation
planSku: planSku
planTier: planTier
}
}

module aiAppModule '' = {
name: '${appInsightModule}-forApp'
params: {
name: '${name}-ai-app'
location: rgLocation
}
}

module aiApiModule '' = {
name: '${appInsightModule}-forApi'
params: {
name: '${name}-ai-api'
location: rgLocation
}
}

module asAppModule '' = {
name: '${appServiceModule}-app'
params: {
name: '${name}-app'
rgLocation: rgLocation
appInsightsKey: aiAppModule.outputs.instrumentationkey
appServicePlanId: aspModule.outputs.appPlanId
appSettings: appSettings
netFrameworkVersion: netFrameworkVersion
}
dependsOn: [aspModule, aiAppModule]
}

module asApiModule '' = {
name: '${appServiceModule}-api'
params: {
name: '${name}-api'
rgLocation: rgLocation
appInsightsKey: aiApiModule.outputs.instrumentationkey
appServicePlanId: aspModule.outputs.appPlanId
appSettings: appSettings
netFrameworkVersion: netFrameworkVersion
}
dependsOn: [aspModule, aiApiModule]
}


What I am trying to achieve is a way to have to make just one call to app insight module and one to app service module instead of two calls each? Because I want this template to satisfy all permutation and combination. If I decide to have one app insight for all app services or one for each.

For App Insight, I was thinking of passing names app insight resources in an array and use for loop at the individual bicep level(resource) but then I got stuck with assigning the appropriate app insight to its related app service. 

I hope the question is clear, please let me know if more details or other individual bicep is also required?

</details>


# 答案1
**得分**: 0

这是代码的翻译部分:

```bicep
// AppInsight bicep
param name string

param location string = resourceGroup().location

param aiPostFix string = 'ai'

var aiName = '${name}-${aiPostFix}'

resource appInsight 'Microsoft.Insights/components@2020-02-02' = {
  name: aiName
  kind: 'web'
  location: location
  properties:{
    Application_Type:'web'
    Request_Source:'rest'
    Flow_Type:'Bluefield'
  }
}

output instrumentationKey string = appInsight.properties.InstrumentationKey
output insightsName string = appInsight.properties.Name

// ASP bicep
param planName string
param planLocation string = resourceGroup().location
param planSku string
param planTier string

param aspPostFix string = 'asp'

var aspName = '${planName}-${aspPostFix}'

resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = { name: aspName, location: planLocation, sku: {
    name: planSku
    tier: planTier
  } }

  output appPlanId string = appServicePlan.id

// App service bicep
param rgLocation string = resourceGroup().location
param name string
param appServicePlanId string
param appServicePostFix string = 'as'
param netFrameworkVersion string = 'v6.0'
param appInsightsKey string
param appSettings array = []

var appServiceName = '${name}-${appServicePostFix}'

resource appService 'Microsoft.Web/sites@2021-02-01' = {
  name: appServiceName
  location: rgLocation
  properties:{
    serverFarmId: appServicePlanId
    siteConfig:{
      alwaysOn: true
      ftpsState: 'Disabled'
      netFrameworkVersion: netFrameworkVersion
      appSettings: appSettings
    }
    httpsOnly: true    
  }
}

output appServiceId string = appService.id

现在是应用服务模板的 bicep 代码:

// astemplate bicep

param rgLocation string = resourceGroup().location
param aspName string
param planSku string = 'XXX'
param planTier string = 'XXX'
param appSettings array = XXX
param netFrameworkVersion string = 'XXX'
param aiName string
param asName string

var aiModule = '${aiName}-AIModule'
var hostingModule = '${aspName}-ASPModule'
var asModule = '${asName}-ASModule'

module aspModule 'asplan.bicep' = {
  name: hostingModule
  params: {
    planName: aspName
    planLocation: rgLocation
    planSku: planSku
    planTier: planTier
  }
}

module appInsightModule 'ai.bicep' = {
  name: aiModule
  params: {
    name: aiName
    location: rgLocation
  }
}

module appServiceAppModule 'as.bicep' = {
  name: asModule
  params: {
    name: asName
    rgLocation: rgLocation
    appInsightsKey: appInsightModule.outputs.instrumentationKey
    appServicePlanId: aspModule.outputs.appPlanId
    appSettings: appSettings
    netFrameworkVersion: netFrameworkVersion
  }
  dependsOn: [ aspModule, appInsightModule ]
}

现在,例如,我需要一个托管两个应用服务的 ASP,它们都有自己的应用洞察,你可以调用上述模块再次,更新 aiName 和 asName 参数,从web1更新到web2,它将在相同的托管计划上部署另一个应用服务,并具有自己的应用洞察。

英文:
//AppInsight bicep
param name string

param location string = resourceGroup().location

param aiPostFix string = &#39;ai&#39;

var aiName = &#39;${name}-${aiPostFix}&#39;

resource appInsight &#39;Microsoft.Insights/components@2020-02-02&#39; = {
  name: aiName
  kind: &#39;web&#39;
  location: location
  properties:{
    Application_Type:&#39;web&#39;
    Request_Source:&#39;rest&#39;
    Flow_Type:&#39;Bluefield&#39;
  }
}

output instrumentationKey string = appInsight.properties.InstrumentationKey
output insightsName string = appInsight.properties.Name

//ASP bicep
param planName string
param planLocation string = resourceGroup().location
param planSku string
param planTier string

param aspPostFix string = &#39;asp&#39;

var aspName = &#39;${planName}-${aspPostFix}&#39;

resource appServicePlan &#39;Microsoft.Web/serverfarms@2022-03-01&#39; = { name: aspName, location: planLocation, sku: {
    name: planSku
    tier: planTier
  } }

  output appPlanId string = appServicePlan.id

//App service bicep
param rgLocation string = resourceGroup().location
param name string
param appServicePlanId string
param appServicePostFix string = &#39;as&#39;
param netFrameworkVersion string = &#39;v6.0&#39;
param appInsightsKey string
param appSettings array = []

var appServiceName = &#39;${name}-${appServicePostFix}&#39;

resource appService &#39;Microsoft.Web/sites@2021-02-01&#39; = {
  name: appServiceName
  location: rgLocation
  properties:{
    serverFarmId: appServicePlanId
    siteConfig:{
      alwaysOn: true
      ftpsState: &#39;Disabled&#39;
      netFrameworkVersion: netFrameworkVersion
      appSettings: appSettings
    }
    httpsOnly: true    
  }
}

output appServiceId string = appService.id

Now the app service template bicep

//astemplate bicep

param rgLocation string = resourceGroup().location
param aspName string
param planSku string = &#39;XXX&#39;
param planTier string = &#39;XXX&#39;
param appSettings array = XXX
param netFrameworkVersion string = &#39;XXX&#39;
param aiName string
param asName string

var aiModule = &#39;${aiName}-AIModule&#39;
var hostingModule = &#39;${aspName}-ASPModule&#39;
var asModule = &#39;${asName}-ASModule&#39;

module aspModule &#39;asplan.bicep&#39; = {
  name: hostingModule
  params: {
    planName: aspName
    planLocation: rgLocation
    planSku: planSku
    planTier: planTier
  }
}

module appInsightModule &#39;ai.bicep&#39; = {
  name: aiModule
  params: {
    name: aiName
    location: rgLocation
  }
}

module appServiceAppModule &#39;as.bicep&#39; = {
  name: asModule
  params: {
    name: asName
    rgLocation: rgLocation
    appInsightsKey: appInsightModule.outputs.instrumentationKey
    appServicePlanId: aspModule.outputs.appPlanId
    appSettings: appSettings
    netFrameworkVersion: netFrameworkVersion
  }
  dependsOn: [ aspModule, appInsightModule ]
}

Now for e.g. I need one asp which hosts two app services and both have their own app insight

module appServiceApiTemplateModule &#39;astemplate.bicep&#39; = {
  name: &#39;${prefixRName}-ApiModule&#39;
  params: {    
    aspName: prefixRName
    aiName: &#39;${prefixRName}-web1&#39;
    asName: &#39;${prefixRName}-web1&#39;
    appSettings: [      
      {
        name: &#39;Environment&#39;
        value: environment
      }
    ]
  }
}

I can call the above module again and update aiName and asName parameter web1 to web2 and it will deploy one more app service with its own app insight but on the same hosting plan.

huangapple
  • 本文由 发表于 2023年3月20日 23:03:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791956.html
匿名

发表评论

匿名网友

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

确定