Skip to content
Back to Cloud Services

AWS CDK Infrastructure Code

Define AWS infrastructure in TypeScript/Python/Java, compiled to CloudFormation with constructs.

AWSIntermediate

Why

CDK gives real programming — loops, conditionals, abstractions — for infra. Constructs encapsulate patterns. aws-cdk-lib has 200+ L2 constructs with sensible defaults.

Synthesizes to CloudFormation.

When

Use CDK for AWS infra with logic, reusable constructs, and team familiarity with code. Use Terraform for multi-cloud, SAM for serverless-only.

How

1. Init — cdk init app --language typescript 2. Stack — class extends Stack, define resources in constructor 3. Constructs — L1 (raw CFN), L2 (sensible defaults), L3 (patterns) 4. Synth — cdk synth, outputs CloudFormation template 5. Deploy — cdk deploy, diff before applying 6. Diff — cdk diff, see what changes 7. Aspects — visit all constructs, apply cross-cutting (tagging, security) 8. Tests — jest with @aws-cdk/assert, snapshot tests

Example

import * as cdk from "aws-cdk-lib"; import * as s3 from "aws-cdk-lib/aws-s3"; import * as lambda from "aws-cdk-lib/aws-lambda"; export class WebStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); const bucket = new s3.Bucket(this, "uploads", { encryption: s3.BucketEncryption.KMS, blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, lifecycleRules: [{ transitions: [{ storageClass: s3.StorageClass.GLACIER, transitionAfter: cdk.Duration.days(90) }] }], }); const fn = new lambda.Function(this, "api", { runtime: lambda.Runtime.NODEJS_20_X, handler: "index.handler", code: lambda.Code.fromAsset("./dist"), environment: { BUCKET: bucket.bucketName }, }); bucket.grantReadWrite(fn); } } // cdk deploy WebStack --parameters Env=prod

Common Problems

  • ⚠Synth non-deterministic — use cdk.json context, pin versions for reproducible builds
  • ⚠L1 vs L2 confusion — L2 has defaults, L1 is raw, prefer L2 unless you need raw control
  • ⚠Imported resources can't be managed — use fromBucketArn (read-only) vs new Bucket (managed)

Best Practices

  • ✓Use L2 constructs — sensible defaults, security best practices built-in
  • ✓Write construct tests — jest snapshots catch unintended infra changes
  • ✓Use Aspects for cross-cutting — tag all resources, enforce encryption globally

FAQ

What is AWS CDK Infrastructure Code and what problem does it solve?

Define AWS infrastructure in TypeScript/Python/Java, compiled to CloudFormation with constructs. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with AWS CDK Infrastructure Code in 5 minutes?

Two steps to run it: (1) Init — cdk init app --language typescript; (2) Stack — class extends Stack, define resources in constructor. Validate on the free tier first, then wire into production.

What is the #1 pitfall with AWS CDK Infrastructure Code?

The most frequent issue is: Synth non-deterministic — use cdk.json context, pin versions for reproducible builds. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for AWS CDK Infrastructure Code?

Best practices for AWS CDK Infrastructure Code include: Use L2 constructs — sensible defaults, security best practices built-in; Write construct tests — jest snapshots catch unintended infra changes; Use Aspects for cross-cutting — tag all resources, enforce encryption globally. The first one — "Use L2 constructs — sensible defaults, security best practices built-in" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with AWS CDK Infrastructure Code?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most AWS CDK Infrastructure Code options offer a free tier — validate within free quota before scaling.