练习 - 使用循环部署多个资源

已完成

到目前为止,Bicep 模板已部署单个 Azure SQL 逻辑服务器,其中包含用于生产环境的审核设置。 你现在需要部署多个逻辑服务器,为你的公司推出新的智能小熊的每个地区都部署一个。

在本练习中,你将扩展先前创建的 Bicep 代码,以便将数据库实例部署到多个 Azure 区域。

在此过程中,你将:

  • 将现有的 Bicep 代码移动到模块中。
  • 使用复制循环创建新的 Bicep 文件,以多次部署模块的资源。
  • 部署 Bicep 文件,并验证资源的部署。
  • 修改参数以添加更多位置,重新部署文件,然后验证新资源是否已部署。

将资源移动到模块中

  1. 在 Visual Studio Code 中,在你创建了 main.bicep 文件的同一文件夹中,创建一个名为“模块”的新文件夹。

  2. 将 main.bicep 文件移动到刚刚创建的“模块”文件夹中。

  3. 将 main.bicep 文件重命名为 database.bicep。

使用复制循环部署多个实例

  1. 新建一个 main.bicep 文件以替换刚刚移动并重命名的文件。

  2. 打开新的 main.bicep 文件,并添加以下参数:

    @description('The Azure regions into which the resources should be deployed.')
    param locations array = [
      'westeurope'
      'eastus2'
    ]
    
    @secure()
    @description('The administrator login username for the SQL server.')
    param sqlServerAdministratorLogin string
    
    @secure()
    @description('The administrator login password for the SQL server.')
    param sqlServerAdministratorLoginPassword string
    
  3. 在参数声明下,添加以下模块声明:

    module databases 'modules/database.bicep' = [for location in locations: {
      name: 'database-${location}'
      params: {
        location: location
        sqlServerAdministratorLogin: sqlServerAdministratorLogin
        sqlServerAdministratorLoginPassword: sqlServerAdministratorLoginPassword
      }
    }]
    

    请注意,模块声明会循环访问 locations 数组参数中的所有值。

  4. 保存对文件所做的更改。

验证 Bicep 文件

完成上述所有更改后,main.bicep 文件应如以下示例所示:

@description('The Azure regions into which the resources should be deployed.')
param locations array = [
  'westeurope'
  'eastus2'
]

@secure()
@description('The administrator login username for the SQL server.')
param sqlServerAdministratorLogin string

@secure()
@description('The administrator login password for the SQL server.')
param sqlServerAdministratorLoginPassword string

module databases 'modules/database.bicep' = [for location in locations: {
  name: 'database-${location}'
  params: {
    location: location
    sqlServerAdministratorLogin: sqlServerAdministratorLogin
    sqlServerAdministratorLoginPassword: sqlServerAdministratorLoginPassword
  }
}]

database.bicep 文件应如以下示例所示:

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

@secure()
@description('The administrator login username for the SQL server.')
param sqlServerAdministratorLogin string

@secure()
@description('The administrator login password for the SQL server.')
param sqlServerAdministratorLoginPassword string

@description('The name and tier of the SQL database SKU.')
param sqlDatabaseSku object = {
  name: 'Standard'
  tier: 'Standard'
}

@description('The name of the environment. This must be Development or Production.')
@allowed([
  'Development'
  'Production'
])
param environmentName string = 'Development'

@description('The name of the audit storage account SKU.')
param auditStorageAccountSkuName string = 'Standard_LRS'

var sqlServerName = 'teddy${location}${uniqueString(resourceGroup().id)}'
var sqlDatabaseName = 'TeddyBear'
var auditingEnabled = environmentName == 'Production'
var auditStorageAccountName = take('bearaudit${location}${uniqueString(resourceGroup().id)}', 24)

resource sqlServer 'Microsoft.Sql/servers@2021-11-01-preview' = {
  name: sqlServerName
  location: location
  properties: {
    administratorLogin: sqlServerAdministratorLogin
    administratorLoginPassword: sqlServerAdministratorLoginPassword
  }
}

