Skip to Content
Cloud Engineering Academy
10. Infrastructure as Code

Module 10: Infrastructure as Code

Define whole AWS environments as reviewable code with CloudFormation, the AWS CDK in C# and Terraform. You learn state, drift, testing and policy-as-code, then build the same banking environment twice, in CDK and in Terraform, and deploy, change, detect drift and destroy it.

DOP-C02SOA-C03SAP-C03

Assumes: .NET 10 LTS · Amazon.CDK.Lib 2.277.0 · Cdklabs.CdkNag 3.0.2 · Terraform AWS provider · Region us-east-1. Verified against official sources on 2026-11-18.

Define whole AWS environments as reviewable code with CloudFormation, the AWS CDK in C# and Terraform. You learn state, drift, testing and policy-as-code, then build the same banking environment twice, in CDK and in Terraform, and deploy, change, detect drift and destroy it.

Estimated timeAbout 20 hours (lessons 11 h · labs 5 h · exercise 4 h)
Priority for youHigh — makes deployment (priority 1) and security (priority 3) repeatable
LevelIntermediate → advanced
CertificationsDOP-C02 · SOA-C03 · SAP-C03
Cost profile🟢 synth, plan and tests locally · 🟡 short-lived deploys · 🔴 NAT Gateways and landing zones (design-only)

Why this matters in banking

Clicking resources together in the console cannot be reviewed, repeated or audited. In a bank, every security group rule, KMS key policy and IAM permission should go through a pull request so that a second person sees it and an auditor can trace it. Infrastructure as code (IaC) also lets you rebuild an environment in another Region for disaster recovery, prove that test matches production, and catch insecure settings before they ever exist. When something drifts because someone "just fixed it" in the console, IaC lets you detect it and put it back.

From .NET/Azure to AWS

You know (Microsoft/Azure)AWS equivalentWhat's different
ARM templatesAWS CloudFormation templates (JSON/YAML)Stacks, change sets and drift detection are built in
BicepAWS CDK in C#Real C# classes compiled into CloudFormation; you can unit test them with xUnit
Terraform with azurermTerraform with the AWS providerSame tool, different provider; state usually lives in S3
Deployment stacks / what-ifCloudFormation change sets / terraform planReview the diff before applying
Azure Policy for templatescdk-nag, Checkov, cfn-nag, CloudFormation GuardPolicy-as-code runs in CI before deploy
Management groups + landing zone acceleratorsAWS Organizations + AWS Control Tower + Account FactoryLanding zones are multi-account by design (see Module 15)
Template specsCloudFormation modules, CDK construct libraries, Terraform modulesReusable, versioned building blocks

Learning objectives

By the end of this module you can:

  • Write and deploy CloudFormation stacks, review change sets, detect drift and roll out to many accounts with StackSets.
  • Build infrastructure in C# with the AWS CDK using L1, L2 and L3 constructs, multiple stacks and environments.
  • Unit test CDK stacks and enforce rules with cdk-nag before synthesis reaches an account.
  • Use Terraform providers, modules and workspaces, with remote state in S3 and state locking.
  • Compare CloudFormation, CDK and Terraform for a given team and justify the choice.
  • Package reusable infrastructure modules, version them and promote them through environments.
  • Apply policy-as-code scanning (Checkov, cfn-nag) in CI, and explain how Control Tower and Account Factory automate landing zones.

Prerequisites

How the pieces fit

Lessons

  1. Why infrastructure as code — reviewability, repeatability, audit trails and the cost of console-built environments.
  2. AWS CloudFormation fundamentals — templates, parameters, outputs, stacks, change sets, rollback behaviour and deletion policies.
  3. Drift detection and StackSets — finding manual changes and deploying baselines across accounts and Regions.
  4. AWS CDK in C#: your first stack — project layout, the construct tree, cdk synth, cdk diff and cdk deploy.
  5. CDK constructs: L1, L2 and L3 — raw resources vs opinionated defaults vs patterns, and writing your own construct library.
  6. CDK stacks, environments and configuration — multiple stacks, cross-stack references, per-environment settings and bootstrapping.
  7. Testing CDK — assertion and snapshot tests in xUnit, plus cdk-nag rule packs and justified suppressions.
  8. Terraform fundamentals — providers, resources, variables, outputs and the plan/apply workflow.
  9. Terraform state — remote state in S3, encryption, locking, workspaces and importing existing resources.
  10. Terraform modules — reusable modules, versioning, registry modules and when not to wrap a single resource.
  11. Lab: one environment, two tools — the module project: the same NAT-free banking environment in CDK and Terraform, deployed, changed, drift-checked and destroyed.
  12. CloudFormation vs CDK vs Terraform — a decision table for banking teams, including multi-cloud and skills considerations.
  13. Environment configuration and versioning — promoting the same code through dev, test and prod; tagging releases.
  14. Policy-as-code and secure deployment — Checkov, cfn-nag, CloudFormation Guard, least-privilege deploy roles and protecting stateful resources.
  15. Landing-zone automation overview — Control Tower, Account Factory and customisations (built fully in Module 15).

