练习 - 审查现有的 Bicep 模板
在我们的方案中,你可以坐下来与质量控制团队的同事一起查看模板。
在就模板进行协作时,你的同事开始询问一系列有关文件结构和内容的问题。 它们看起来有些混乱。 也许你的模板可以从某些改进中受益,变得更易于阅读和理解?
看看以下模板,这是你首次见到该模板。 你是否了解模板中的所有内容都有何用途? 你可以发现多少问题? 可以执行哪些操作来改进模板?
param location string = resourceGroup().location
@allowed([
'F1'
'D1'
'B1'
'B2'
'B3'
'S1'
'S2'
'S3'
'P1'
'P2'
'P3'
'P4'
])
param skuName string = 'F1'
@minValue(1)
param skuCapacity int = 1
param sqlAdministratorLogin string
@secure()
param sqlAdministratorLoginPassword string
param managedIdentityName string
param roleDefinitionId string = 'b24988ac-6180-42a0-ab88-20f7382dd24c'
param webSiteName string = 'webSite${uniqueString(resourceGroup().id)}'
param container1Name string = 'productspecs'
param productmanualsName string = 'productmanuals'
var hostingPlanName = 'hostingplan${uniqueString(resourceGroup().id)}'
var sqlserverName = 'toywebsite${uniqueString(resourceGroup().id)}'
var storageAccountName = 'toywebsite${uniqueString(resourceGroup().id)}'
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: storageAccountName
location: 'eastus'
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
accessTier: 'Hot'
}
resource blobServices 'blobServices' existing = {
name: 'default'
}
}
resource container1 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-05-01' = {
parent: storageAccount::blobServices
name: container1Name
}
resource sqlserver 'Microsoft.Sql/servers@2023-08-01-preview' = {
name: sqlserverName
location: location
properties: {
administratorLogin: sqlAdministratorLogin
administratorLoginPassword: sqlAdministratorLoginPassword
version: '12.0'
}
}
var databaseName = 'ToyCompanyWebsite'
resource sqlserverName_databaseName 'Microsoft.Sql/servers/databases@2023-08-01-preview' = {
name: '${sqlserver.name}/${databaseName}'
location: location
sku: {
name: 'Basic'
}
properties: {
collation: 'SQL_Latin1_General_CP1_CI_AS'
maxSizeBytes: 1073741824
}
}
resource sqlserverName_AllowAllAzureIPs 'Microsoft.Sql/servers/firewallRules@2023-08-01-preview' = {
name: '${sqlserver.name}/AllowAllAzureIPs'
properties: {
endIpAddress: '0.0.0.0'
startIpAddress: '0.0.0.0'
}
dependsOn: [
sqlserver
]
}
resource productmanuals 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-05-01' = {
name: '${storageAccount.name}/default/${productmanualsName}'
}
resource hostingPlan 'Microsoft.Web/serverfarms@2023-12-01' = {
name: hostingPlanName
location: location
sku: {
name: skuName
capacity: skuCapacity
}
}
resource webSite 'Microsoft.Web/sites@2023-12-01' = {
name: webSiteName
location: location
properties: {
serverFarmId: hostingPlan.id
siteConfig: {
appSettings: [
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: AppInsights_webSiteName.properties.InstrumentationKey
}
{
name: 'StorageAccountConnectionString'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
}
]
}
}
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${msi.id}': {}
}
}
}
// We don't need this anymore. We use a managed identity to access the database instead.
//resource webSiteConnectionStrings 'Microsoft.Web/sites/config@2020-06-01' = {
// name: '${webSite.name}/connectionstrings'
// properties: {
// DefaultConnection: {
// value: 'Data Source=tcp:${sqlserver.properties.fullyQualifiedDomainName},1433;Initial Catalog=${databaseName};User Id=${sqlAdministratorLogin}@${sqlserver.properties.fullyQualifiedDomainName};Password=${sqlAdministratorLoginPassword};'
// type: 'SQLAzure'
// }
// }
//}
resource msi 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-07-31-preview' = {
name: managedIdentityName
location: location
}
resource roleassignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(roleDefinitionId, resourceGroup().id)
properties: {
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleDefinitionId)
principalId: msi.properties.principalId
}
}
resource AppInsights_webSiteName 'Microsoft.Insights/components@2020-02-02' = {
name: 'AppInsights'
location: location
kind: 'web'
properties: {
Application_Type: 'web'
}
}
创建并保存 Bicep 文件
在本模块中,你将执行某些更改来改进模板。 你将遵循最佳做法,使模板更容易阅读和理解,让同事能够更轻松地使用。
首先,需要创建 Bicep 文件并将其保存在本地以便使用。
打开 Visual Studio Code。
新建一个名为 main.bicep 的文件。
复制上述 Bicep 模板,将其粘贴到文件中。
保存对文件所做的更改。
重要
通过重新组织和重命名来改进代码的过程称为“重构”。 重构代码时,最好使用 Git 等版本控制系统。 利用版本控制,可对代码进行更改、撤消这些更改或返回到以前的版本。
在本模块中,无需使用 Git 来跟踪文件。 但这个想法很好,因此可考虑选择性地使用。