Software Crafting

Codifying Your Cloud: Architecting Multi-Environment AWS Deployments with AWS CDK and TypeScript

BBadal SinghCloud and DevOps10 min read06 Apr 2026Updated 2026-11-07
Rows of dark tiles topped with glowing cyan, green and orange city-like light structures, joined by bright cable lines on a dark grid

TL;DR: This guide demonstrates how to replace manual AWS ClickOps with deterministic Infrastructure as Code using AWS CDK and TypeScript. You will learn how to architect environment-agnostic stacks and use zod to strictly validate configuration objects at synthesis time to prevent deployment failures. It also covers designing dynamic, multi-environment VPCs that automatically adjust resources like NAT Gateways to optimize for both development costs and production availability.

Key Takeaways

  • Use zod to strictly validate environment configurations (like CIDR blocks and account IDs) to fail fast during cdk synth rather than mid-deployment.
  • Build environment-agnostic infrastructure by passing configuration objects into generic stack classes instead of hardcoding separate stacks (e.g., StagingVpcStack).
  • Dynamically load target environments by passing context variables through the CDK CLI using app.node.tryGetContext('env') (e.g., cdk deploy -c env=prod).
  • Control VPC costs and availability through config, such as setting natGateways: 0 in dev (with isolated subnets only) and 3 in production.
  • Protect production account IDs from being hardcoded in repositories by injecting them via AWS Systems Manager (SSM) or CI/CD pipeline environment variables.

Your development velocity is being bottlenecked by ClickOps.

When engineering teams rely on manual AWS console modifications or ad-hoc shell scripts to provision infrastructure, environments become hard to reproduce. A developer requests an S3 bucket or an SQS queue, and operations provisions it manually. Over time, Configuration Drift creeps in. Security group rules added in production are missed in staging, and the notorious "it works in staging, but fails in production" syndrome becomes a daily reality.

While Terraform has long been the industry standard for Infrastructure as Code (IaC), the cognitive load of context-switching between TypeScript (in your application layer) and HCL (HashiCorp Configuration Language) slows down full-stack teams. Terraform's domain-specific looping and conditional constructs (like count and for_each) can also feel more limited than a general-purpose programming language when environments differ in structure. Both tools are mature; the choice is mostly about team skills and ecosystem.

The AWS Cloud Development Kit (CDK) solves this by allowing developers to define cloud infrastructure using expressive, familiar programming languages like TypeScript. However, treating infrastructure as code means you must apply software engineering principles, like dependency injection and modularity, to your cloud resources.

In this guide, we will build a multi-environment AWS CDK deployment. We will build config-driven stacks, manage stateful resources safely, avoid cross-stack export locking, and automate deployments via CDK Pipelines.

Bootstrapping a Multi-Environment CDK Architecture

A common anti-pattern in early CDK adoption is hardcoding environment variables or creating entirely separate stack classes for staging and production (e.g., StagingVpcStack and ProdVpcStack). This defeats the primary purpose of reusable IaC.

Instead, your architecture must be environment-agnostic. The environment (dev, staging, prod) should simply be a configuration object passed into your generic stacks.

First, initialize the project:

npx cdk init app --language typescript
npm install zod

We use zod to strictly validate our environment configuration at synthesis time. If a developer forgets to add a crucial environment variable, the CDK app should fail immediately during cdk synth, rather than midway through a CloudFormation deployment.

Define your environment structure in lib/config/environment.ts:

import { z } from 'zod';

export const EnvironmentConfigSchema = z.object({
  envName: z.enum(['dev', 'staging', 'prod']),
  accountId: z.string().length(12),
  region: z.string(),
  vpcCidr: z
    .string()
    .regex(
      /^([0-9]{1,3}\.){3}[0-9]{1,3}\/([0-9]|[1-2][0-9]|3[0-2])$/,
      'Invalid IPv4 CIDR notation',
    ),
  maxAzs: z.number().min(2).max(3),
  natGateways: z.number().min(0),
  retainStatefulResources: z.boolean(),
});

export type EnvironmentConfig = z.infer<typeof EnvironmentConfigSchema>;

export const environments: Record<string, EnvironmentConfig> = {
  dev: {
    envName: 'dev',
    accountId: '111122223333',
    region: 'us-east-1',
    vpcCidr: '10.0.0.0/16',
    maxAzs: 2,
    natGateways: 0, // Cost savings in dev
    retainStatefulResources: false,
  },
  prod: {
    envName: 'prod',
    accountId: '444455556666',
    region: 'us-east-1',
    vpcCidr: '10.1.0.0/16',
    maxAzs: 3,
    natGateways: 3, // Highly available NATs in prod
    retainStatefulResources: true,
  },
};

