What Is CDK?

Course Objectives: Understand what is CDK and what are the advantages of using it Understand the basic concepts in CDK (Contsructs, Apps, and Stacks) Understand the development environment of CDK (basic CDK commands, TypeScript) Build basic HTTP Service using CDK
Estimated Time: 60 minutes
Target Audience: Software Developers

1. Why Infrastructure as Code is Awesome

  1. Scalability - Write it once and then reuse it many times. Deploy globally and replicate horizontally, etc.
  2. Change Management - Gives more complete visibility of configuration changes. Ability to rollback to a previous states.
  3. Automation - Gives us the ability to automate setup and configuration tasks in a reproducible way.
  4. Security & Best Practices - Gives us the ability to define a secure architecture following best practices once and re-use multiple times.
  5. Abstraction - With advancements like CDK we have the ability to abstract common infrastructure patterns in an easily reproducible fashion. Allowing application developers to focus more directly on meeting their customer needs and less on infrastructure details.

2. What is CDK?

The AWS CDK is a new software development framework from AWS with the sole purpose of making it fun and easy to define cloud infrastructure in your favorite programming language and deploy it using AWS CloudFormation.

What is CDK

Advantages of using CDK:

  • Higher level abstraction
  • Easy to share
  • All the benefits of CloudFormation
  • Available in a variety of languages - JavaScript, TypeScript, Python. Java and C# are coming soon

3. Example of a CDK Project

Let’s look at a small but complete example for a Fargate Cluster.

import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import ecs_patterns = require('@aws-cdk/aws-ecs-patterns');
import cdk = require('@aws-cdk/core');

// A Stack
class HelloFargate extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    
    // VPC Construct
    const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2 });

    // Cluster Construct
    const cluster = new ecs.Cluster(this, 'Cluster', { vpc });

    // Load Balanced Fargate Construct
    const fargateService = new ecs_patterns.LoadBalancedFargateService(this, "FargateService", {
      cluster,
      image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
    });

    // Output the DNS where you can access your service
    new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: fargateService.loadBalancer.loadBalancerDnsName });
  }
}

//An App
const app = new cdk.App();

new HelloFargate(app, 'Hello');

4. Definitions

  • App - A collection of related stacks.
  • Stack - The set of AWS resources that are created and managed as a single unit when AWS CloudFormation instantiates a template
  • Construct - Everything defined in the CDK is a Construct. It can be thought of as a re-usable “Cloud Component” representing anything from a single AWS resource to architectures of arbitrary complexity.