Infrastructure as Code (IaC) manages networks, virtual machines, load balancers, and connection topology in version-controlled templates instead of manual portal clicks.

I had a strong suspicion that IaC would only grow more important over time, so I used some downtime to learn it end to end. In practical terms, IaC lets you describe Azure resources in a template file, deploy them with a single command, and wire that repository into GitHub Actions so every commit updates your environment automatically.

This walkthrough covers three milestones:

  1. Building and deploying an ARM template (azuredeploy.json)
  2. Separating environment values into a parameter file (azuredeploy.parameters.json)
  3. Automating deployments on every push with GitHub Actions

How declarative IaC manages state from plan to drift#

Declarative templates describe the target state you want in Azure, and the resource manager figures out what to create, update, or reconcile when live infrastructure drifts.

Interactive State Model / Declarative IaC

Plan, Apply, and Drift Detection in one loop#

Imperative scripts list steps; declarative templates declare the destination. Switch modes to see how Git, GitHub Actions, and Azure Resource Manager keep live infrastructure aligned with code.

State diff & drift checkSOURCE OF TRUTHGit Repositoryazuredeploy.jsonparameters.jsonCI/CD RUNNERGitHub Actionson: push (master)Service PrincipalCONTROL PLANEARM EngineValidates & diffsdesired stateLIVE CLOUDRG: pipelineStorage AcctStandard_LRS
Dry-run evaluationPLANNED DIFF

Compare Git template against live Azure state before changing anything#

Azure Resource Manager reads azuredeploy.json plus azuredeploy.parameters.json and diffs them against the target resource group. No live resources are touched.

az deployment group what-if --resource-group pipeline
$ New-AzResourceGroupDeployment -WhatIf -TemplateFile azuredeploy.json -TemplateParameterFile azuredeploy.parameters.json
Scope: /subscriptions/<id>/resourceGroups/pipeline
+ Microsoft.Storage/storageAccounts/thordraperjrpipeline
location: "eastus" sku.name: "Standard_LRS" kind: "StorageV2"
Resource changes: 1 to create, 0 to modify, 0 to delete.
Declarative ARM templates turn infrastructure changes into repeatable pull-and-push workflows instead of one-off portal clicks.

Step 1: Build and deploy your first ARM template#

Start with a local Git repository, an empty Azure resource group, and a blank ARM template to verify your tooling before adding real resources.

Install three local prerequisites first:

Initialize your local Git repository:

mkdir IaC-Pipeline

cd .\IaC-Pipeline\

git init

Sign in to Azure and create a target resource group named pipeline:

Connect-AzAccount

New-AzResourceGroup `
  -Name pipeline `
  -Location "East US"

In VS Code, open a new JSON file and type arm! on the first line.

Typing arm! snippet in VS Code

Press Enter to generate the base ARM template scaffold.

Prefilled ARM template in VS Code

You can also study auto-generated templates from the Azure Portal or browse the Azure quickstart templates.

Deploy the blank template first using New-AzResourceGroupDeployment to confirm your authentication and resource group path:

$templateFile = "{provide-the-path-to-the-template-file}"
New-AzResourceGroupDeployment `
  -Name blanktemplate `
  -ResourceGroupName pipeline `
  -TemplateFile $templateFile

No resources appear yet, but you will see a successful deployment record in Azure. Now add a real resource: place a curly brace { inside the "resources" array and VS Code will prompt you with resource snippets.

VS Code ARM resource snippet list

A storage account is the cleanest first resource. Storage account names must be globally unique, 3 to 24 characters long, and lowercase letters and numbers only (my name is uncommon enough that I used it). Update the resource name, displayName, sku.name, and sku.tier.

Storage account defined in azuredeploy.json

Save the file as azuredeploy.json, set your default subscription and resource group, and deploy it:

Get-AzSubscription

$context = Get-AzSubscription -SubscriptionId {Your subscription ID}
Set-AzContext $context

Set-AzDefault -ResourceGroupName pipeline
$templateFile = "azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addstorage-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile

The variables build a dated deployment name (addstorage-MM-dd-yyyy) so each run is easy to trace in Azure deployment history.

Storage account deployment in Azure Portal

Parameterize the template for multiple environments#

Hard-coding values limits a template to one deployment. Adding parameters lets you reuse the same azuredeploy.json across dev, test, and production by passing different values at deploy time.

Adding storage SKU parameters in azuredeploy.json

With allowedValues defined for storageAccountType, you can pass the SKU directly on the command line:

$templateFile = "azuredeploy.json"
New-AzResourceGroupDeployment `
  -Name testdeployment1 `
  -TemplateFile $templateFile `
  -storageAccountType Standard_LRS

