如何仅在Azure Function App不存在时通过Bicep创建该资源?
问题:能否让Bicep仅在Azure Function App资源不存在时才创建该资源?
以下是用户提供的Bicep文件:
主模块文件
module functionAppTemplate 'functionApp.bicep' = { name: 'functionAppTemplate' params: { name: '${functionAppName}' kind: functionAppKind location: location servicePlanName: servicePlanName secretSettings: union(appSettings, productionSettings) } dependsOn: [ servicePlanTemplate ] } module functionAppSlotTemplate 'functionAppSlot.bicep' = { name: 'functionAppSlotTemplate' params: { name: '${functionAppName}/staging' kind: functionAppKind location: location servicePlanName: servicePlanName secretSettings: union(appSettings, stagingSettings) } dependsOn: [ functionAppTemplate ] }
functionApp.bicep
resource functionApp 'Microsoft.Web/sites@2018-02-01' = { name: name location: location kind: kind properties: { serverFarmId: resourceId('Microsoft.Web/serverfarms', servicePlanName) clientAffinityEnabled: false httpsOnly: true siteConfig: { use32BitWorkerProcess : false appSettings: secretSettings } } identity: { type: 'SystemAssigned' } }
functionAppSlot.bicep
resource functionAppSlot 'Microsoft.Web/sites/slots@2018-11-01' = { name: name kind: kind location: location properties: { clientAffinityEnabled: true enabled: true httpsOnly: true serverFarmId: resourceId('Microsoft.Web/serverfarms', servicePlanName) siteConfig: { use32BitWorkerProcess : false appSettings: secretSettings } } identity: { type: 'SystemAssigned' } }
可以实现,通过Bicep的resourceExists()函数结合条件部署即可,具体有两种方式:
方式1:在子模块functionApp.bicep内控制资源创建
修改functionApp.bicep,添加条件判断,仅当资源不存在时才执行创建逻辑:
param name string param location string param kind string param servicePlanName string param secretSettings array resource functionApp 'Microsoft.Web/sites@2018-02-01' = if (!resourceExists(resourceId('Microsoft.Web/sites', name))) { name: name location: location kind: kind properties: { serverFarmId: resourceId('Microsoft.Web/serverfarms', servicePlanName) clientAffinityEnabled: false httpsOnly: true siteConfig: { use32BitWorkerProcess : false appSettings: secretSettings } } identity: { type: 'SystemAssigned' } }
resourceExists(resourceId('Microsoft.Web/sites', name))会检查指定名称的Function App是否已存在if (!...)表示仅当资源不存在时,才会创建该资源;若资源已存在,部署时会跳过创建操作,不会修改现有资源
方式2:在主模块中控制是否部署functionAppTemplate模块
修改主模块代码,给functionAppTemplate模块添加条件判断:
module functionAppTemplate 'functionApp.bicep' = if (!resourceExists(resourceId('Microsoft.Web/sites', functionAppName))) { name: 'functionAppTemplate' params: { name: '${functionAppName}' kind: functionAppKind location: location servicePlanName: servicePlanName secretSettings: union(appSettings, productionSettings) } dependsOn: [ servicePlanTemplate ] } module functionAppSlotTemplate 'functionAppSlot.bicep' = { name: 'functionAppSlotTemplate' params: { name: '${functionAppName}/staging' kind: functionAppKind location: location servicePlanName: servicePlanName secretSettings: union(appSettings, stagingSettings) } dependsOn: [ functionAppTemplate ] }
这种方式是直接控制整个子模块的部署,仅当Function App不存在时才部署functionAppTemplate模块。
内容的提问来源于stack exchange,提问作者user989988
相关产品推荐
相关产品推荐