When you finish, keep the Module 10 cheat sheet handy for revision.

Labs and module project

The hands-on work sits inside the lessons (each labelled cost: free or credits, with free local alternatives) and in one capstone lab.

Module project: Lab: one environment, two tools (🟡 credits, about $0.20 per hour while deployed). Define an entire application environment as code (networking, compute on ECS Fargate, database, IAM, secrets, monitoring and alarms) in both CDK (C#) and Terraform. The design is NAT-free, using VPC endpoints for ECR, S3, CloudWatch Logs and Secrets Manager, and the lab documents the cost and security trade-off against a NAT Gateway. Deploy, make a change through a reviewed diff, detect drift, and destroy everything. Then write an ADR choosing one tool for your team.

The code is in labs/m10-environment-two-tools/: a CDK app in infra/cdk/, Terraform in infra/terraform/, a C# drift-report tool in src/, and 50 xUnit v3 tests in tests/.

The lab tests need Node.js for the CDK part

The CDK for .NET runs its construct library in Node.js, so the lab's CDK tests need Node.js 22 or 24 on PATH. Without Node, 22 of the 50 tests pass and 28 are skipped. With Node installed and CI=true set, all 50 run and a missing Node fails the build instead of skipping silently. No AWS account is needed for any test.

Cost profile

PartLabelFree alternative
CloudFormation, CDK and Terraform themselves🟢 freeThe tools cost nothing; you pay only for what they deploy
cdk synth, terraform plan, unit tests and policy scans🟢 freeRun locally or in GitHub Actions
S3 state bucket, KMS key, small stacks🟡 creditsA customer managed KMS key costs about $1 per month — schedule deletion after the module
Full environment (Fargate, ALB, RDS, interface endpoints)🟡 creditsDeploy, verify and destroy within an hour; interface endpoints also bill hourly
NAT Gateway🔴 paidUse VPC endpoints instead, or keep the NAT version design-only
Control Tower landing zone🔴 paidDesign-only here; a lite landing zone is built in Module 15

Certification alignment

ExamDomains (weight)What this module covers
DOP-C02Config & IaC (17%) · SDLC (22%)CloudFormation, CDK, StackSets, drift, testing infrastructure
SOA-C03Deploy & Automation (22%)Provisioning and automating resources with CloudFormation
SAP-C03Ops & AutomationMulti-account deployment and landing-zone automation

The domain labels are short forms; see each exam page for the official wording.

Service availability notes

Tooling notes — verified 2026-11-17

Versions used in labs: Amazon.CDK.Lib 2.277.0 and Cdklabs.CdkNag 3.0.2 (NuGet).

Terraform's S3 backend supports native state locking with a lock file in the bucket (use_lockfile), and DynamoDB-based locking is deprecated ⚠ VERIFY against the current Terraform S3 backend documentation before the lab.

CloudFormation, CDK and Control Tower are fully available.

Learning shortcut — not for production

Labs deploy from your workstation with an SSO profile. In a bank, production infrastructure changes should only be applied by a pipeline role after an approved pull request, never from a laptop.

Key AWS services

AWS CloudFormation (stacks, change sets, drift detection, StackSets, Guard) · AWS Cloud Development Kit (C#) · Terraform with the AWS provider (third-party) · Amazon S3 (remote state) · AWS KMS · AWS IAM · AWS Organizations · AWS Control Tower + Account Factory · cdk-nag · Checkov and cfn-nag (open source)

Independent learning resource — not affiliated with, endorsed or sponsored by Amazon Web Services. AWS and related marks are trademarks of Amazon.com, Inc. or its affiliates. All banking data in examples is fictional. Sample architectures are for learning and are not automatically compliant with GDPR, PCI DSS or any banking regulation.

On this page