The previous post covered VPC connectivity patterns. This one is how I build that infrastructure with the AWS CDK in TypeScript: how I split stacks, how they share values, and the practices that keep a growing CDK app from turning into a coupled mess.

Layer stacks like an onion

The most useful habit: design stacks in layers, each depending only on the ones inside it. I start with a foundation stack (shared KMS keys, log groups, parameter namespaces, deployment roles), then a VPC stack, then data and application stacks on top.

CDK stack layers Tap the diagram to open it full size.

Why layering works:

  • Each stack is one CloudFormation template with its own rollback scope, so a failed deploy rolls back that layer instead of the whole application. That isolates the mechanics of change, not the runtime blast radius: a successful but bad change can still break the layers above.
  • Layers deploy in order and tear down in reverse, matching the real dependencies. Cross-stack references tell CDK that order for you; once stacks share values through SSM instead, declare the order yourself with stack.addDependency() or orchestrate it in CI.
  • Teams can own layers without stepping on each other.

Passing values between stacks: the trade-offs

The real design question is how the VPC stack hands things (VPC id, subnet ids, security group ids) to the stacks above it. Three honest options, picked per value:

1. Direct props (cross-stack references). Pass the L2 construct: new AppStack(app, 'App', { vpc: networkStack.vpc }). CDK turns this into a CloudFormation export and Fn::ImportValue: fully typed, explicit dependency, but hard coupling. You cannot change or delete the export while anything imports it, and the stacks must share a CDK app and environment (same account and Region, unless you opt into crossRegionReferences). Good for tightly related stacks deployed together.

2. SSM Parameter Store. Producer writes a parameter, consumer reads it, no shared CloudFormation export. My default across boundaries. Two ways to read, and confusing them causes real pain:

  • At deploy time (ssm.StringParameter.valueForStringParameter): CDK hands back a token, and the synthesized template carries a CloudFormation parameter of type AWS::SSM::Parameter::Value (parameter name as its default) that CloudFormation resolves from Parameter Store when the consumer stack is created or updated. No re-synth when the value changes, but since the template is unchanged a plain cdk deploy reports no changes and running resources keep the old value until you force a stack update (drift detection does not re-resolve these values). And nothing is validated at synthesis: a missing or misspelled parameter fails the deployment, not the build.

SSM parameter resolution timing Tap the diagram to open it full size.

  • At synthesis (ssm.StringParameter.valueFromLookup): CDK queries the account during cdk synth, caches the value in cdk.context.json, and bakes the literal string into the template. Deterministic, so template tests are trustworthy. The catch: it goes stale (re-deployed subnets stay old until you refresh context and re-synth). Credentials are needed only when the value is not already cached, so commit cdk.context.json to keep CI deterministic.

3. At runtime. The app reads the parameter itself (instance startup, Lambda env pointing at a path). No CloudFormation coupling, and the right answer for secrets, which belong in Secrets Manager rather than Parameter Store.

Rule of thumb: props inside one deployment boundary, SSM across boundaries, runtime reads for secrets and fast-changing values.

Practices that matter as much as layering

One stack instance per environment. Pass env: { account, region } explicitly; model dev/stage/prod as separate instantiations of the same stack classes, not if (env === 'prod') branches.

Let CloudFormation name things. Hardcoded physical names block re-deploys and make replacements painful. Omit names and pass the generated ones to whatever needs them.

Reusable constructs with sane defaults. Wrap repeated patterns (a service with its log group, alarms and IAM wiring) in your own constructs; drop to L1 Cfn* escape hatches only when L2 genuinely cannot express what you need.

Least privilege through grants. table.grantReadData(fn) (DynamoDB) or bucket.grantRead(fn) (S3) and friends generate tighter policies than hand-written wildcards, and keep permissions next to the code that needs them.

Test the template, not just the code. aws-cdk-lib/assertions can assert a stack contains a KMS-encrypted bucket or match a template against a snapshot; deterministic synthesis (no uncached lookups) is what makes those tests trustworthy.

Deploy safely. Run cdk diff in CI so every PR shows its infrastructure delta, and use CDK Pipelines (or your CI equivalent) with a stage per environment instead of deploying from a laptop.

CDK deployment flow Tap the diagram to open it full size.

The shape in code

A trimmed-down version, the VPC stack publishing its id and consuming a foundation value through SSM:

import { Stack, StackProps, Tags } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ssm from 'aws-cdk-lib/aws-ssm';

// foundation-stack.ts
export class FoundationStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    new ssm.StringParameter(this, 'OrgNameParam', {
      parameterName: '/foundation/org-name',
      stringValue: 'acme',
    });
  }
}

// vpc-stack.ts
export class VpcStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    const orgName = ssm.StringParameter.valueForStringParameter(this, '/foundation/org-name');
    const vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 2, natGateways: 2 });
    Tags.of(vpc).add('Org', orgName);
    new ssm.StringParameter(this, 'VpcIdParam', {
      parameterName: '/network/vpc-id',
      stringValue: vpc.vpcId,
    });
  }
}

An application stack then reads /network/vpc-id at deploy time with valueForStringParameter, so it picks up the current value on its own next stack update rather than a synth-time cached value or an unchangeable export.

None of this removes coupling; it moves coupling where you can see and version it. A parameter name is a contract, so version it (/network/v1/vpc-id) when you need to break it, and deploy order still matters: the producer must write the parameter before the consumer’s first deploy reads it. That visibility, plus the layered stacks, is what keeps a CDK codebase pleasant a year in.

Sources: AWS CDK Developer Guide on best practices, reading SSM parameter values, stacks and testing, plus the VPC connectivity post linked above. The SSM synthesis behavior was verified against aws-cdk-lib directly.