Volume 5 · Chapter 7 — Advanced .NET Cloud Native Architecture

Infrastructure as Code

Goutham Mayaluri 7 min read .NETC#Cloud NativeInfrastructure as Code

7.1 Introduction

Infrastructure as Code (IaC) means defining cloud infrastructure in version-controlled, declarative files and provisioning it through automation instead of manual portal clicks. It brings software-engineering discipline - review, versioning, testing, repeatability - to infrastructure, eliminating the configuration drift and snowflake environments that plague manually-managed estates. For cloud-native systems, IaC is also how you make infrastructure reproducible and disaster-recoverable: the environment can be rebuilt from code. (Volume 8 covers Bicep and Terraform in depth.)

7.2 Declarative and Idempotent

IaC tools are declarative: you describe the desired end state and the tool computes the changes to reach it, which makes them idempotent - applying the same definition repeatedly converges to the same result, whether creating from scratch or correcting drift. The plan/apply workflow is the safety gate:planshows exactly what will change (reviewed in a pull request), and only after approval doesapplyexecute.

sequenceDiagram
  autonumber
  participant Dev as Engineer
  participant Repo as Git (IaC)
  participant CI as Pipeline
  participant Azure as Azure
  Dev->>Repo: PR with infra change
  Repo->>CI: validate + what-if/plan (what will change)
  CI-->>Dev: plan reviewed in PR
  Dev->>Repo: approve + merge
  Repo->>CI: apply (reconcile desired vs actual)
  CI->>Azure: create/update resources

7.3 Bicep and Terraform

Bicep is Azure’s native IaC language - clean, typed, no state file to manage (Azure is the source of truth) - ideal for Azure-only estates. Terraform is cloud-agnostic with a state file you must manage carefully - suited to multi-cloud. Both provision infrastructure declaratively from version control.


// Bicep: a reusable module for an AKS-adjacent resource with managed identity.
param location string = resourceGroup().location
param name string

resource app 'Microsoft.Web/sites@2023-01-01' = {
  name: name
  location: location
  identity: { type: 'SystemAssigned' }   // managed identity - no secrets
  properties: { httpsOnly: true }
}
output principalId string = app.identity.principalId

7.4 Modules, Environments, and Landing Zones

Reusable modules encode a golden, compliant pattern for a component (a hardened VNet, an AKS cluster, a private-endpoint SQL) so every team provisions to the same secure standard. The same modules parameterized per environment produce consistent dev/test/prod. Enterprise landing zones provide a standardized, governed foundation - subscription structure, networking, identity, security policies, monitoring - onto which workloads deploy.

Senior Interview Question

Why is Infrastructure as Code important, and what does the plan/apply workflow protect against?

IaC is important because it makes infrastructure repeatable, reviewable, auditable, and reliable instead of manual and drift-prone. Defining infrastructure declaratively in version control means every change is peer-reviewed, versioned, and auditable (you can see who changed what and why, and revert); environments are reproduced identically from the same code, eliminating snowflake servers and “works in one environment” surprises; disaster recovery becomes “re-run the code” rather than hoping someone remembers the manual steps; and it’s idempotent, so re-applying converges to the desired state and corrects drift. For regulated enterprises it also provides the audit trail and consistency that compliance demands. In short, IaC applies software-engineering discipline - version control, review, testing, reuse - to infrastructure, which is what makes cloud estates manageable and reproducible at scale, rather than a collection of manually-configured, drifting resources nobody can fully recreate. The plan/apply workflow (Terraform plan/apply, or Bicep what-if then deploy) is the crucial safety gate that protects against making unreviewed, surprising, or destructive infrastructure changes.plancomputes and shows exactly which resources will be created, changed, or - critically - destroyed, without making any change, and that output is reviewed in a pull request just like code. This protects against the most dangerous failure modes: it catches a change that would delete a production database, drop data, open a firewall, or replace a resource before it executes, so a reviewer can veto it; it makes the blast radius of an infrastructure change visible and reviewable rather than opaque; and it prevents the “I ran a command and it did something unexpected” class of incidents. Only after the plan is reviewed and approved doesapplyperform precisely those shown changes. So plan/apply turns infrastructure modifications from risky, direct, opaque actions into transparent, peer-reviewed, predictable operations - it’s the mechanism that lets teams trust automation with production infrastructure, because you see and approve exactly what will happen before it happens. Together, IaC gives you repeatable, auditable, drift-free, recoverable infrastructure, and the plan/apply gate ensures changes to it are reviewed and predictable rather than surprising or destructive - which is why both are foundational to safely operating cloud infrastructure at scale.