Production Note: Never hardcode actual production account IDs in open-source or highly accessible repositories. Use AWS Systems Manager (SSM) or inject them via environment variables in your CI/CD pipeline.

Next, wire up the entry point in bin/app.ts to contextually load these environments.

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { BaseInfrastructureStack } from '../lib/stacks/base-infra-stack';
import { environments, EnvironmentConfigSchema } from '../lib/config/environment';

const app = new cdk.App();

// Retrieve environment context passed via CLI: cdk deploy -c env=prod
const targetEnv = app.node.tryGetContext('env') || 'dev';
const rawConfig = environments[targetEnv];
if (!rawConfig) {
  throw new Error(
    `Unknown environment "${targetEnv}". Expected one of: ${Object.keys(environments).join(', ')}`,
  );
}

// Fail fast on misconfiguration
const config = EnvironmentConfigSchema.parse(rawConfig);

new BaseInfrastructureStack(app, `BaseInfra-${config.envName}`, {
  env: { account: config.accountId, region: config.region },
  config,
});

Designing Deterministic Networking Stacks

When building your Virtual Private Cloud (VPC), explicit configuration matters. By default, the CDK's Vpc construct makes opinionated choices that might not match your compliance needs or budget, such as deploying a NAT Gateway per Availability Zone (AZ).

In our design, we pass the parsed config object to dictate VPC behavior. Each NAT Gateway is billed per hour plus per GB processed (see the Amazon VPC pricing page for your region), so removing them from non-production environments is a common saving. Note that CDK refuses to synthesize a VPC with natGateways: 0 and PRIVATE_WITH_EGRESS subnets, so the compute subnet type has to follow the config as well.

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { EnvironmentConfig } from '../config/environment';

interface BaseInfrastructureProps extends cdk.StackProps {
  config: EnvironmentConfig;
}

export class BaseInfrastructureStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;

  constructor(scope: Construct, id: string, props: BaseInfrastructureProps) {
    super(scope, id, props);

    // Without NAT Gateways there is no egress route, so private compute subnets must be isolated.
    // Workloads there need VPC endpoints (S3, ECR, Secrets Manager, etc.) to reach AWS services.
    const computeSubnetType =
      props.config.natGateways > 0
        ? ec2.SubnetType.PRIVATE_WITH_EGRESS
        : ec2.SubnetType.PRIVATE_ISOLATED;

    this.vpc = new ec2.Vpc(this, 'MainVpc', {
      ipAddresses: ec2.IpAddresses.cidr(props.config.vpcCidr),
      maxAzs: props.config.maxAzs,
      natGateways: props.config.natGateways,
      subnetConfiguration: [
        {
          cidrMask: 24,
          name: 'Public',
          subnetType: ec2.SubnetType.PUBLIC,
        },
        {
          cidrMask: 24,
          name: 'PrivateCompute',
          subnetType: computeSubnetType,
        },
        {
          cidrMask: 28, // Smaller CIDR for isolated data stores
          name: 'PrivateIsolated',
          subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
        },
      ],
    });

    // Enforce flow logs in production environments
    if (props.config.envName === 'prod') {
      this.vpc.addFlowLog('VpcFlowLogs', {
        destination: ec2.FlowLogDestination.toCloudWatchLogs(),
        trafficType: ec2.FlowLogTrafficType.ALL,
      });
    }
  }
}

Managing Stateful Resources and Preventing Catastrophic Data Loss

Stateless resources like Lambda functions or ECS services can be destroyed and recreated safely. Stateful resources, such as databases, S3 buckets, and KMS keys, require strict lifecycle management.

A common failure mode in IaC is accidentally deleting a database during a stack refactor, for example by changing a construct ID so CloudFormation replaces the resource. Set RemovalPolicy and deletion protection from the environment context so production data is retained by default. (If you want a second pair of eyes on this setup, our DevOps and cloud deployment services cover CDK reviews.)

import * as cdk from 'aws-cdk-lib';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { EnvironmentConfig } from '../config/environment';

interface DatabaseStackProps extends cdk.StackProps {
  config: EnvironmentConfig;
  vpc: cdk.aws_ec2.IVpc;
}

export class DatabaseStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: DatabaseStackProps) {
    super(scope, id, props);

