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.
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 time | About 20 hours (lessons 11 h · labs 5 h · exercise 4 h) |
| Priority for you | High — makes deployment (priority 1) and security (priority 3) repeatable |
| Level | Intermediate → advanced |
| Certifications | DOP-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 equivalent | What's different |
|---|---|---|
| ARM templates | AWS CloudFormation templates (JSON/YAML) | Stacks, change sets and drift detection are built in |
| Bicep | AWS CDK in C# | Real C# classes compiled into CloudFormation; you can unit test them with xUnit |
| Terraform with azurerm | Terraform with the AWS provider | Same tool, different provider; state usually lives in S3 |
| Deployment stacks / what-if | CloudFormation change sets / terraform plan | Review the diff before applying |
| Azure Policy for templates | cdk-nag, Checkov, cfn-nag, CloudFormation Guard | Policy-as-code runs in CI before deploy |
| Management groups + landing zone accelerators | AWS Organizations + AWS Control Tower + Account Factory | Landing zones are multi-account by design (see Module 15) |
| Template specs | CloudFormation modules, CDK construct libraries, Terraform modules | Reusable, 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
- Module 2: Networking — VPCs, subnets, endpoints and the NAT Gateway trade-off
- Module 3: .NET on AWS — the API and container you will deploy
- Module 5: Security — IAM policies, KMS keys and secrets
- Helpful: Module 9: DevOps to run plans and deploys from a pipeline
How the pieces fit
Lessons
- Why infrastructure as code — reviewability, repeatability, audit trails and the cost of console-built environments.
- AWS CloudFormation fundamentals — templates, parameters, outputs, stacks, change sets, rollback behaviour and deletion policies.
- Drift detection and StackSets — finding manual changes and deploying baselines across accounts and Regions.
- AWS CDK in C#: your first stack — project layout, the construct tree,
cdk synth,cdk diffandcdk deploy. - CDK constructs: L1, L2 and L3 — raw resources vs opinionated defaults vs patterns, and writing your own construct library.
- CDK stacks, environments and configuration — multiple stacks, cross-stack references, per-environment settings and bootstrapping.
- Testing CDK — assertion and snapshot tests in xUnit, plus cdk-nag rule packs and justified suppressions.
- Terraform fundamentals — providers, resources, variables, outputs and the plan/apply workflow.
- Terraform state — remote state in S3, encryption, locking, workspaces and importing existing resources.
- Terraform modules — reusable modules, versioning, registry modules and when not to wrap a single resource.
- Lab: one environment, two tools — the module project: the same NAT-free banking environment in CDK and Terraform, deployed, changed, drift-checked and destroyed.
- CloudFormation vs CDK vs Terraform — a decision table for banking teams, including multi-cloud and skills considerations.
- Environment configuration and versioning — promoting the same code through dev, test and prod; tagging releases.
- Policy-as-code and secure deployment — Checkov, cfn-nag, CloudFormation Guard, least-privilege deploy roles and protecting stateful resources.
- 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
| Part | Label | Free alternative |
|---|---|---|
| CloudFormation, CDK and Terraform themselves | 🟢 free | The tools cost nothing; you pay only for what they deploy |
cdk synth, terraform plan, unit tests and policy scans | 🟢 free | Run locally or in GitHub Actions |
| S3 state bucket, KMS key, small stacks | 🟡 credits | A customer managed KMS key costs about $1 per month — schedule deletion after the module |
| Full environment (Fargate, ALB, RDS, interface endpoints) | 🟡 credits | Deploy, verify and destroy within an hour; interface endpoints also bill hourly |
| NAT Gateway | 🔴 paid | Use VPC endpoints instead, or keep the NAT version design-only |
| Control Tower landing zone | 🔴 paid | Design-only here; a lite landing zone is built in Module 15 |
Certification alignment
| Exam | Domains (weight) | What this module covers |
|---|---|---|
| DOP-C02 | Config & IaC (17%) · SDLC (22%) | CloudFormation, CDK, StackSets, drift, testing infrastructure |
| SOA-C03 | Deploy & Automation (22%) | Provisioning and automating resources with CloudFormation |
| SAP-C03 | Ops & Automation | Multi-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.
Module 9 cheat sheet
One-page revision summary for DevOps & CI/CD: services, Azure DevOps mapping, commands, limits and defaults, decision rules, exam traps and cost traps.
Why infrastructure as code
Explain why a bank builds AWS environments from reviewed code instead of console clicks, name the three tools you will use in this module, and run a read-only inventory that shows how much of an account is not under code.