Principal Engineer Question

How do you design an enterprise IaC practice that keeps many teams compliant without slowing them down?

I combine reusable golden modules with platform-level policy enforcement and a plan-reviewed pipeline, so teams provision infrastructure quickly on a paved road while compliance is guaranteed by construction rather than by manual review of every change. The golden modules encode compliant patterns - a VNet with the right segmentation, an AKS baseline, SQL with private endpoints and encryption, a landing-zone-conformant workload - so teams provision secure infrastructure by consuming a versioned module rather than assembling raw resources and getting security wrong; the secure configuration is the default and the easy path. Azure Policy provides a backstop that enforces non-negotiables (allowed regions, required tags, mandatory private endpoints and encryption, no public storage) regardless of what code is written, so guardrails hold even outside the modules and non-compliant deployments are blocked or auto-remediated. IaC runs through pipelines with a plan/what-if review gate so every change is seen and approved before apply, plus IaC security scanning (Checkov/tfsec/PSRule) to catch insecure configurations, remote and locked state (or Bicep, which avoids state management), and least-privilege workload identity for the pipeline rather than stored credentials. I build on a landing zone that provides the governed foundation (subscription structure, networking, identity, security baselines, monitoring) so workloads deploy onto consistent, compliant ground. I version modules so upgrades are controlled and I can roll improvements out, and I isolate state and blast radius per environment and component. Crucially, I prevent drift by closing the manual-change path: production infrastructure is not editable by hand - access is granted only via the pipeline’s identity, enforced by RBAC and policy, with break-glass access audited - and I add drift detection (scheduled plan runs alerting on divergence) so drift is caught quickly. The result is a self-service model: teams move fast using approved building blocks and their own pipelines, while compliance is guaranteed by golden modules plus policy rather than by gating every change through a central team - guardrails, not gates. The platform team owns and improves the modules, policies, landing zone, and pipeline templates; application teams self-serve within them. So the practice is: reusable golden modules for compliant-by-default provisioning, Azure Policy as an enforced backstop, plan-reviewed least-privilege pipelines with IaC scanning and managed state, a governed landing-zone foundation, versioned modules, blocked manual production changes with drift detection - delivered as a paved road so many teams stay compliant and secure while moving quickly, which is the way to reconcile governance with velocity at enterprise scale.

7.5 Banking Enterprise Scenario

A bank provisions all cloud infrastructure through IaC - Bicep for its Azure estate - built from a library of golden modules encoding compliant patterns (segmented VNets, hardened AKS baselines, private-endpoint SQL). The same modules, parameterized per environment, produce identical dev/test/prod. Every change flows through a pipeline that runs a what-if reviewed in the pull request, scans the IaC, and applies with least-privilege workload identity. Azure Policy enforces allowed regions, mandatory encryption, and required private endpoints, production is not editable by hand, and drift detection alerts on any divergence.

Chapter Revision Notes

  • IaC makes infrastructure repeatable, reviewable, auditable, and drift-free.
  • Declarative + idempotent: describe desired state, re-apply to converge and correct drift.
  • The plan/apply (what-if) gate makes changes reviewable before they happen - preventing destructive surprises.
  • Bicep is Azure-native (no state file); Terraform is multi-cloud (manage state carefully).
  • Golden modules + Azure Policy + landing zones enforce compliance while enabling self-service.