resource sqlDatabase 'Microsoft.Sql/servers/databases@2021-11-01-preview' = {
  parent: sqlServer
  name: sqlDatabaseName
  location: location
  sku: sqlDatabaseSku
}

resource auditStorageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = if (auditingEnabled) {
  name: auditStorageAccountName
  location: location
  sku: {
    name: auditStorageAccountSkuName
  }
  kind: 'StorageV2'  
}

resource sqlServerAudit 'Microsoft.Sql/servers/auditingSettings@2021-11-01-preview' = if (auditingEnabled) {
  parent: sqlServer
  name: 'default'
  properties: {
    state: 'Enabled'
    storageEndpoint: environmentName == 'Production' ? auditStorageAccount.properties.primaryEndpoints.blob : ''
    storageAccountAccessKey: environmentName == 'Production' ? listKeys(auditStorageAccount.id, auditStorageAccount.apiVersion).keys[0].value : ''
  }
}

如果不是,请复制示例或调整模板以与该示例一致。

将 Bicep 模板部署到 Azure

在 Visual Studio Code 终端中,运行以下代码,将 Bicep 模板部署到 Azure:

az deployment group create --template-file main.bicep

在 Visual Studio Code 终端中,运行以下 Azure PowerShell 命令,将 Bicep 模板部署到 Azure:

New-AzResourceGroupDeployment -TemplateFile main.bicep

注意

请务必使用与前面相同的登录名和密码,否则部署将无法成功完成。

等待部署完成。

验证部署

部署完成后,需要验证是否部署了新的逻辑服务器和数据库,以及它们是否位于正确的 Azure 区域中。

  1. 转到 Azure 门户,并确保你位于沙盒订阅中。

  2. 选择 沙盒资源组名称

  3. 验证新的逻辑服务器和数据库是否位于美国东部 2 区域(在 locations 参数的默认值中指定的)。

    Screenshot of the Azure portal, showing the deployment of the logical servers and databases in various locations.

  4. 使页面在浏览器中处于打开状态。 稍后会再次检查部署。

使用逻辑服务器的更多位置更新模板,并将模板重新部署到 Azure

小熊玩具团队即将再次推出新品,这次将面向亚洲市场。 此团队要求你在东亚地区部署新的服务器和数据库。 为此,你需要更新 Bicep 参数并重新部署模板。

  1. 返回到 Visual Studio Code。 在 main.bicep 文件的顶部,向 locations 数组添加一个新值:

    @description('The Azure regions into which the resources should be deployed.')
    param locations array = [
      'westeurope'
      'eastus2'
      'eastasia'
    ]
    
  2. 保存对文件所做的更改。

  3. 在 Visual Studio Code 终端中,通过运行以下代码重新部署文件:

    az deployment group create --template-file main.bicep
    
  1. 返回到 Visual Studio Code。 在 main.bicep 文件的顶部,向 locations 数组添加一个新值:

    @description('The Azure regions into which the resources should be deployed.')
    param locations array = [
      'westeurope'
      'eastus2'
      'eastasia'
    ]
    
  2. 保存对文件所做的更改。

  3. 在 Visual Studio Code 终端中,通过运行以下代码重新部署文件:

    New-AzResourceGroupDeployment -TemplateFile main.bicep
    

注意

请务必使用与前面相同的登录名和密码,否则部署将无法成功完成。

等待部署完成。

验证重新部署

现在你已经重新部署了资源,需要验证是否已在东亚地区创建其他逻辑服务器和数据库资源。

  1. 返回 Azure 门户,选择 [沙盒资源组名称] 资源组。 如有必要,选择“刷新”以查看新部署的资源。

  2. 验证是否已在东亚地区部署新的逻辑服务器和数据库。

    Screenshot of the Azure portal, showing the deployment of a logical server and database in an additional region.