Cloud Development Kit (CDK)

Icon

CDK Icon

About

  • Define your cloud infrastructure using familiar language.

    • Supported languages includes Java, Python, Typescript/Typescript, .NET.

  • Contains high level components called constructs.

  • The code is then compiled into a CloudFormation Template(CFT) (json/yaml). So this is step above CFT.

  • You can therefore deploy infrastructure and application runtime code together.

    • It is great for Lambda functions

    • Great for docker container ECS/EKS

CDK and SAM

  • CDK and SAM can be used to test your CDK apps locally.

  • For this first run cdk synth command. This will generate a CFT.

  • Secondly run sam local invoke -t <template file> <function-name> to call the function.

CDK construct

  • CDK construct is a component that encapsulates everything CDK needs to create the final CloudFormation stack.

  • Can represent a single or group of AWS resources.

  • Construct Hub is a community that contains AWS, third-party and open source CDKs.

  • Constructs are generated based on CloudFormation Resource Specification.

  • The name of construct starts with Cfn.

Levels of CDK

Level
Name
Description

Level 1

CFN Resources

Represents all resources directly available in Cloud Formation

Level 2

Intent Based

Similar to Level 1, but with sensible defaults and boilerplates, provides method

Level 3

Pattern based

Represent multiple related resources, helps to complete most common tasks

CDK Testing

  • To test CDK apps, use CDK Assertions module, combined with popular test frameworks like Jest or Pytest.

  • Two types of tests

    • Fine-grained Assertions

    • Snapshot Tests

Working with CDK CLI

  1. Install the CDK, requires nodejs.

        sudo npm install -g aws-sdk-lib
  2. Initialize the CDK application

        cdk init app --language <language>
        ## Example
        cdk init app --language javascript
  3. Returns CDK app stack

        cdk ls
  4. Add your code to the CDKAppStack created in step 3 to the file cdk-app-stack.js.

  5. Make the required setup based on your infrastructure added in step 4.

  6. Run the command cdk bootstrap once per account per region. This will use cdk.json file present in the root of directory to create the necessary CDKToolKit and necessary resources like AWS S3 and IAM roles before it can deploy into an AWS environment.

    • AWS Environment = account and region

  7. Run the command cdk synth optionally to generate the CFT.

  8. Run the command cdk deploy to deploy the application.

  9. Run the command cdk destroy to destroy the stack created in deployment.

  10. cdk diff will give you difference between Local CDK and deployed stack.

Last updated