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.
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
- Why infrastructure as code, CloudFormation fundamentals, Drift detection and StackSets
- CDK in C#: first stack, CDK constructs, CDK stacks, environments and configuration and Testing CDK
- Terraform fundamentals and Terraform state (the Terraform half uses an S3 backend you create there)
- NAT gateway and egress and VPC endpoints and PrivateLink
- Tools: .NET 10 SDK or Docker, Node.js 22 or 24 LTS, the CDK CLI (
npm install -g aws-cdk), Terraform 1.10+ (or thetfDocker function from the Terraform lessons), Docker Desktop with buildx, AWS CLI v2 signed in withaws sso login --profile learn-sandbox. The account must be bootstrapped for the CDK (cdk bootstrap, see the first-stack lesson).
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
| Need | How the design meets it |
|---|---|
| No outbound internet from the workload | No NAT gateway; isolated subnets; endpoints for ECR (two), CloudWatch Logs, Secrets Manager; an S3 gateway endpoint for image layers |
| Least privilege between tiers | Security groups reference each other (ALB to task to database), never "anything in the VPC" |
| Data encrypted at rest | One customer managed key for RDS storage, the database secret, logs and the alarm topic |
| No secrets in code or settings | RDS generates the password into Secrets Manager; the task definition references it |
| Know when it breaks | Five alarms to an encrypted SNS topic that refuses plain HTTP |
| Immutable releases | ECR tags are immutable; latest is refused by validation |
| Prove it | Tests 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.
| Option | Hourly cost (two AZs) | Per-GB cost | Security posture |
|---|---|---|---|
| NAT gateway (one per AZ) | 2 x 0.045 = 0.09 USD | 0.045 USD processed | Workload 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 USD | 0.01 USD processed | Workload can reach only the named AWS services; no internet route exists |
| S3 gateway endpoint | Free | Free | Image 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 Endpoints | Fargate in isolated subnets with VPC interface endpoints | An endpoint is an ENI per AZ with a private DNS name |
| Azure Firewall for egress | None needed: no route out | Fewer moving parts, less flexibility |
| Key Vault with customer managed keys | KMS customer managed key | Key policy plus IAM; grants across stacks need care |
| Azure Database for PostgreSQL Flexible Server | RDS for PostgreSQL | RDS can generate and rotate the master password into Secrets Manager |
| Bicep + Terraform in one estate | CDK (C#) + Terraform | The CDK produces CloudFormation; Terraform keeps its own state |
| Azure Monitor alerts + action groups | CloudWatch alarms + SNS topic | The 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.slnxOr 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:
| Suite | Tests | What it proves |
|---|---|---|
| Settings and profiles | pure C# | Bad CIDRs, latest tags, placeholder owner and unknown environments are refused; dev, test and prod differ only in configuration |
| Network | CDK synthesis | No 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 |
| Security | CDK synthesis | Database 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 |
| Operations | CDK synthesis | Five 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-nag | CDK synthesis | Resource counts per type match a committed file; zero unacknowledged AWS Solutions findings in dev and prod; a bad bucket really is flagged |
| Drift report | pure 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.jsonThe 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-sandboxExpect 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-sandboxDetect 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 applyPush 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 IPRepeat 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.jsonlTwo 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 destroyThen 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 textExpect 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 textCloudFormation 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.
| Concept | CDK (C#), infra/cdk | Terraform, infra/terraform |
|---|---|---|
| Entry point | Program.cs builds the app, adds the cdk-nag plugin and synthesizes | main.tf with versions.tf and variables.tf |
| Environment settings | EnvironmentProfile and EnvironmentSettings records, -c context | local.profiles map, variables.tf with validation, terraform.tfvars |
| Network | NatFreeVpc L3 construct (Patterns/NatFreeVpc.cs) | modules/nat-free-vpc |
| Application | AppStack (security groups, RDS, ALB, Fargate, alarms) | modules/accounts-app (security.tf, database.tf, ecs.tf, monitoring.tf) |
| Foundation | FoundationStack | Root module: key, alias, ECR repository |
| Two stages | Two stacks and imageTag | count = local.deploy_app ? 1 : 0 on the app module |
| State | CloudFormation (AWS holds it) | S3 backend with use_lockfile |
| Tests | xUnit v3 over the synthesized template, cdk-nag, snapshot | terraform test with mock_provider |
| Tagging | Tagging.Apply (app-level tags) | default_tags in the provider block |
| Drift | cdk drift, detect-stack-drift | plan -refresh-only |
| Names | hl-m10-cdk-dev-*, VPC 10.60.0.0/16 | hl-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:
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:
/// <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
EnvironmentSettings.FromContext(app.Node). ReadsenvironmentName,owner,allowedIngressCidr,imageTagand other settings fromcdk.jsonand-cflags into one typed record, so a typo is a compile error and a missing value is a clear message.HarbourlineApp.Build(app, settings). Used by bothProgram.csand the tests. The tests therefore check exactly whatcdk synthproduces.appStack is null. With no image tag only the foundation exists. This is the first stage of the two-stage deployment.Validations.Of(app).AddPlugins(new AwsSolutionsChecks(...)). The cdk-nag v3 way to register a rule pack. An unacknowledged finding stops synthesis.NatFreeVpc. Subnet configuration withNatGateways = 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.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.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.AcknowledgeFindings. Each cdk-nag acknowledgment has a written reason; two are conditional on the profile (single-AZ and deletion protection in dev).- Terraform
locals.profiles. The same profile values asEnvironmentProfile.For(), so a reviewer can compare them side by side. 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.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_logvalidates. 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
PENDINGorCannotPullContainerError, 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
ArnLikecondition on the encryption context, and the alarms statement byaws: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.tfvarsorbackend.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
Replacementonly in one tool. CDK shows a diff of properties; Terraform shows-/+for replacement; CloudFormation change sets showReplacement: True. Learn each tool's signal. - Forgetting immutable tags. Pushing
1.0.0twice 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
| Symptom | Likely cause | Fix |
|---|---|---|
| 28 CDK tests skipped | Node.js not on PATH | Install Node.js 22 or 24, or use the Docker command with Node installed |
CDK tests fail with DependencyCycle | An old copy of the lab without the key-import fix | Use the current AppStack.cs (key imported by ARN) |
Set -c owner=<your-name> | Validation guard | Pass your name; the placeholder is refused on purpose |
Set -c allowedIngressCidr=... | Missing or too wide range | Use your public IP as /32 |
Set -c imageTag=... | The app stack needs an image | Push an image and pass its tag |
Task stuck PENDING or CannotPullContainerError | Image not in the repository, wrong tag, or an endpoint missing | Check the tag in ECR; check the four endpoints and the S3 gateway endpoint exist |
tag invalid: ... immutable on push | The tag already exists | Push a new tag |
| ALB returns 503 or targets unhealthy | Health check path or port mismatch | The stand-in image serves / on 8080 (the default settings); check healthCheckPath and containerPort |
Terraform: Backend initialization required | Ran -backend=false earlier | terraform init -backend-config=backend.hcl |
Terraform: Error acquiring the state lock | A previous run is still active or crashed | Wait, or see force-unlock in the state lesson (only if you are sure no run is active) |
terraform test fails with invalid ARN | The mock returns random ARNs | Use the lab's mock_resource ARN defaults |
| Drift not detected for a hand-added rule in Terraform | Rules are separate resources, so unmanaged additions are invisible | Expected; see Part E and put it in the ADR |
secret ... scheduled for deletion on redeploy | Recovery window for the CDK database secret | delete-secret --force-delete-without-recovery |
Unable to resolve AWS account to use or no credentials | Not signed in, or missing --profile | aws sso login --profile learn-sandbox |
Cost and cleanup
Cost: credits. Per hour per environment, approximately (us-east-1 list prices, ⚠ VERIFY):
| Item | Approximate hourly cost |
|---|---|
| Four interface endpoints x two AZs | 0.08 USD |
| Application Load Balancer | 0.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 key | about 0.001 USD (1 USD per month, prorated) |
| Logs, ECR, alarms | cents |
| Total per environment | about 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-envIf 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 diffoutput 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/0to 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
Part A: 50 tests pass with Node.js 22. Part B: the foundation diff shows only additions (key, alias, VPC resources, endpoints, ECR repository); the app diff adds the ALB, service, database and alarms plus IAM statements, which the CLI prints in a table. Part C: exactly one modified AWS::CloudWatch::Alarm with Threshold 80 to 70 and no replacement; the tests still pass because they assert that five alarms exist and notify the topic, not the threshold. If your diff shows more, you changed something else (check the description text, which is also a property).
CloudFormation sees the hand-added security group ingress (ADD at /SecurityGroupIngress/1) and the desired count (NOT_EQUAL, 1 versus 3), with expected and actual values. Terraform's refresh-only plan sees the ECS service update and the deleted managed rule, but not a new unmanaged rule, and reports no attribute values in the JSON stream. Neither tool sees a change the resource type does not support or a property not in the code. Your notes should end with the consequence: add AWS Config rules (for example, no unrestricted ingress) as a second detective control that does not depend on how the tool models the resource.
Detection: for CDK stacks, scheduled detect-stack-drift (hourly or daily) plus the DriftReport exit code; for Terraform, scheduled plan -refresh-only -json with the same report; plus an AWS Config rule or EventBridge rule on AuthorizeSecurityGroupIngress events for near-real-time detection that is independent of both tools. Notification: exit code 3 pages the security on-call; exit 2 opens a ticket for the owning team. Fix: through a pull request that reverts or accepts the change, with the drift-aware change set (CloudFormation) or terraform apply (Terraform) run by the pipeline. Prevention: a Service Control Policy denying ec2:AuthorizeSecurityGroupIngress with 0.0.0.0/0 outside an exemption role, and removing console write access to production. The honest answer to "how long": as long as the schedule, unless the event-driven rule is in place, in which case minutes.
Knowledge check
Answer: The application needs a container image that exists in the foundation's ECR repository, plus the network and key. The CDK models this with a stack dependency and the Terraform version with a conditional module; neither builds the application until an image tag is supplied.
Answer: Not at this size: four interface endpoints in two AZs cost about as much per hour as two NAT gateways. It wins on per-GB cost at volume and on posture, because the workload has no internet route at all.
Answer: The CDK declares the rules inside the security group, so CloudFormation compares the whole list and sees an extra entry. The Terraform version models each rule as a separate resource, so Terraform only checks the rules it manages and cannot see an unmanaged addition.
Answer: A cross-stack dependency cycle (the key policy named the application's role) and a failing Terraform mock. The lesson: tests that skip silently hide defects; fail the build in CI when a prerequisite is missing, and run the suite fully at least once.
Answer: So both can run in the same account and Region at the same time without name clashes, and could be peered later without overlapping address ranges.
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 without0.0.0.0/0egress, 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
A bank's workload in private subnets must not have any path to the internet, but it must pull images from Amazon ECR, write logs to CloudWatch Logs and read secrets from Secrets Manager. Which design meets this with the fewest components?
A. A NAT gateway in each public subnet and a restrictive network ACL. B. VPC interface endpoints for ECR (API and Docker), CloudWatch Logs and Secrets Manager, plus an S3 gateway endpoint, with no NAT gateway or internet route in the workload subnets. C. A transit gateway to an on-premises proxy. D. Public IP addresses on the tasks with security groups blocking inbound traffic.
Answer: B. Endpoints give private access to exactly the services needed, and the S3 gateway endpoint carries image layers. A leaves an internet path. C adds latency and complexity. D exposes the tasks to the internet route.
A team manages an ECS service with CloudFormation. An engineer scaled the service by hand during an incident. The team wants to detect this and see which property differs. Which action does that?
A. Run drift detection on the stack and view the resource drift for the service. B. Delete the stack. C. Look at CloudTrail only. D. Execute the last change set again.
Answer: A. Drift detection returns the property differences (here DesiredCount). CloudTrail shows who made the call but not the comparison with the template. B destroys the environment. D changes nothing if the template is unchanged.
A Terraform configuration manages security group rules as separate aws_vpc_security_group_ingress_rule resources. Someone adds an extra inbound rule in the console. A scheduled terraform plan -refresh-only shows no differences. Why, and what should the team add?
A. Terraform is broken; reinstall it. B. Terraform only tracks the rules it manages, so an unmanaged extra rule is invisible; add an AWS Config rule (or similar) that evaluates the security group itself. C. Refresh-only plans never detect changes. D. The rule is not real.
Answer: B. Separate rule resources mean Terraform has no expectation for unmanaged rules. A second detective control on the security group catches it. C is wrong: refresh-only detects changes to managed resources.
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.
Related lessons and next steps
- Previous: Testing CDK
- Module overview: Module 10: Infrastructure as Code
- The decision lesson: CloudFormation vs CDK vs Terraform
- Governance and landing zones: Module 15
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.
Terraform modules
Package a secure S3 bucket as a reusable Terraform module with validated inputs, outputs and a terraform test suite, version it with Git tags, call it from registry and Git sources, and decide when a module is worth writing at all.
CloudFormation vs CDK vs Terraform
Compare the same secure bucket in CloudFormation, the AWS CDK (C#) and Terraform, score the tools against a bank's real criteria with a small C# decision scorer, and write an architecture decision record you could defend to your team.