Volume 9 · Chapter 4 — CI/CD, DevOps and Infrastructure as Code

Infrastructure as Code

Goutham Mayaluri 8 min read AWSCloudCI/CDInfrastructure as Code

4.1 Introduction

Just as application code flows through a pipeline, infrastructure should be defined in code and provisioned through automation - not clicked together by hand. Infrastructure as code (IaC) is how mature teams create and manage AWS infrastructure reliably, reproducibly, and auditably. Introduced in Volume 1, it deserves a full treatment here because it’s central to DevOps and a frequent interview topic. This chapter covers what IaC is, the AWS-native and cross-cloud tools, the plan/apply safety workflow, and reusable, environment-consistent infrastructure.

4.2 What IaC Is and Why It Matters

Infrastructure as code means defining your infrastructure - networks, servers, databases, permissions - in declarative, version-controlled files that a tool provisions and updates automatically, instead of configuring it manually in the console. The benefits mirror those of application code discipline: every infrastructure change is peer-reviewed and version-controlled (you know who changed what and can revert); environments are reproduced identically from the same code, eliminating configuration drift and “works in staging” surprises; the whole environment can be rebuilt from code, making disaster recovery a matter of re-running templates; and it’s idempotent, so re-applying converges to the desired state and corrects drift. For a regulated bank, IaC also provides the audit trail, consistency, and control that compliance demands - which is why production infrastructure is defined in code, not clicked together manually.

4.3 CloudFormation, CDK, and Terraform

There are a few main IaC tools. CloudFormation is AWS’s native IaC service - you describe resources in declarative templates (JSON/YAML) and it provisions and manages them, with no separate state to maintain since AWS tracks it. The CDK (Cloud Development Kit) lets you define infrastructure in real programming languages, generating CloudFormation under the hood - convenient for developers who prefer code with loops and abstractions. Terraform is the popular cloud-agnostic alternative that works across AWS and other clouds, using its own declarative language and a state file you must manage carefully. The choice: CloudFormation or CDK for AWS-native provisioning (no state management), Terraform when you want a single tool across multiple clouds or prefer its ecosystem - both provision infrastructure declaratively from version control.


# CloudFormation: declarative desired state, reviewed in a PR, applied repeatably.
Resources:
  AppSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: App tier - least privilege
      VpcId: !Ref BankVpc
      SecurityGroupIngress:
        - IpProtocol: tcp        # only from the load balancer's SG
          FromPort: 443
          ToPort: 443
          SourceSecurityGroupId: !Ref LoadBalancerSecurityGroup

4.4 The Plan/Apply Safety Workflow

The critical safety mechanism in IaC is the plan/apply workflow. Before making changes, the tool computes and shows exactly what will be created, changed, or - crucially - destroyed (Terraform’s plan, CloudFormation’s change sets or what-if), without making any change. That output is reviewed, ideally in a pull request just like code, and only after approval does apply execute precisely those shown changes. This protects against the most dangerous failure modes: it catches a change that would delete a production database, drop data, or open a firewall before it happens, so a reviewer can veto it, and it makes the blast radius of an infrastructure change visible and reviewable rather than opaque. Plan/apply turns infrastructure modifications from risky, direct, opaque actions into transparent, peer-reviewed, predictable operations - which is what lets teams trust automation with production infrastructure.

4.5 Reusable Modules and Consistent Environments

IaC becomes powerful with reusable modules and parameterization. You encode a compliant, standardized pattern for a component - a hardened network, a database with encryption and private endpoints - as a reusable module, so every team provisions to the same secure standard rather than assembling raw resources and getting security wrong. The same modules and templates, parameterized per environment, produce identical development, staging, and production environments - which is exactly what eliminates the “works in one environment” class of problems and makes staging a faithful preview of production. For a bank, reusable modules also enforce compliance by construction (encryption on by default, no public access, correct network placement), and running everything through the IaC pipeline keeps every environment consistent and every change reviewed.

4.6 Banking Production Scenario

