Understanding Infrastructure as Code
This lesson explains why infrastructure is defined as code, how declarative Bicep deployments work, and how Bicep and the Azure Machine Learning CLI divide the work.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the benefits of infrastructure as code.
- Describe idempotent deployments and what-if.
- Identify the parts of a Bicep file and parameters file.
- Distinguish what Bicep deploys from what az ml YAML defines.
Why infrastructure as code
A workspace created by hand in the portal is hard to reproduce: its settings exist only in the resource and in someone's memory. Infrastructure as code describes the workspace, its dependencies, network settings and role assignments in files. Deploying the files creates the same infrastructure every time, in every environment, and the files' history in Git shows who changed what and why.
- Repeatability: development, test and production are built from the same definitions with different parameters.
- Review: infrastructure changes go through pull requests like code.
- Recovery: a deleted or broken environment can be recreated from the files.
- Governance: required settings, such as disabled public access, are written once in the template.
Declarative deployments
Bicep is declarative: a file states the desired end state, and Azure Resource Manager works out how to reach it. Deploying the same file twice changes nothing the second time, which is called idempotence. Changing a property in the file and redeploying changes only that property, so the file remains the source of truth.
A deployment targets a scope, most often a resource group with az deployment group create. In the default incremental mode, resources in the resource group that are not in the file are left alone. az deployment group what-if compares the file with what exists and lists the resources it would create, modify or delete, which is the step reviewers look at in a pull request.
az deployment group what-if --resource-group rg-ml-dev \
--template-file main.bicep --parameters env=dev
az deployment group create --resource-group rg-ml-dev \
--template-file main.bicep --parameters env=devBicep structure
A Bicep file is built from a few elements. Parameters take values that differ per deployment, such as the environment name or region. Variables compute values, such as resource names built from a prefix. Resources declare Azure resources with a type, API version, name and properties. Outputs return values, such as the workspace name, to the caller. Modules package a set of resources for reuse.
param env string
param location string = resourceGroup().location
var prefix = 'churn-${env}'
resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
name: 'kv-${prefix}'
location: location
properties: {
tenantId: subscription().tenantId
sku: { family: 'A', name: 'standard' }
enableRbacAuthorization: true
}
}
output keyVaultId string = kv.idReferences between resources, such as kv.id, create implicit dependencies, so Resource Manager deploys resources in the right order. A parameters file (.bicepparam) per environment supplies values such as names, sizes and network settings.
Bicep and the ml extension together
Azure Machine Learning infrastructure has two layers, and teams usually use the best tool for each. Azure resources, such as the workspace, storage, key vault, registry, private endpoints and role assignments, are Azure Resource Manager resources and suit Bicep. Workspace contents, such as datastores, compute, environments, components, data assets and endpoints, are managed by the az ml commands from YAML files, which are designed for them.
Some workspace contents, such as compute clusters, can be declared in Bicep as Microsoft.MachineLearningServices/workspaces/computes. Either approach is valid; consistency within a team matters more than the choice.
Drift between environments
The test and production workspaces were created by hand a year apart. A model that works in test fails in production because production's storage account allows public network access while test's does not, and production lacks a role assignment. The team wants the environments to stay identical except for names and sizes.
Describe the workspace and its dependencies, network settings and role assignments in one Bicep template with a parameters file per environment, and deploy both environments from it through a workflow, using what-if on pull requests. Settings such as public network access are then defined once, and the only differences are the parameter values.
Documenting the settings in a wiki would describe them but not enforce them. Exporting the production resource group's template would capture production's current, incorrect state. Copying settings by hand after each change is the process that caused the drift.
What does running a Bicep deployment twice with the same parameters do, and how can a change be previewed?
Show answer
Bicep deployments are declarative and idempotent, so the second run changes nothing if the resources already match. az deployment group what-if previews the changes a deployment would make.
Exam Tips
Remember for the exam
- IaC files make environments repeatable, reviewable and recoverable.
- Bicep deployments are declarative and idempotent; what-if previews changes.
- Parameters files give each environment its values from one template.
- Bicep for Azure resources; az ml YAML for workspace contents.