AWS in 1 Min · Episode 168

AWS CDK explained: infrastructure in real code

Infrastructure in real code. Watch the one-minute video, then read the details.

David Gomez1:16 video · 2 min read

"Infrastructure code has to be YAML." Not with the CDK. CloudFormation templates are precise, but large ones get long and repetitive: the same subnet, role and alarm blocks again and again, with little room for reuse. The AWS Cloud Development Kit (CDK) lets you write the same infrastructure in a real programming language.

What the CDK is

The AWS CDK is an open-source framework for defining infrastructure in TypeScript, JavaScript, Python, Java, C# or Go. You get everything a language offers: variables, loops, functions, classes, packages, unit tests and your editor's autocomplete. Under the hood, the CDK still deploys through AWS CloudFormation, so you keep stacks, change sets and rollback from Part 1 and Part 2.

Constructs

You build CDK apps from constructs, in three levels:

  • L1 constructs map one to one to CloudFormation resources (CfnBucket), with every property exposed.
  • L2 constructs are curated: sensible, secure defaults and helper methods. bucket.grantRead(fn) writes the IAM policy for you.
  • L3 constructs, or patterns, combine many resources. ApplicationLoadBalancedFargateService creates a load-balanced container service, and with a new VPC the docs' example produces dozens of resources from a few lines.

Teams can publish their own constructs, for example a "standard service" with logging, alarms and tags built in, so good practice is reused instead of copied.

Synth, diff, deploy

  1. cdk synth runs your code and produces a CloudFormation template.
  2. cdk diff compares it with what is deployed and shows what will change, including IAM and security group changes.
  3. cdk deploy deploys it as a normal CloudFormation stack.

Before the first deployment in an account and Region, cdk bootstrap creates the resources the CDK needs, such as an S3 bucket for assets.

A common mistake is treating the CDK as "just code" and skipping review of what it generates. A refactor that renames a construct can change its logical ID, which CloudFormation sees as delete and create. cdk diff shows that before it happens.

Where AI fits

Coding assistants are fluent in the CDK. It is ordinary, typed code, so the compiler and the editor catch many mistakes an assistant makes, and type definitions guide it to valid properties. The open-source AWS IaC MCP server adds tools that search CDK documentation, find samples and constructs, and return CDK best practices, so an assistant works from current information rather than from memory. Keep the same habit as with templates: run cdk diff, and read it, before every deploy.

Quick recap

  • Real code, not YAML: TypeScript, Python, Java, C#, Go or JavaScript.
  • Constructs bring safe defaults, from L1 resources to L3 patterns.
  • It deploys through CloudFormation: synth, diff, then deploy.
AWS in 1 Min #168 · 1:16

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS