0
点赞
收藏
分享

微信扫一扫

使用BICEP实现Azure资源自动创建 - BICEP模块

BICEP一个非常大的进步就是可以支持module,类似于在terraform中做的一样,这种模式可以极大减少代码的冗余,让整个项目的结构变得更精简,直观

同样地,BICEP中模块的使用也不复杂,如果之前了解过terraform的话,相信上手起来应该会非常快

这次就用一个简单的例子做个demo

整体文件结构

--|main.bicep

  --|modules/storage.bicep

  --|modules/appService.bicep

简单做个说明,详细的云资源的定义是放在modules文件夹里,modules中的内容是可以给多个项目或者场景公用的,所以一般这里要定义的内容会非常多,而main文件中只是引用modules中的内容,然后给一些参数进行赋值即可,类似于编程语言中函数的定义和调用一样,基本是一个道理,还是比较容易理解的

modules/storage.bicep

@description('location which resources should be deployed')
param location string = resourceGroup().location

@description('name of storage account')
@minLength(6)
@maxLength(24)
param storageAccountName string

@description('kind of storage account')
@allowed([
'BlobStorage'
'BlockBlobStorage'
'FileStorage'
'Storage'
'StorageV2'
])
param storageAccountKind string = 'StorageV2'

@description('sku of storage account')
@allowed([
'Premium_LRS'
'Premium_ZRS'
'Standard_GRS'
'Standard_GZRS'
'Standard_LRS'
'Standard_RAGRS'
'Standard_RAGZRS'
'Standard_ZRS'
])
param storageAccountSKU string = 'Standard_LRS'

resource storageaccount 'Microsoft.Storage/storageAccounts@2021-02-01' = {
name: storageAccountName
location: location
kind: storageAccountKind
sku: {
name: storageAccountSKU
}
}

modules/appService.bicep

@description('location which resources should be deployed')
param location string = resourceGroup().location

@description('name of app service plan')
param appServicePlanName string

@description('sku of app service plan')
param appServiceSkuName string

@description('capacity of app service plan')
param appServiceCapacity int = 1

@description('name of app service')
param appServiceName string

@description('OS of app service')
@allowed([
'Linux'
'Windows'
])
param appServicePlanKind string = 'Linux'

resource appServicePlan 'Microsoft.Web/serverfarms@2020-12-01' = {
name: appServicePlanName
location: location
sku: {
name: appServiceSkuName
capacity: appServiceCapacity
}
properties:{
reserved:true
}
kind:appServicePlanKind
}


resource webApplication 'Microsoft.Web/sites@2021-02-01' = {
name: appServiceName
location: location
tags: {
'hidden-related:${resourceGroup().id}/providers/Microsoft.Web/serverfarms/appServicePlan': 'Resource'
}
properties: {
serverFarmId: appServicePlan.id
siteConfig:{
linuxFxVersion:'DOTNETCORE|3.1'
}
httpsOnly:true
}
}

main.bicep

@description('The name of the environment. This must be dev, test, or prod.')
@allowed([
'dev'
'test'
'prod'
])
param environmentName string = 'dev'

@description('storage account name')
param storageAccountName string = 'sa${take(uniqueString(resourceGroup().id), 10)}'

@description('The number of App Service plan instances.')
@minValue(1)
@maxValue(10)
param appServicePlanInstanceCount int = 1

@description('sku of app service plan')
param appServiceSkuName string = 'F1'

@description('The Azure region into which the resources should be deployed.')
param location string = resourceGroup().location

var appServicePlanName = '${environmentName}-bicep-plan'
var appServiceAppName = '${environmentName}-bicep-app'

module appService 'modules/appService.bicep' = {
name: 'appService'
params:{
appServiceName:appServiceAppName
appServicePlanName:appServicePlanName
appServiceSkuName:appServiceSkuName
location:location
}

}

module storage 'modules/storage.bicep' = {

name:'storage'
params:{
storageAccountName:storageAccountName
location:location
storageAccountSKU:(environmentName == 'prod')? 'Standard_GRS' : 'Standard_LRS'
}

}

先用what-if 检查下

az deployment group what-if --template-file .\main.bicep --resource-group bicep

使用BICEP实现Azure资源自动创建 - BICEP模块_Azure

部署

az deployment group what-if --template-file .\main.bicep --resource-group bicep

使用BICEP实现Azure资源自动创建 - BICEP模块_Cloud_02

查看结果

使用BICEP实现Azure资源自动创建 - BICEP模块_IAC_03

举报

相关推荐

0 条评论