練習 - 檢閱現有的 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 來追蹤檔案。 不過最好是進行此操作,所以請將其視為選擇性的額外項目。