The AWS CDK for Beginners

The AWS Cloud Development Kit (CDK) is an open source software development framework for defining cloud infrastructure in code and provisioning it through AWS.

With CDK, you can use familiar programming languages like TypeScript, Python, Java etc. to model your cloud resources. CDK handles converting your code into AWS infrastructure through CloudFormation.

This lets you leverage the power of programming to create reusable infrastructure components. You can deploy and manage them using the CDK toolkit locally, without needing the AWS console.

Now let's dive into the key benefits of using CDK as a developer...

Why Use CDK?

For developers, CDK offers 3 major benefits:

1. Infrastructure as Code

You can define your AWS resources in a familiar programming language instead of JSON/YAML. This unlocks the full power of abstraction, modularity, and reuse.

For example, here's how you could create an S3 bucket using TypeScript CDK:

import { Bucket, BucketEncryption } from "aws-cdk-lib/aws-s3"
import { Construct } from "constructs"

export class MyBucket extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id)

    new Bucket(this, "MyBucket", {
      encryption: BucketEncryption.KMS_MANAGED,
      versioned: true,
    })
  }
}

Much easier to digest than YAML!

2. Higher-Level Abstractions

The CDK includes "Constructs" which are reusable cloud components. You can stitch together constructs instead of repeating boilerplate code.

For example, an auto-scaling Fargate service:

import { FargateService } from "aws-cdk-lib/aws-ecs-patterns"

const fargateService = new FargateService(this, "MyFargateService", {
  cluster,
  taskDefinition,
})

fargateService.autoScaleTaskCount({ minCapacity: 2, maxCapacity: 10 })

This abstracts all the complex plumbing. Many services can be easily linked together, ready to deploy (or destroy... and re-deploy!) in a matter of seconds.

3. Local Tooling

The CDK toolkit lets you work locally without needing the AWS console:

  • cdk synth to emit infrastructure-as-code
  • cdk diff to compare local vs deployed stacks
  • cdk deploy to provision stacks to your account

Overall, CDK enables developers to use their existing languages and tools to build reusable infrastructure patterns.

A Simple CDK App

Let's see a complete CDK app example.

We'll make a stack with an S3 bucket, Lambda function, and trigger to run the function when a file lands in the bucket:

import { Bucket, EventType } from "aws-cdk-lib/aws-s3"
import { Function, Code, Runtime } from "aws-cdk-lib/aws-lambda"
import { LambdaDestination } from "aws-cdk-lib/aws-s3-notifications"
import { Construct } from "constructs"

export class CdkStack extends Construct {
  constructor(scope: Construct, id: string) {
    super(scope, id)

    // S3 bucket
    const bucket = new Bucket(this, "MyBucket")

    // Lambda function
    const fn = new Function(this, "LambdaHandler", {
      runtime: Runtime.NODEJS_14_X,
      handler: "handler.handler",
      code: Code.fromAsset("lambda"),
    })

    // Trigger
    const notification = new LambdaDestination(fn)
    bucket.addEventNotification(EventType.OBJECT_CREATED, notification)
  }
}

We can deploy this stack to AWS via cdk deploy. The Lambda will run each time a file lands in the bucket.

This shows CDK's value in letting us define reusable patterns in code.

Getting Started

To start using the AWS CDK:

  • Install the CDK toolkit globally via npm install -g aws-cdk
  • Initialize a new CDK project via cdk init app --language=typescript
  • Write your stacks and constructs
  • Use cdk synth and cdk deploy to provision them

See the CDK Developer Guide for in-depth tutorials.

AWS CDK enables developers to use the power of programming to define cloud infrastructure. With its abstractions, local tooling, and integration with modern languages, it unlocks new agility and productivity.


Profile picture

Maintained by Caleb Waymeyer