    const removalPolicy = props.config.retainStatefulResources
      ? cdk.RemovalPolicy.RETAIN
      : cdk.RemovalPolicy.DESTROY;

    // Secure Document Storage
    const storageBucket = new s3.Bucket(this, 'AppStorage', {
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
      encryption: s3.BucketEncryption.S3_MANAGED,
      enforceSSL: true,
      versioning: props.config.retainStatefulResources,
      removalPolicy,
      autoDeleteObjects: !props.config.retainStatefulResources, // DANGEROUS in prod
    });

    // Database cluster
    const cluster = new rds.DatabaseCluster(this, 'AppDatabase', {
      engine: rds.DatabaseClusterEngine.auroraPostgres({
        // Pin a minor version that is currently supported by Aurora in your region
        version: rds.AuroraPostgresEngineVersion.VER_17_9,
      }),
      vpc: props.vpc,
      vpcSubnets: { subnetType: cdk.aws_ec2.SubnetType.PRIVATE_ISOLATED },
      writer: rds.ClusterInstance.serverlessV2('Writer'),
      serverlessV2MinCapacity: props.config.envName === 'prod' ? 2 : 0.5,
      serverlessV2MaxCapacity: props.config.envName === 'prod' ? 16 : 2,
      removalPolicy,
      deletionProtection: props.config.retainStatefulResources,
    });
  }
}

Warning: Setting autoDeleteObjects: true provisions a custom resource Lambda function that deletes every object in the bucket when the bucket is removed from the stack or the stack is destroyed. Never enable this for production environments, because an accidental stack deletion would wipe the data (versioning does not help, since the function deletes all object versions).

Resolving Secrets and Decoupling Cross-Stack References

As your CDK application grows, you will need to pass references between stacks, for example passing the VPC ID from the BaseInfrastructureStack to the DatabaseStack.

The default way CDK handles passing resources between stacks is by utilizing CloudFormation Exports. While this is convenient, it has a well-known drawback: export locking.

If Stack A exports a value that Stack B imports, CloudFormation will not let you delete or change that export while it is in use. Replacing or removing the resource in Stack A therefore requires first deploying Stack B without the import, which makes some refactors a two-step deployment.

One way to decouple stacks is to publish identifiers to AWS Systems Manager (SSM) Parameter Store and read them in the consuming stack.

Here is how you write to SSM in the infrastructure stack:

import * as ssm from 'aws-cdk-lib/aws-ssm';

// Inside BaseInfrastructureStack constructor
new ssm.StringParameter(this, 'VpcIdParameter', {
  parameterName: `/${props.config.envName}/network/vpc-id`,
  stringValue: this.vpc.vpcId,
});

And here is how you dynamically resolve it in the dependent stack without creating a hard CloudFormation export:

import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ssm from 'aws-cdk-lib/aws-ssm';

// Inside DatabaseStack constructor
const vpcId = ssm.StringParameter.valueFromLookup(this, `/${props.config.envName}/network/vpc-id`);

const vpc = ec2.Vpc.fromLookup(this, 'ImportedVpc', {
  vpcId: vpcId,
});

With valueFromLookup, the CDK resolves the parameter at synthesis time and writes the VPC ID directly into the DatabaseStack template. There is no CloudFormation export coupling the two stacks, so the network stack can be refactored without the import lock.

The trade-offs are real, though:

  • The parameter must already exist at synth time. On a fresh account, deploy the base stack first, or synthesis will fail because the lookup cannot find the parameter.
  • Lookups are cached. The CDK stores the result in cdk.context.json, which AWS recommends committing. If the VPC is replaced, run cdk context --reset <key> and synthesize again, or the dependent stack keeps the old ID.
  • Nothing stops you deleting a VPC that is still in use. The export lock was a safety net; with SSM you need your own deployment ordering.

If both stacks always deploy together, passing the vpc object as a prop (as the DatabaseStack above does) is simpler and perfectly valid.

Implementing CDK Pipelines for Multi-Account Deployments

To keep environments consistent, developers should not deploy shared environments via cdk deploy from their local machines. Instead, deployment should run through an automated pipeline.

The aws-cdk-lib/pipelines module provides self-mutating pipelines. The pipeline automatically updates its own structure if you add new stages or stacks in your CDK code.

Create a pipeline-stack.ts that defines your automated route to production:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {
  CodePipeline,
  CodePipelineSource,
  ShellStep,
  ManualApprovalStep,
} from 'aws-cdk-lib/pipelines';
import { AppStage } from './app-stage';