A bank provisions all its AWS infrastructure through IaC - using CloudFormation (with the CDK where teams prefer code) - built from a library of reusable modules encoding compliant patterns: segmented networks, hardened databases with encryption and private endpoints, least-privilege security groups. The same modules, parameterized per environment, produce identical dev, staging, and production, so staging faithfully previews production. Every infrastructure change flows through a pipeline that runs a plan/what-if reviewed in a pull request before apply, so no change - especially a destructive one - happens unreviewed. Production infrastructure is never edited by hand; changes go through the reviewed pipeline, producing a full audit trail. Compliance controls like mandatory encryption are baked into the modules, enforced by construction across every environment.

4.7 Troubleshooting: IaC Problems

IaC issues usually involve drift, review gaps, or inconsistency. Environments differing despite “the same” setup means they weren’t all provisioned from the same code, or someone made manual changes - provision everything from IaC and prohibit manual changes. A destructive change slipping through means the plan/what-if wasn’t reviewed before apply - always review the plan in a PR. Live infrastructure diverging from its code (“drift”) means manual changes were made outside the pipeline - route all changes through IaC and run drift detection. Teams provisioning insecure infrastructure means they’re assembling raw resources instead of using compliant modules - provide golden modules. Terraform state problems mean the state file isn’t managed carefully (remote, locked) - manage it properly, or use CloudFormation which avoids separate state. The discipline is all infrastructure in version- controlled IaC, plan-reviewed changes through a pipeline, reusable compliant modules, and no manual production changes.

Interview Question

What is infrastructure as code, and what does the plan/apply workflow protect against?

Infrastructure as code means defining your infrastructure - networks, servers, databases, permissions - in declarative, version-controlled files that a tool provisions and updates automatically, instead of configuring it manually in the console. It matters because it applies software-engineering discipline to infrastructure: every change is peer-reviewed and version-controlled so you know who changed what and can revert; environments are reproduced identically from the same code, eliminating configuration drift and “works in staging but not production” problems; the whole environment can be rebuilt from code, making disaster recovery a matter of re-running templates; and it’s idempotent, so re-applying converges to the desired state and corrects drift. For a regulated bank it also provides the audit trail, consistency, and control compliance requires, which is why production infrastructure is defined in code rather than clicked together. The main tools are CloudFormation (AWS’s native declarative service, no separate state to manage), the CDK (define infrastructure in real programming languages, generating CloudFormation), and Terraform (cloud-agnostic, with a state file you manage carefully). The plan/apply workflow is the critical safety mechanism, and it protects against making unreviewed, surprising, or destructive infrastructure changes. Before making any change, the tool computes and shows exactly which resources will be created, changed, or - most importantly - destroyed, without actually changing anything; this is Terraform’s plan or CloudFormation’s change sets and what-if. That output is reviewed, ideally in a pull request just like code, and only after approval does apply execute precisely those shown changes. 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 and destructive” class of incidents. So plan/apply turns infrastructure changes from risky, direct, opaque actions into transparent, peer-reviewed, predictable operations - you see and approve exactly what will happen before it happens, which is what lets teams trust automation with production infrastructure. Combined with reusable modules that encode compliant patterns so every environment is provisioned securely and identically, IaC gives you reproducible, auditable, drift-free infrastructure, and the plan/apply gate ensures changes to it are reviewed and predictable rather than surprising or destructive - which is exactly what a bank needs to manage infrastructure safely at scale.

Chapter Revision Notes

  • IaC defines infrastructure in declarative, version-controlled files provisioned automatically - reproducible, reviewable, auditable, idempotent.
  • Tools: CloudFormation (AWS-native, no state to manage), CDK (real languages → CloudFormation), Terraform (cloud-agnostic, manage state carefully).
  • Plan/apply (what-if) shows exactly what will be created/changed/destroyed before applying - reviewed in a PR - preventing destructive surprises.
  • Reusable modules encode compliant patterns so every team/environment provisions securely and identically.
  • Route all infrastructure changes through reviewed IaC pipelines; prohibit manual production changes and run drift detection.