Skip to Content
Cloud Engineering Academy
10. Infrastructure as Code

Lab: one environment, two tools

Define the same NAT-free Fargate and PostgreSQL environment twice, in CDK (C#) and in Terraform, run both test suites without an account, deploy for under an hour, change it through a reviewed diff, detect drift with a tested C# report, destroy everything and write the ADR that picks one tool.

advanced⏱ 4 h 🟡 Low costDOP-C02SOA-C03SAP-C03

Assumes: .NET 10 LTS · Amazon.CDK.Lib 2.277.0 · Cdklabs.CdkNag 3.0.2 · Terraform 1.17 · AWS provider ~> 6.73 · AWS CLI v2 · Node.js 22 LTS · Region us-east-1 · labs/m10-environment-two-tools. Verified against official sources on 2026-11-18.

This is the Module 10 project. You take one realistic application environment (a private ECS Fargate service, an encrypted PostgreSQL database, secrets, logging and alarms, with no NAT gateway) and build it twice: once with the AWS CDK in C#, once with Terraform. Then you do the things that separate infrastructure code from a diagram: run the tests, read the diff, deploy, change it, let someone "fix it in the console", detect the drift, and destroy it all.

The code is in labs/m10-environment-two-tools in this repository. Its README.md is the short version of this page.

Learning objectives

By the end of this lab you can:

  • Explain the NAT-free network design, and the cost and security trade-off against a NAT gateway.
  • Run the CDK tests (including cdk-nag and a resource-count snapshot) and the Terraform tests without any AWS account.
  • Deploy both versions in two stages (foundation, then application), push an image to a private ECR repository with immutable tags, and verify the service.
  • Make a change through a reviewed diff in each tool and say what each diff shows.
  • Introduce drift on purpose, detect it with CloudFormation drift detection and terraform plan -refresh-only, and merge both results into one report with exit codes.
  • Destroy both environments, prove nothing tagged for the lab is left, and write an architecture decision record (ADR) choosing one tool for a team.

Prerequisites

Cost, in one place

Part A is free and needs no account. Parts B to F deploy two environments for under an hour each. Expect about 0.15 to 0.20 US dollars per hour per environment (four interface endpoints in two Availability Zones, an Application Load Balancer, a small RDS instance, one Fargate task) ⚠ VERIFY against current prices. Destroy both within the hour. Details and teardown are below.

Why this matters in banking

Two different teams in the same bank will pick two different tools, and both will be right for their context. A platform engineer who has built the same environment in both can read either team's code review, can explain the trade-offs to a risk committee, and can write the ADR that says why this team uses this tool. This lab is that experience compressed into an afternoon.

The environment itself is a pattern banks ask for: no route to the internet from the workload. Fargate tasks and the database sit in subnets that have no NAT gateway and no internet gateway route. They reach AWS APIs only through VPC endpoints. A compromised container cannot call out to a command-and-control server, because there is no path. That is a control a security architect can point to, and a test can prove.

The idea in plain language

You build a small branch office for the Accounts API:

  • The front desk (an Application Load Balancer in public subnets) accepts HTTP only from your IP address.
  • The back office (Fargate tasks in private "app" subnets) accepts traffic only from the front desk. It has no door to the street. Its only phone lines go to AWS services through endpoints.
  • The vault (PostgreSQL in "data" subnets) accepts connections only from the back office.
  • A key (customer managed KMS key) encrypts the database, its secret, the log groups and the alarm topic.
  • Five alarms ring an encrypted topic when the service is unhealthy or the database is stressed.

The code is split into two stacks (or two parts in Terraform): a foundation (network, key, registry) that rarely changes, and the application that changes on every release.

Analogy: the foundation is the building, poured once and inspected carefully. The application is the office fit-out, changed often. You can refit an office without re-pouring the foundation.

What problem the design solves

NeedHow the design meets it
No outbound internet from the workloadNo NAT gateway; isolated subnets; endpoints for ECR (two), CloudWatch Logs, Secrets Manager; an S3 gateway endpoint for image layers
Least privilege between tiersSecurity groups reference each other (ALB to task to database), never "anything in the VPC"
Data encrypted at restOne customer managed key for RDS storage, the database secret, logs and the alarm topic
No secrets in code or settingsRDS generates the password into Secrets Manager; the task definition references it
Know when it breaksFive alarms to an encrypted SNS topic that refuses plain HTTP
Immutable releasesECR tags are immutable; latest is refused by validation
Prove itTests over the synthesized template (CDK) and the plan with a mock provider (Terraform)

The cost and security trade-off against a NAT gateway

Prices below are us-east-1 list prices at the time of writing ⚠ VERIFY on the pricing pages before relying on them.

OptionHourly cost (two AZs)Per-GB costSecurity posture
NAT gateway (one per AZ)2 x 0.045 = 0.09 USD0.045 USD processedWorkload can reach any internet address; you control it with security groups and a firewall
Interface endpoints (this lab: 4 services x 2 AZs)8 x 0.01 = 0.08 USD0.01 USD processedWorkload can reach only the named AWS services; no internet route exists
S3 gateway endpointFreeFreeImage layers from ECR travel inside the AWS network

The honest finding: at this size the endpoints are not cheaper than NAT gateways (roughly 58 US dollars per month for four interface endpoints in two AZs, against roughly 66 US dollars for two NAT gateways, before data charges). They win on per-GB cost at volume and, above all, on posture: with a NAT gateway a compromised task can exfiltrate to anywhere. With endpoints it cannot, and you can say so in an audit. Endpoints lose when the workload needs a third-party SaaS API on the internet: then you need NAT or a proxy anyway (NAT gateway and egress covers the design choices, and segmentation and egress control the firewall option).

Microsoft / Azure comparison

You know (Microsoft/Azure)AWS equivalent (this lab)What's different
Container Apps or AKS in a VNet with Private EndpointsFargate in isolated subnets with VPC interface endpointsAn endpoint is an ENI per AZ with a private DNS name
Azure Firewall for egressNone needed: no route outFewer moving parts, less flexibility
Key Vault with customer managed keysKMS customer managed keyKey policy plus IAM; grants across stacks need care
Azure Database for PostgreSQL Flexible ServerRDS for PostgreSQLRDS can generate and rotate the master password into Secrets Manager
Bicep + Terraform in one estateCDK (C#) + TerraformThe CDK produces CloudFormation; Terraform keeps its own state
Azure Monitor alerts + action groupsCloudWatch alarms + SNS topicThe topic is encrypted and denies plain HTTP

Architecture

Security-boundary view. Trust zones are the three subnet tiers. Allowed flows are: your IP to the ALB (port 80), the ALB to tasks (container port), tasks to the database (5432), and tasks to the endpoints (443). Everything else is absent from the route tables or the security groups, which the tests assert. Logging: VPC flow logs (all traffic) and application logs go to KMS-encrypted log groups with retention.

The two-stage deployment

The application stack needs an image that already exists, so the foundation is deployed first without an image tag. Each tool refuses to build the application part until you pass imageTag.

Step-by-step implementation

Part A: prove it locally (free, no account)

Run both test suites. This is the most valuable part of the lab.

Part B: deploy the CDK version

Foundation, push an image, then the application stack.

Part C: make a reviewed change

Lower one alarm threshold through code, read the diff, deploy.

Part D: cause and detect drift

Change the environment behind the tool's back, detect it, and report it.

Part E: the Terraform version

The same environment, the same changes, a different tool.

Part F: destroy, verify and decide

Tear both down, prove nothing is left, and write the ADR.

AWS Console instructions

You can do everything from the CLI, but these console views are worth opening during the lab:

  • Console → VPC → Your VPCs / Route tables: select an app or data subnet's route table. It has exactly one route (local). The public subnets have a route to the internet gateway.
  • Console → VPC → Endpoints: four interface endpoints plus one gateway endpoint. Open one and see the ENIs in the app subnets and the private DNS name.
  • Console → CloudFormation → Stacks (CDK version): the two stacks and, on the Stack actions menu, Detect drift.
  • Console → ECS → Clusters → your cluster → Services: the service, its tasks, and the Deployments tab with the circuit breaker.
  • Console → EC2 → Security groups: the ALB group has one ingress rule (your /32). Open the Outbound rules to see that nothing is "all traffic".
  • Console → CloudWatch → Alarms: five alarms per environment, all actions pointing at the SNS topic.

Console labels change from time to time ⚠ VERIFY.

AWS CLI

Part A: run the tests (free)

The CDK tests need Node.js 22 or 24. Without it they are skipped (not failed) on your machine; set CI=true to make a missing Node.js a failure.

cd labs/m10-environment-two-tools

# with the .NET 10 SDK and Node.js installed
dotnet test --solution EnvironmentTwoTools.slnx

Or with only Docker (Node.js installed inside the container, and your own NuGet cache volume):

MSYS_NO_PATHCONV=1 docker run --rm --memory 3g \
  -v "$PWD:/src" -v cea-nuget-m10:/root/.nuget/packages -w /src mcr.microsoft.com/dotnet/sdk:10.0 \
  sh -c "curl -fsSL https://nodejs.org/dist/v22.12.0/node-v22.12.0-linux-x64.tar.gz | tar -xz -C /usr/local --strip-components=1 \
         && CI=true dotnet test --solution EnvironmentTwoTools.slnx; \
         find . -type d \( -name bin -o -name obj -o -name TestResults \) -prune -exec rm -rf {} +"

Expected result on the course machine: 50 tests, 50 passed with Node.js 22. Without Node.js: 22 passed and 28 skipped (the 28 are the CDK synthesis tests). What the 50 cover:

SuiteTestsWhat it proves
Settings and profilespure C#Bad CIDRs, latest tags, placeholder owner and unknown environments are refused; dev, test and prod differ only in configuration
NetworkCDK synthesisNo NAT gateway; app and data route tables have only the local route; four interface endpoints with private DNS; S3 endpoint policy allows only the ECR layer bucket; tasks have no public IP; flow logs capture all traffic
SecurityCDK synthesisDatabase private, encrypted with the data key, IAM auth on; PostgreSQL reachable only from the task security group; nothing open to the whole internet; credentials reach the container only as secrets; the task role has no permissions; every log group and the topic are encrypted
OperationsCDK synthesisFive alarms notify on alarm and on recovery; dev is cheap and destroyable; prod uses multi-AZ, deletion protection and retention; failed deployments roll back; stacks are linked through exports; resources carry the lab tags
Snapshot and cdk-nagCDK synthesisResource counts per type match a committed file; zero unacknowledged AWS Solutions findings in dev and prod; a bad bucket really is flagged
Drift reportpure C#CloudFormation and Terraform fixtures parse into one model; security-relevant drift is ranked first and exits with code 3

Terraform's own tests, also with no account (the AWS provider is mocked):

cd labs/m10-environment-two-tools/infra/terraform
terraform init -backend=false
terraform validate
terraform test          # expect: Success! 6 passed, 0 failed.

Or in Docker: docker run --rm -v "$PWD:/tf" -w /tf --entrypoint sh hashicorp/terraform:1.17 -c "terraform init -backend=false && terraform test".

Part B: deploy the CDK version

Replace <you> with your name and <ip> with your public IP (curl https://checkip.amazonaws.com).

cd labs/m10-environment-two-tools/infra/cdk

# 1. Foundation: key, network, ECR (read the diff first)
cdk diff   hl-m10-cdk-dev-foundation -c owner=<you> --profile learn-sandbox
cdk deploy hl-m10-cdk-dev-foundation -c owner=<you> --profile learn-sandbox --outputs-file foundation-outputs.json

# 2. Push an arm64 image tagged 1.0.0 to the lab's ECR repository (immutable tags)
ACCOUNT=$(aws sts get-caller-identity --profile learn-sandbox --query Account --output text)
REGISTRY=$ACCOUNT.dkr.ecr.us-east-1.amazonaws.com
aws ecr get-login-password --profile learn-sandbox | docker login --username AWS --password-stdin $REGISTRY
docker pull --platform linux/arm64 nginxinc/nginx-unprivileged:stable
docker tag  nginxinc/nginx-unprivileged:stable $REGISTRY/hl-m10-cdk-dev-accounts-api:1.0.0
docker push $REGISTRY/hl-m10-cdk-dev-accounts-api:1.0.0

# 3. Application stack
cdk diff   hl-m10-cdk-dev-app -c owner=<you> -c imageTag=1.0.0 -c allowedIngressCidr=<ip>/32 --profile learn-sandbox
cdk deploy hl-m10-cdk-dev-app -c owner=<you> -c imageTag=1.0.0 -c allowedIngressCidr=<ip>/32 --profile learn-sandbox --outputs-file app-outputs.json

The container is a stand-in web server that listens on port 8080 and answers 200 on / (nginx-unprivileged runs as a non-root user and listens on 8080; ⚠ VERIFY the image tag and arm64 support on Docker Hub). The lab is about the environment, not the application. To run the real Accounts API from Module 3 instead you must give it a PostgreSQL connection and change the health check path; that is a good extension, not a requirement.

Verify:

API=$(python -c "import json;print(json.load(open('app-outputs.json'))['hl-m10-cdk-dev-app']['ApiUrl'])")
curl -i "$API"                                   # expect HTTP 200 from your IP only
aws ecs describe-services --cluster hl-m10-cdk-dev --services hl-m10-cdk-dev-accounts-api --profile learn-sandbox \
  --query "services[0].[runningCount,desiredCount,deployments[0].rolloutState]"

The first deployment takes about 10 to 15 minutes, mostly the RDS instance and the interface endpoints. If python is not available, read ApiUrl from the CloudFormation outputs: aws cloudformation describe-stacks --stack-name hl-m10-cdk-dev-app --query "Stacks[0].Outputs".

Part C: a reviewed change

Edit AppStack.cs and change the ServiceCpuHigh alarm threshold from 80 to 70 (and the description). Then:

cdk diff hl-m10-cdk-dev-app -c owner=<you> -c imageTag=1.0.0 -c allowedIngressCidr=<ip>/32 --profile learn-sandbox

Expect exactly one [~] on an AWS::CloudWatch::Alarm, changing Threshold from 80 to 70. If the diff shows anything else, stop and find out why before deploying. Run the tests again (dotnet test): the five-alarm test still passes. Deploy.

Part D: cause and detect drift (CDK version)

Change the environment out of band, the way a hurried engineer would:

# 1. open the ALB security group to the whole internet "for a minute"
ALB_SG=$(aws ec2 describe-security-groups --profile learn-sandbox \
  --filters Name=group-name,Values=hl-m10-cdk-dev-alb --query "SecurityGroups[0].GroupId" --output text)
aws ec2 authorize-security-group-ingress --group-id "$ALB_SG" --protocol tcp --port 80 --cidr 0.0.0.0/0 --profile learn-sandbox

# 2. scale the service by hand during an imaginary incident
aws ecs update-service --cluster hl-m10-cdk-dev --service hl-m10-cdk-dev-accounts-api --desired-count 3 --profile learn-sandbox

Detect and report:

ID=$(aws cloudformation detect-stack-drift --stack-name hl-m10-cdk-dev-app --profile learn-sandbox \
  --query StackDriftDetectionId --output text)
aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id "$ID" --profile learn-sandbox \
  --query "[DetectionStatus,StackDriftStatus,DriftedStackResourceCount]"          # repeat until DETECTION_COMPLETE

aws cloudformation describe-stack-resource-drifts --stack-name hl-m10-cdk-dev-app \
  --stack-resource-drift-status-filters MODIFIED DELETED --profile learn-sandbox --output json > cfn-drift.json

cd ../..   # back to labs/m10-environment-two-tools
dotnet run --project src/Harbourline.Environment.DriftReport -- --env dev --cfn infra/cdk/cfn-drift.json
echo "exit code: $?"

Expect two findings: the security group (SecurityGroupIngress gained 0.0.0.0/0, security-relevant) and the ECS service (DesiredCount 1 versus 3, operational). The report lists the security finding first and the exit code is 3. Compare with tests/Harbourline.Environment.DriftReport.Tests/fixtures/cfn-drift.json: the fixture is a real-shaped copy of this output.

Fix it: remove the rule (aws ec2 revoke-security-group-ingress ...) and set the desired count back, or redeploy through a drift-aware change set (Drift detection). Detect again and confirm IN_SYNC.

Part E: the Terraform version

You need the S3 state bucket from the Terraform state lesson. If it is gone, recreate it there first (versioning, encryption, public access blocked).

cd labs/m10-environment-two-tools/infra/terraform
cp terraform.tfvars.example terraform.tfvars      # set owner and allowed_ingress_cidr
cp backend.hcl.example backend.hcl                # set bucket; keep use_lockfile = true
terraform init -backend-config=backend.hcl
terraform plan                                    # foundation only: image_tag is empty
terraform apply

Push the image to the Terraform repository (named hl-m10-tf-dev-accounts-api), set image_tag = "1.0.0" in terraform.tfvars, read the plan and apply again:

REPO=$(terraform output -raw repository_url)
aws ecr get-login-password --profile learn-sandbox | docker login --username AWS --password-stdin "${REPO%%/*}"
docker tag nginxinc/nginx-unprivileged:stable "$REPO:1.0.0"
docker push "$REPO:1.0.0"

terraform plan -out=app.tfplan                    # read it, then:
terraform apply app.tfplan
curl -i "$(terraform output -raw api_url)"        # HTTP 200 from your IP

Repeat the reviewed change from Part C: in modules/accounts-app/monitoring.tf change the service-cpu-high threshold from 80 to 70. terraform plan shows one ~ update in-place on aws_cloudwatch_metric_alarm.this["service-cpu-high"] (threshold: 80 -> 70).

Cause drift on the Terraform environment, then detect it:

# revoke the managed ALB ingress rule, and change the desired count
ALB_SG=$(aws ec2 describe-security-groups --profile learn-sandbox   --filters Name=group-name,Values=hl-m10-tf-dev-alb --query "SecurityGroups[0].GroupId" --output text)
RULE=$(aws ec2 describe-security-group-rules --profile learn-sandbox --filters Name=group-id,Values=$ALB_SG   --query "SecurityGroupRules[?IsEgress==\`false\`].SecurityGroupRuleId" --output text)
aws ec2 revoke-security-group-ingress --group-id "$ALB_SG" --security-group-rule-ids $RULE --profile learn-sandbox
aws ecs update-service --cluster hl-m10-tf-dev --service hl-m10-tf-dev-accounts-api --desired-count 3 --profile learn-sandbox

terraform plan -refresh-only -json > tf-drift.jsonl
cd ../..
dotnet run --project src/Harbourline.Environment.DriftReport -- --env dev --tf infra/terraform/tf-drift.jsonl

Two things to notice. First, Terraform reports that a resource drifted (aws_ecs_service.api updated, aws_vpc_security_group_ingress_rule.alb_http deleted), but not the individual attribute values, while CloudFormation lists property differences. Second, Terraform models each security group rule as its own resource, so a new rule added by hand (as in Part D) is not visible to a refresh-only plan at all: Terraform only knows about rules it manages. CloudFormation, which checks the security group's declared ingress list, saw it. How a tool models a resource decides what drift it can find, and that belongs in your ADR.

Restore with terraform apply (it recreates the deleted rule and resets the desired count).

Part F: destroy and verify

Destroy both environments the same day. Destroy the application before the foundation.

# CDK
cd labs/m10-environment-two-tools/infra/cdk
cdk destroy hl-m10-cdk-dev-app hl-m10-cdk-dev-foundation -c owner=<you> -c imageTag=1.0.0 -c allowedIngressCidr=<ip>/32 --profile learn-sandbox

# Terraform
cd ../terraform
terraform destroy

Then verify nothing tagged for the lab is left, in every Region you used:

aws resourcegroupstaggingapi get-resources --profile learn-sandbox --region us-east-1 \
  --tag-filters Key=Lab,Values=m10-env --query "ResourceTagMappingList[].ResourceARN" --output text

Expect an empty list, plus possibly the two KMS keys in state PendingDeletion (they finish deleting after the 7-day window and are not billed while pending ⚠ VERIFY). Also check the Secrets Manager secrets and the interface endpoints:

aws secretsmanager list-secrets --profile learn-sandbox --include-planned-deletion \
  --query "SecretList[?starts_with(Name,'hl-m10')].[Name,DeletedDate]" --output table
aws ec2 describe-vpc-endpoints --profile learn-sandbox --filters Name=tag:Lab,Values=m10-env \
  --query "VpcEndpoints[].VpcEndpointId" --output text

CloudFormation deletes the database secret hl-m10-cdk-dev/db with a recovery window, and a secret scheduled for deletion blocks a new secret with the same name. If you want to redeploy straight away, remove it permanently:

aws secretsmanager delete-secret --secret-id hl-m10-cdk-dev/db --force-delete-without-recovery --profile learn-sandbox

(Terraform's RDS-managed secret is deleted together with the instance.)

Infrastructure as Code

The lab is the IaC. This table is the map between the two halves; read the same concept in both before you start.

ConceptCDK (C#), infra/cdkTerraform, infra/terraform
Entry pointProgram.cs builds the app, adds the cdk-nag plugin and synthesizesmain.tf with versions.tf and variables.tf
Environment settingsEnvironmentProfile and EnvironmentSettings records, -c contextlocal.profiles map, variables.tf with validation, terraform.tfvars
NetworkNatFreeVpc L3 construct (Patterns/NatFreeVpc.cs)modules/nat-free-vpc
ApplicationAppStack (security groups, RDS, ALB, Fargate, alarms)modules/accounts-app (security.tf, database.tf, ecs.tf, monitoring.tf)
FoundationFoundationStackRoot module: key, alias, ECR repository
Two stagesTwo stacks and imageTagcount = local.deploy_app ? 1 : 0 on the app module
StateCloudFormation (AWS holds it)S3 backend with use_lockfile
TestsxUnit v3 over the synthesized template, cdk-nag, snapshotterraform test with mock_provider
TaggingTagging.Apply (app-level tags)default_tags in the provider block
Driftcdk drift, detect-stack-driftplan -refresh-only
Nameshl-m10-cdk-dev-*, VPC 10.60.0.0/16hl-m10-tf-dev-*, VPC 10.70.0.0/16

The two use different name prefixes and CIDR ranges so both can exist in one account at once, which is exactly what you need to compare them.

Complete code

Everything is in labs/m10-environment-two-tools. The key files, in the order to read them:

labs/m10-environment-two-tools/
├── README.md
├── EnvironmentTwoTools.slnx
├── global.json                          # .NET SDK pin and Microsoft.Testing.Platform runner
├── infra/
│   ├── cdk/
│   │   ├── cdk.json                     # context and feature flags
│   │   └── Harbourline.Environment.Cdk/
│   │       ├── Program.cs               # entry point, cdk-nag plugin
│   │       ├── EnvironmentSettings.cs   # profiles, context, validation
│   │       ├── HarbourlineApp.cs        # builds the stacks for one environment
│   │       ├── FoundationStack.cs       # key, NAT-free VPC, ECR
│   │       ├── AppStack.cs              # ALB, Fargate, RDS, alarms, acknowledgments
│   │       ├── Tagging.cs
│   │       └── Patterns/NatFreeVpc.cs   # the L3 construct
│   └── terraform/
│       ├── main.tf  variables.tf  outputs.tf  versions.tf
│       ├── backend.hcl.example  terraform.tfvars.example
│       ├── modules/nat-free-vpc/   modules/accounts-app/
│       └── tests/environment.tftest.hcl
├── src/Harbourline.Environment.DriftReport/   # the C# drift report tool
└── tests/
    ├── Harbourline.Environment.Cdk.Tests/       # 28 synthesis tests, with Node.js
    └── Harbourline.Environment.DriftReport.Tests/

The entry point shows how cdk-nag v3 is registered and how the tool's findings fail the synthesis:

infra/cdk/Harbourline.Environment.Cdk/Program.cs
using Amazon.CDK;
using Cdklabs.CdkNag;
using Harbourline.Environment.Cdk;

var app = new App();
var settings = EnvironmentSettings.FromContext(app.Node);

var (_, appStack) = HarbourlineApp.Build(app, settings);
if (appStack is null)
{
    Console.Error.WriteLine("Note: no -c imageTag=... given, so only the foundation stack is synthesized.");
}

// cdk-nag v3 (AWS Solutions rules) runs as a CDK policy-validation plugin.
// Any unacknowledged finding makes `cdk synth` and `cdk deploy` fail with a validation report.
Validations.Of(app).AddPlugins(new AwsSolutionsChecks(app, new NagPackProps { Verbose = true }));

app.Synth();

The drift report merges both tools into one exit code. Its formatter is short enough to read in a minute:

src/Harbourline.Environment.DriftReport/DriftReportFormatter.cs (excerpt)
/// <summary>Exit code for pipelines: 0 no drift, 2 operational drift only, 3 security-relevant drift.</summary>
public static int ExitCode(IReadOnlyList<DriftItem> items) =>
    items.Count == 0 ? 0 : items.Any(i => i.IsSecurityRelevant) ? 3 : 2;

Code explained line by line

  1. EnvironmentSettings.FromContext(app.Node). Reads environmentName, owner, allowedIngressCidr, imageTag and other settings from cdk.json and -c flags into one typed record, so a typo is a compile error and a missing value is a clear message.
  2. HarbourlineApp.Build(app, settings). Used by both Program.cs and the tests. The tests therefore check exactly what cdk synth produces.
  3. appStack is null. With no image tag only the foundation exists. This is the first stage of the two-stage deployment.
  4. Validations.Of(app).AddPlugins(new AwsSolutionsChecks(...)). The cdk-nag v3 way to register a rule pack. An unacknowledged finding stops synthesis.
  5. NatFreeVpc. Subnet configuration with NatGateways = 0, isolated app and data tiers, flow logs to an encrypted log group, an S3 gateway endpoint with a policy for the ECR layer bucket only, and four interface endpoints reachable from the app subnets on 443.
  6. FoundationStack. The key (rotation on, 7-day deletion window, retain in prod), the key-policy statements that let CloudWatch Logs and CloudWatch alarms use it only for this account, and the ECR repository with immutable tags and scan on push.
  7. AppStack. A chain of security groups, RDS with a generated secret, an ALB, Fargate with the secret injected as an ECS secret, and five alarms to an encrypted topic. Its header comment explains why the key is imported by ARN and the repository by attributes.
  8. AcknowledgeFindings. Each cdk-nag acknowledgment has a written reason; two are conditional on the profile (single-AZ and deletion protection in dev).
  9. Terraform locals.profiles. The same profile values as EnvironmentProfile.For(), so a reviewer can compare them side by side.
  10. mock_provider "aws" in the .tftest.hcl. Generates fake values for computed attributes. Resources whose ARNs are validated by other resources (flow log, log group, key) need realistic ARNs in the mock, or the provider rejects the plan. That was a real bug fixed in this lab.
  11. ExitCode. One line that lets a pipeline treat security drift differently from operational drift.

Fixes made while building this lab

Two defects were found by running the whole test suite with Node.js, which the default Docker image does not include (so the 28 CDK tests had been skipped):

  • A stack dependency cycle in the CDK app. Granting the application's execution role use of the foundation's key wrote the role's ARN into the key policy, making the foundation depend on the application. Fixed by importing the key by ARN in AppStack (explained in CDK stacks, environments and configuration). All 28 CDK tests failed before the fix and pass after it.
  • A failing Terraform test. The mock provider returned random strings for ARNs that aws_flow_log validates. Fixed by giving the mock realistic ARNs.

A test suite that silently skips is a hole. The CDK tests now fail in CI (CI=true) when Node.js is missing.

Security considerations

⚠ Learning shortcut — not for production

The load balancer speaks plain HTTP to your IP only. Production uses HTTPS with an ACM certificate, a WAF and a real client range (Module 5 and Module 11). RDS credential rotation and ALB access logs are acknowledged in cdk-nag with a written reason and not implemented. You deploy from a laptop with an SSO session and the default CDK bootstrap execution policy; in a bank a pipeline role applies approved changes, and the bootstrap policy is narrowed. The container is a stand-in web server, not the Accounts API.

  • No NAT gateway means no outbound internet. That is the point, and it also means anything the task needs must have an endpoint. If a task hangs at PENDING or CannotPullContainerError, an endpoint is missing or blocked.
  • The ALB security group is your /32. Validation refuses ranges wider than /16 and 0.0.0.0/0. If you open the group in Part D, close it again.
  • KMS key policy scope. The CloudWatch Logs statement is limited by an ArnLike condition on the encryption context, and the alarms statement by aws:SourceAccount. Neither grants wider access than this account.
  • The task role has no permissions because the application calls no AWS API. The execution role can read only the database secret and pull from the repository.
  • State and secrets. Terraform state contains resource attributes, so use the encrypted, versioned backend with a TLS-only policy from the state lesson, and never commit terraform.tfvars or backend.hcl (they hold no secrets, but they identify your account).
  • Never claim compliance. This lab demonstrates controls (private networking, encryption, least privilege, alarms, evidence). Whether they satisfy a specific regulation is for your compliance function.

Banking use case

Harbourline's payments platform team wants the Accounts API environment reproducible per Region for disaster recovery and identical between test and production. They keep the CDK app in the platform repository, with the settings in version control and the tests as a required check. A second team inherited a Terraform estate from a previous acquisition and keeps it. Both teams publish the same evidence in the same format: a test report, the reviewed diff for each change, and the daily drift report with its exit code. When the operational-resilience review asks for a rebuild in another Region, the answer is a documented command in each repository, with the last successful drill date.

Common mistakes

  • Skipping Part A and debugging a deployment that a test would have caught in seconds.
  • Reading Replacement only in one tool. CDK shows a diff of properties; Terraform shows -/+ for replacement; CloudFormation change sets show Replacement: True. Learn each tool's signal.
  • Forgetting immutable tags. Pushing 1.0.0 twice fails by design. Use a new tag per image.
  • Using latest. Refused by validation, and a bad idea anyway.
  • Not destroying the application before the foundation (the CLI destroys in dependency order if you give both names; Terraform handles order itself).
  • Leaving endpoints running. Interface endpoints bill hourly even when idle; they are the largest part of the hourly cost.
  • Redeploying within days and hitting the secret name. A deleted secret stays reserved for its recovery window; force-delete the lab secret.
  • Treating a clean drift report as proof of safety. It is a measurement of declared properties on supported types.

Troubleshooting

SymptomLikely causeFix
28 CDK tests skippedNode.js not on PATHInstall Node.js 22 or 24, or use the Docker command with Node installed
CDK tests fail with DependencyCycleAn old copy of the lab without the key-import fixUse the current AppStack.cs (key imported by ARN)
Set -c owner=<your-name>Validation guardPass your name; the placeholder is refused on purpose
Set -c allowedIngressCidr=...Missing or too wide rangeUse your public IP as /32
Set -c imageTag=...The app stack needs an imagePush an image and pass its tag
Task stuck PENDING or CannotPullContainerErrorImage not in the repository, wrong tag, or an endpoint missingCheck the tag in ECR; check the four endpoints and the S3 gateway endpoint exist
tag invalid: ... immutable on pushThe tag already existsPush a new tag
ALB returns 503 or targets unhealthyHealth check path or port mismatchThe stand-in image serves / on 8080 (the default settings); check healthCheckPath and containerPort
Terraform: Backend initialization requiredRan -backend=false earlierterraform init -backend-config=backend.hcl
Terraform: Error acquiring the state lockA previous run is still active or crashedWait, or see force-unlock in the state lesson (only if you are sure no run is active)
terraform test fails with invalid ARNThe mock returns random ARNsUse the lab's mock_resource ARN defaults
Drift not detected for a hand-added rule in TerraformRules are separate resources, so unmanaged additions are invisibleExpected; see Part E and put it in the ADR
secret ... scheduled for deletion on redeployRecovery window for the CDK database secretdelete-secret --force-delete-without-recovery
Unable to resolve AWS account to use or no credentialsNot signed in, or missing --profileaws sso login --profile learn-sandbox

Cost and cleanup

Cost: credits. Per hour per environment, approximately (us-east-1 list prices, ⚠ VERIFY):

ItemApproximate hourly cost
Four interface endpoints x two AZs0.08 USD
Application Load Balancer0.02 USD plus capacity units (negligible here)
RDS db.t4g.micro (single AZ, 20 GiB gp3)0.02 USD
Fargate task (0.25 vCPU, 0.5 GiB, ARM)0.01 USD
KMS keyabout 0.001 USD (1 USD per month, prorated)
Logs, ECR, alarmscents
Total per environmentabout 0.15 USD (0.30 USD if both run at once)

A one-hour session with both environments costs well under 1 US dollar. A forgotten environment costs about 3.6 USD per day each, mostly the endpoints. Set a calendar reminder when you start.

Free alternative. Do Part A only (tests, cdk synth for foundation and app with -c flags, terraform validate and terraform test), and write the ADR from what you read. You lose the drift exercise and the deployment feel, nothing else.

Teardown (summary of Part F):

# CDK (from infra/cdk)
cdk destroy hl-m10-cdk-dev-app hl-m10-cdk-dev-foundation -c owner=<you> -c imageTag=1.0.0 -c allowedIngressCidr=<ip>/32 --profile learn-sandbox
# Terraform (from infra/terraform)
terraform destroy

# prove it
aws resourcegroupstaggingapi get-resources --profile learn-sandbox --region us-east-1 --tag-filters Key=Lab,Values=m10-env

If cdk destroy fails on the ECR repository, empty it first (aws ecr batch-delete-image for the tags), or rely on the dev setting EmptyOnDelete. Never delete lab resources outside the tool unless a destroy is stuck, and then say so in your notes.

Exercises

  • 🟢 Guided: complete Parts A, B, C and F for the CDK version. Record the cdk diff output of Part C in your notes and explain each line.
  • 🟡 Challenge: complete Parts D and E: cause the same two changes in both environments, produce both drift reports, and write down which findings each tool saw and which it missed. Add a cdk-nag acknowledgment that has a ticket reference and expiry date using the helper from Testing CDK.
  • 🔴 Live problem: the risk committee asks, "If a developer adds a rule 0.0.0.0/0 to the ALB security group in production, how long until we know, and who is told?" With the two tools you have built, design the answer: detection method per tool, schedule, who is paged, how a fix is reviewed, and what you would add to prevent it (not only detect it).

Assignment

Write docs/adr-0001-iac-tool.md in your portfolio repository. Use this structure and keep it under two pages:

# ADR 0001: Infrastructure as code tool for the Payments platform team

Status: Proposed | Accepted    Date: YYYY-MM-DD    Deciders: names or roles

## Context
What we build (the NAT-free Accounts API environment), team skills, audit and change-control
requirements, multi-account and multi-Region plans, existing estate.

## Options considered
1. AWS CDK in C#  2. Terraform  3. CloudFormation YAML  (one paragraph each, with evidence from this lab)

## Decision
We will use ... for ... and ... for ...

## Consequences
Positive, negative, risks, what we must build (pipeline, state protection or bootstrap policy, training).

## Evidence from the lab
Test counts, diff readability, drift findings per tool, time to first deployment, cost per hour.

## Review date
When we revisit this decision.

A good ADR names what the losing options were better at.

Solutions

Knowledge check

Interview questions

  • "Walk me through how you would prove an ECS service has no internet access." Show the route tables of the task subnets (only local), no NAT or internet gateway route, security groups without 0.0.0.0/0 egress, the endpoint list the service uses, and the unit tests that assert each of these on the template, plus a runtime check such as Reachability Analyzer or a failed outbound request from an exec session in a test environment.
  • "CDK or Terraform for a bank?" It depends on skills, existing estate, multi-cloud needs and audit tooling. Name what you would measure: time to review a change, drift visibility, test coverage, state-handling risk, hiring. (The ADR is the portfolio answer.)
  • "How do you promote the same code through environments?" One codebase with typed or variable-file settings per environment, validated and tested, deployed by a pipeline in order with approvals, with the settings diff as the review artefact.
  • "What would you not trust a clean drift report to tell you?" That the template is secure, that unsupported resource types match, or that properties absent from the code were not changed.

Certification practice

Completion checklist

  • I ran both test suites without an account (50 tests in the .NET solution, 6 Terraform tests)
  • I can explain the NAT-free design and its cost and security trade-off
  • I deployed, verified and destroyed the CDK version, and read every diff before applying
  • I deployed, verified and destroyed the Terraform version
  • I caused drift on purpose, produced the report from both tools and know what each missed
  • The tag query shows nothing left billing (apart from keys pending deletion)
  • I wrote the ADR choosing a tool for a team, including what the other tool does better

Key takeaways

  • One environment, two tools: the design is the same; the review, state and drift stories are different, and those are what you choose between.
  • NAT-free is a posture decision first and a cost decision second.
  • Tests over the synthesized template or the mock plan are free, fast evidence. Run them completely, and make a missing prerequisite fail in CI.
  • How a tool models a resource decides what drift it can see. Add a second detective control.
  • Destroy within the hour, verify with tags, and write down the decision.

Further reading

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