export class PipelineStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const pipeline = new CodePipeline(this, 'EnterprisePipeline', {
      pipelineName: 'InfraDeliveryPipeline',
      // Required when stages deploy to other accounts: encrypts artifact buckets with KMS
      crossAccountKeys: true,
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.connection('your-org/your-repo', 'main', {
          connectionArn: 'arn:aws:codeconnections:us-east-1:111122223333:connection/abc-123',
        }),
        commands: ['npm ci', 'npx cdk synth'],
      }),
    });

    // Deploy to Staging Automatically
    const stagingStage = new AppStage(this, 'StagingDeployment', {
      env: { account: '111122223333', region: 'us-east-1' },
      envName: 'staging',
    });
    pipeline.addStage(stagingStage);

    // Deploy to Production with Manual Approval Guardrail
    const prodStage = new AppStage(this, 'ProdDeployment', {
      env: { account: '444455556666', region: 'us-east-1' },
      envName: 'prod',
    });

    pipeline.addStage(prodStage, {
      pre: [new ManualApprovalStep('PromoteToProduction')],
    });
  }
}

The AppStage encapsulates your BaseInfrastructureStack and DatabaseStack logic. When code is merged into the main branch, the pipeline automatically runs cdk synth, deploys the infrastructure to the Staging account, runs integration tests (if configured), and then halts at a manual approval gate before assuming an IAM role in the Production account to deploy the final stage. For this to work, each target account must be bootstrapped with cdk bootstrap --trust <PIPELINE-ACCOUNT-ID> --cloudformation-execution-policies <POLICY-ARN>. Prefer a scoped policy over the default AdministratorAccess where you can.

The Path Forward

Moving to AWS CDK and TypeScript changes how engineering teams approach cloud architecture. You stop treating AWS as a separate operational concern and start treating it as an extension of your application's codebase.

By defining validated configuration, protecting stateful resources with environment-aware removal policies, choosing deliberately between exports and SSM lookups, and deploying through a CDK pipeline, you remove most of the paths by which drift enters. Manual console changes can still happen, so run CloudFormation drift detection periodically.

Staging differs from production only where the config says it should, and a new engineer can read the whole topology from the repository and run npm ci && npx cdk synth to see exactly what will be deployed.

Sources

Available for Work

Need help building this in production?

SoftwareCrafting is a full-stack dev agency - we ship fast, scalable React, Next.js, Node.js, React Native & Flutter apps for global clients.

Get a Free Consultation

Frequently Asked Questions

Why should development teams migrate from Terraform or ClickOps to AWS CDK with TypeScript?

Relying on manual AWS console changes (ClickOps) tends to cause configuration drift between environments. Terraform is a strong option too; AWS CDK mainly removes the context switch between application code and HCL by allowing developers to define infrastructure using expressive, familiar languages like TypeScript.

What is the best practice for structuring multi-environment AWS CDK deployments?

You should avoid the anti-pattern of creating entirely separate stack classes for different environments, such as StagingVpcStack and ProdVpcStack. Instead, build environment-agnostic stacks where the environment details are passed in as a configuration object.

How can I validate AWS CDK environment variables before deployment?

You can use a schema validation library like zod to strictly validate your environment configuration objects at synthesis time. This "fail fast" approach ensures that if a developer forgets a crucial variable, the app fails immediately during cdk synth rather than breaking midway through a CloudFormation deployment.

How do I target a specific environment when deploying an AWS CDK application?

You can pass environment context directly through the AWS CDK CLI using the -c flag, for example: cdk deploy -c env=prod. Inside your CDK app's entry point (typically bin/app.ts), you can retrieve this value using app.node.tryGetContext('env') to dynamically load the correct environment configuration.

How can I reduce AWS NAT Gateway costs in development environments using CDK?

By default, the CDK Vpc construct deploys a NAT Gateway per Availability Zone, and each one is billed hourly plus per GB processed. You can pass an environment config that sets NAT Gateways to 0 in development (using only public and isolated subnets, plus VPC endpoints) while keeping one per AZ in production. A middle ground is natGateways: 1 in non-production environments.

About the author

Badal Singh

Co-founder of SoftwareCrafting

Badal Singh is the Co-founder of SoftwareCrafting in Delhi and writes technical guides about web and mobile development, backend security, cloud infrastructure, DevOps, and developer tooling.

Writes about Next.js, React, Node.js, PostgreSQL, React Native, Flutter.

View author profile

Last updated: 2026-11-07