Defining an IaC Strategy for Source Control, Testing and Deployment
This lesson explains how to apply engineering discipline to infrastructure definitions.
Learning outcomes
After completing this lesson, you will be able to:
- Decide where IaC files should live.
- Describe the stages of IaC testing.
- Explain what what-if shows.
- Read a pipeline that deploys IaC.
Source control
IaC files belong in Git, with the same branch policies as application code. A team must choose where they live. Keeping them with the application suits resources owned by that application, and allows the two to change together. A separate repository suits shared platform resources, such as hub networks, owned by a platform team.
Shared standards belong in modules. Bicep modules can be published to a private Bicep registry in Azure Container Registry, or as template specs, and they are versioned. Azure Verified Modules are Microsoft-maintained modules that follow good practice, which is usually a better starting point than writing a module from nothing.
module web 'br:contosoregistry.azurecr.io/bicep/web-app:1.4.0' = {
name: 'web-deployment'
params: {
appName: 'contoso-web'
}
}Where IaC lives
Testing infrastructure
Testing IaC is staged, in the same way that application testing is. Each stage catches a different class of error.
Linting
The Bicep linter runs in bicep lint and bicep build, and it checks good practice, such as hard-coded environment URLs and unused parameters. Rules are configured in bicepconfig.json and can be set to error level, which fails a pipeline. ARM-TTK and tools such as Checkov add further checks.
Validation
az deployment group validate asks Azure Resource Manager to check that a template would be accepted. This catches errors such as invalid property values and quota limits, as well as Azure Policy Deny effects, without changing anything.
What-if
What-if compares a template with the current state of Azure and predicts the changes, shown as create, modify, delete or no change. Running it on a pull request, and posting the result for reviewers, is a strong practice. It shows the effect of a change, which a diff of the code alone may not reveal.
Integration and policy tests
A real deployment to a temporary environment proves that everything works, and tests can then verify connectivity or configuration. The environment is deleted afterwards. Azure Policy enforces rules whichever tool is used.
The following diagram shows what-if output reaching the reviewer.

Stages of IaC testing
Deploying with a pipeline
The following pipeline validates on all runs, shows a what-if preview, and deploys only with approval through the environment.
stages:
- stage: Validate
jobs:
- job: Check
steps:
- script: az bicep lint --file infra/main.bicep
displayName: Lint
- task: AzureCLI@2
displayName: Preview changes
inputs:
azureSubscription: sc-azure-prod
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az deployment group validate -g rg-web -f infra/main.bicep \
-p infra/environments/production.bicepparam
az deployment group what-if -g rg-web -f infra/main.bicep \
-p infra/environments/production.bicepparam
- stage: Deploy
dependsOn: Validate
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
jobs:
- deployment: Infrastructure
environment: production-infra
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: AzureCLI@2
inputs:
azureSubscription: sc-azure-prod
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az deployment group create -g rg-web -f infra/main.bicep \
-p infra/environments/production.bicepparamNote that a deployment job does not check out code automatically, so the explicit checkout: self step is needed. In GitHub Actions, the azure/bicep-deploy action or Azure CLI steps give the same result. With Terraform, a pipeline usually saves the plan as an artifact and applies that exact plan after approval, so that what was reviewed is what gets deployed. The identity that deploys should use workload identity federation, with a role such as Contributor scoped as narrowly as possible.
An IaC pipeline
Reviewing infrastructure changes
A reviewer approved a small Bicep change, but the deployment recreated a production resource, causing an outage. The team wants reviewers to see what will actually happen in Azure before approving. What should you add?
Run what-if against the target environment in the pull request pipeline, and show its output to reviewers. It reports each resource that would be created, modified or deleted. The linter checks code quality and not the effect on a live environment. Validation confirms that a template is acceptable, but it does not list changes.
Why does a Deploy stage need checkout: self in a deployment job?
Show answer
Deployment jobs do not check out the repository automatically, so the template files would be absent.
Exam Tips
Remember for the exam
- The order of stages is lint, validate, what-if, deploy, then verify.
- What-if shows the predicted effect on real resources.
- Azure Verified Modules give a good starting point for standards.
- A deployment job needs checkout: self to see the repository.
- Apply the saved Terraform plan that was reviewed.