Exercise - Deploy multiple resources by using loops
So far, your Bicep template has deployed a single Azure SQL logical server, with auditing settings included for your production environment. You now need to deploy multiple logical servers, one for each region where your company is launching its new smart teddy bear.
In this exercise, you'll extend the Bicep code that you created previously so that you can deploy instances of your databases to multiple Azure regions.
During the process, you'll:
- Move your existing Bicep code into a module.
- Create a new Bicep file with a copy loop to deploy the module's resources multiple times.
- Deploy the Bicep file, and verify the deployment of the resources.
- Modify the parameter to add an additional location, redeploy the file, and then verify that the new resources have been deployed.
Move resources into a module
In Visual Studio Code, create a new folder called modules in the same folder where you created your main.bicep file.
Move your main.bicep file into the modules folder you just created.
Rename the main.bicep file to database.bicep.
Deploy multiple instances by using a copy loop
Create a new main.bicep file to replace the one you just moved and renamed.
Open the new main.bicep file, and add the following parameters:
@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
Below the parameter declarations, add the following module declaration:
module databases 'modules/database.bicep' = [for location in locations: { name: 'database-${location}' params: { location: location sqlServerAdministratorLogin: sqlServerAdministratorLogin sqlServerAdministratorLoginPassword: sqlServerAdministratorLoginPassword } }]
Notice that the module declaration loops over all the values in the
locations
array parameter.Save the changes to the file.
Verify your Bicep file
After you've completed all of the preceding changes, your main.bicep file should look like this example:
@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
}
}]
Your database.bicep file should look like this example:
@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@2023-08-01-preview' = {
name: sqlServerName
location: location
properties: {
administratorLogin: sqlServerAdministratorLogin
administratorLoginPassword: sqlServerAdministratorLoginPassword
}
}
resource sqlDatabase 'Microsoft.Sql/servers/databases@2023-08-01-preview' = {
parent: sqlServer
name: sqlDatabaseName
location: location
sku: sqlDatabaseSku
}
resource auditStorageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = if (auditingEnabled) {
name: auditStorageAccountName
location: location
sku: {
name: auditStorageAccountSkuName
}
kind: 'StorageV2'
}
resource sqlServerAudit 'Microsoft.Sql/servers/auditingSettings@2023-08-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 : ''
}
}
If it doesn't, either copy the example or adjust your template to match the example.
Deploy the Bicep template to Azure
In the Visual Studio Code terminal, deploy the Bicep template to Azure by running the following code:
az deployment group create --template-file main.bicep
In the Visual Studio Code terminal, deploy the Bicep template to Azure by running the following Azure PowerShell command:
New-AzResourceGroupDeployment -TemplateFile main.bicep
Caution
Be sure to use the same login and password that you used previously, or the deployment won't finish successfully.
Wait for the deployment to finish.
Verify the deployment
After the deployment is finished, you want to verify that the new logical server and database are deployed and that they're located in the correct Azure regions.
Go to the Azure portal, and make sure that you're in the sandbox subscription.
Select
[sandbox resource group name] .Verify that new logical server and database are located in the East US 2 region, which you specified in the
locations
parameter's default value.Leave the page open in your browser. You'll check on deployments again later.
Update and redeploy the template to Azure with an additional location for a logical server
The teddy bear toy team is about to launch again, this time into Asia. The team is asking you to deploy a new server and database in the East Asia region. To do so, you need to update your Bicep parameter and redeploy your template.
Return to Visual Studio Code. At the top of the main.bicep file, add a new value to the
locations
array:@description('The Azure regions into which the resources should be deployed.') param locations array = [ 'westeurope' 'eastus2' 'eastasia' ]
Save the changes to the file.
In the Visual Studio Code terminal, redeploy the file by running the following code:
az deployment group create --template-file main.bicep
Return to Visual Studio Code. At the top of the main.bicep file, add a new value to the
locations
array:@description('The Azure regions into which the resources should be deployed.') param locations array = [ 'westeurope' 'eastus2' 'eastasia' ]
Save the changes to the file.
In the Visual Studio Code terminal, redeploy the file by running the following code:
New-AzResourceGroupDeployment -TemplateFile main.bicep
Caution
Be sure to use the same login and password that you used previously, or the deployment won't finish successfully.
Wait for the deployment to finish.
Verify the redeployment
Now that you've redeployed the resources, you want to verify that the additional logical server and database resources have been created in the East Asia region.
Return to the Azure portal and select the
[sandbox resource group name] resource group. If necessary, select Refresh to see the newly deployed resources.Verify that the new logical server and database have been deployed in the East Asia region.