Extend that pattern to parameterize the storage account name and prefix as well:

Parameterizing storage account name and SKU

$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addOutputs-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -storageName thordraperjrpipeline `
  -storageSKU Standard_LRS

Adding storagePrefix parameter in azuredeploy.json

$templateFile = "azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addVariable-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -storagePrefix thordraper `
  -storageSKU Standard_LRS

Passing every parameter inline works for two or three flags, but it becomes error-prone as templates grow.

Step 2: Separate environment values into a parameter file#

Moving environment-specific values into azuredeploy.parameters.json keeps your deployment command short and makes each environment’s configuration auditable in Git.

Creating azuredeploy.parameters.json

Deploy using -TemplateParameterFile:

$templateFile = "azuredeploy.json"
$parameterFile="azuredeploy.parameters.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addParameterFile-"+"$today"
New-AzResourceGroupDeployment `
  -Name $deploymentName `
  -TemplateFile $templateFile `
  -TemplateParameterFile $parameterFile

The command is now clean, but you still have to trigger it manually from your workstation.

Step 3: Automate deployments with GitHub Actions#

Connecting GitHub Actions to Azure via a scoped service principal lets every commit to your repository deploy infrastructure automatically.

Create a scoped Azure service principal#

Open Azure Cloud Shell in the portal and create a service principal with Contributor permissions scoped strictly to your pipeline resource group:

projectName="GitHubActionPipeline"
location="eastus"
resourceGroupName="pipeline"
appName="http://${projectName}"

# Store the resource group ID in a variable
scope=$(az group list --query "[?contains(name, '$resourceGroupName')].id" -o tsv)

# Create the service principal with contributor rights to the resource group we just created
az ad sp create-for-rbac --name $appName --role Contributor --scopes $scope --sdk-auth

Creating the Azure service principal in Cloud Shell

Copy the JSON output from Cloud Shell. In your GitHub repository, navigate to Settings > Secrets > New repository secret, name it AZURE_CREDENTIALS, and paste the JSON output as the value.

Adding AZURE_CREDENTIALS to GitHub repository secrets

Add the GitHub Actions workflow#

Workflow files live in .github/workflows/ at the root of your repository as .yml or .yaml. You can push the file from VS Code or open the Actions tab in GitHub and select Set up a workflow yourself.

GitHub Actions set up a workflow yourself

Replace the starter main.yml with the following workflow:

name: Deploy ARM Template

on:
  push:
    branches:
      - master
env:
  AZURE_SUBSCRIPTION_ID: <<YOURSUBSCRIPTION>>   # set this to your Azure Subscription Id
  AZURE_RESOURCE_GROUP: pipeline   # set this to your target resource group

jobs:
  deploy-template:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@master

      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy ARM Template
        uses: azure/arm-deploy@v1
        with:
          scope: resourcegroup
          subscriptionId: ${{ env.AZURE_SUBSCRIPTION_ID }}
          resourceGroupName: ${{ env.AZURE_RESOURCE_GROUP }}
          template: ./azuredeploy.json
          parameters: ./azuredeploy.parameters.json

The workflow is split into three blocks:

  • name: Labels the workflow in the Actions tab.
  • on: Triggers a run whenever a push lands on master.
  • jobs: Defines deploy-template, which checks out the repo, signs in to Azure using secrets.AZURE_CREDENTIALS, and deploys azuredeploy.json with azuredeploy.parameters.json.

Commit the file directly to master. Because the trigger listens for pushes on master, the first pipeline run starts immediately.

Verify the pipeline run in Azure#

Check the Actions tab in GitHub to watch the job finish, then confirm the live deployment record in the Azure Portal.

In the Azure Portal, open Resource groups > pipeline > Deployments and verify that the latest deployment shows Succeeded. From here, adding new resources to your lab is a template edit and a git push away.