htadev.me

What is AWS Cloud Development Kit(CDK) AWS?

December 26, 2024 | by [email protected]

cdk-logo6-1260×476

AWS Cloud Development Kit (CDK)

The AWS Cloud Development Kit (AWS CDK) is an open-source software framework designed to define cloud application resources using familiar programming languages. It simplifies the process by compiling AWS CDK applications into CloudFormation templates and deploying infrastructure on the AWS Cloud.

The first public beta version of AWS CDK (v.0.8.0) was released in 2018, introducing CDK as an alternative approach to deploying AWS resources using code written in TypeScript.

AWS CDK is an open-source project. Its Generally Available (GA) version, AWS CDK v2, was officially announced in December 2021 at the re:Invent event.

AWS CDK v2 will support languages:

Programming LanguageStability
TypeScriptStable
JavaScriptStable
PythonStable
JavaStable
C#/.NETStable
GoExperimental

Overview CDK

An AWS CDK application consists of three main components (basic building blocks):

  1. App: The core structure that combines all stacks in the application and handles deployment to AWS.
  2. Stack: Similar to a CloudFormation stack, this is a unit (template) that contains AWS resources in the form of constructs and can be used for deployment.
  3. Construct: The basic building block that holds one or more AWS resources. You can customize and combine resources to create your constructs.

AWS CDK helps you generate source code. Using the AWS CDK CLI, which acts as a “translator,” this code is synthesized into a CloudFormation template, essentially serving as an “assembly language” that contains all the components of the system.

This template can then be deployed to AWS using CloudFormation, which acts as the “processor.”

In addition to the AWS CDK Core framework and AWS CDK CLI, AWS also provides the AWS Construct Hub Library—a library that aggregates CDK constructs for AWS services. Construct Hub is a resource that helps you discover additional constructs from AWS, third parties, and the open-source community.

Overview AWS CDK Construct

As shown in the diagram above, multiple stacks are composed of various constructs, each containing different AWS resources. Constructs are arguably the most convenient feature that AWS CDK offers to developers.

There are three ways to build constructs in AWS CDK:

1. L1 Constructs

  • These are essentially AWS resources that map directly to CloudFormation resources, with attributes that correspond to those in CloudFormation.
  • If you only use L1 Constructs, it’s essentially no different from using CloudFormation directly. However, if L2 or L3 Constructs don’t allow you to customize a specific property, you can fall back on L1 Constructs.

2. L2 Constructs

  • Developed by the AWS CDK team, these constructs wrap L1 resources and apply best practices in terms of security and optimized default configurations.
  • Think of L2 Constructs as “boilerplates” for AWS resources, where you don’t need deep knowledge to create them on AWS.
  • Main benefit: You can create AWS resources securely and efficiently with just a few lines of code.

3. L3 Constructs / Patterns

  • Also known as patterns, these are collections of L2 Constructs grouped to provide complete solutions.
  • For example, you can combine multiple resources to create a 3-tier web application: EC2 instances for backend and frontend linked with RDS, and an Application Load Balancer (ALB) for load balancing.
  • With just a few lines of code, you can initialize these resource groups, making it easy to reuse these patterns in different projects or stacks.

4. In summary:

  • L1: Direct mapping to CloudFormation—basic but customizable.
  • L2: Simplifies resource creation with best practices and default settings.
  • L3: Full solutions combining multiple resources for common use cases.

Features of AWS CDK

  1. Supports multiple popular programming languages
    AWS CDK allows developers to use familiar languages such as Python, TypeScript, Java, C#, and others to define infrastructure.
  2. DRY (Don’t Repeat Yourself)
    Avoid redundant code by leveraging L2 and L3 Constructs to reuse resource groups or configurations frequently used in your project.
  3. Generate large CloudFormation templates with minimal code
    With just a few lines of code, you can create large templates and easily template complex structures or systems that can be reused across projects.
  4. Store your infrastructure configuration in a repository
    Keep your infrastructure setup and configuration in a single repository, managed by a single AWS CDK application for better version control and collaboration.
  5. Open-source contributions and feedback
    As an open-source project, AWS CDK allows you to contribute directly to its development and provide feedback to the AWS team.
  6. Leverage familiar programming languages and logic
    Write deployment logic and unit tests using the language you’re comfortable with. For example, you can use loops to create multiple similar resources, such as 15 Lambda functions, without repeating code.

AWS CDK with CloudFormation

Key Differences:

  1. L2/L3 Constructs with Best Practices
    AWS CDK provides L2/L3 Constructs that implement best practices for security and performance, making development faster and safer than manually configuring every resource setting in CloudFormation.
  2. Programming Languages Instead of YAML/JSON
    AWS CDK lets you use programming languages like Python, TypeScript, Java, etc instead of YAML or JSON. Writing in a real programming language is more powerful, and beneficial for your career than to just writing YAML for CloudFormation.
  3. Simplified Code
    A famous selling point of AWS CDK: “1500 lines of CloudFormation became 14 lines of CDK. It’s important to understand that the deployed application still has 1500 lines’ worth of operations and maintenance ownership, not 14.”
    Ben Kehoe (@ben11kehoe)

Key Considerations

  1. Still Generates CloudFormation Templates
    AWS CDK ultimately compiles into a CloudFormation template. It’s essential to use tools like cfn-lint or Checkov to prevent misconfigurations in your system.
  2. Understand the Foundation (CloudFormation)
    While AWS CDK abstracts CloudFormation, understanding how CloudFormation works is crucial. Without this knowledge, debugging or explaining why your AWS CDK app isn’t working can be time-consuming. Having a solid foundation in CloudFormation concepts is highly beneficial.
  3. Large community
    AWS CDK benefits from a large community and a dedicated Construct library for reuse

Best Practices for Deploying AWS CDK Applications

  1. Create Multiple Environments Using Separate AWS Accounts
    Set up distinct AWS accounts for different environments such as Dev, Test, Stage, and Prod to ensure isolation and reduce risks during deployment.
  2. Add Automated Integration Tests in Your CI/CD Pipeline
    Incorporate integration tests into your pipeline to minimize the chances of breaking changes affecting your application.
  3. Use cdk diff to Review Changes Before Deployment
    Always run cdk diff to examine and review changes before deploying them to production, ensuring you understand the impact.
  4. Separate Stateful and Stateless Constructs
    • Stateful Constructs: Include resources like databases and storage, which retain critical data. Enable termination protection for these to prevent accidental deletion.
    • Stateless Constructs: Include resources like APIs, ECS clusters, and monitoring tools. These can be safely deleted or recreated without risk of data loss.
    By separating these resources into different stacks, you can:
    • Protect critical data (stateful resources).
    • Easily manage stateless resources without worrying about data integrity.
  5. Avoid Renaming Stateful Resources
    • Renaming stateful resources (e.g., databases or storage) often leads to resource replacement, causing potential data loss.
    • Place such resources in dedicated constructs or stacks that are less likely to be renamed or moved.
  6. Leverage Programming Language Logic Instead of CloudFormation Logic
    Use the programming language’s native control structures (like loops, conditions, and variables) to define your constructs during the CDK synthesis process. Avoid relying on CloudFormation-specific logic such as Conditions, {Fn:If} or Parameters, which can complicate deployments.
  7. Refer to Official Documentation for Additional Best Practices
    Regularly check AWS CDK’s official documentation for updates and guidance on best practices to keep your deployments optimized and secure.

Programming Tools for AWS CDK

Although AWS CDK already saves a lot of development time, the following tools can further enhance your workflow and boost productivity:

  1. AWS Toolkit for VS Code
    • A plugin for Visual Studio Code that provides a tree-like view of your AWS CDK application’s resources.
    • Makes it easier to explore, manage, and deploy AWS resources directly from the IDE.
    • AWS Toolkit for VS Code
  2. Projen
    • A project generator for AWS CDK that sets up a new project using best practices and standard configurations.
    • Helps you quickly kick-start projects aligned with industry standards.
    • Projen Documentation
  3. IntelliCode
    • An AI-powered tool that offers intelligent code completion while building Constructs in your preferred programming language.
    • Speeds up coding by suggesting context-aware completions for constructs and CDK-specific APIs.
  4. CDK-nag
    • A tool for validating your AWS CDK application against predefined rules and best practices.
    • Can be integrated into your CI/CD pipeline to enforce the highest standards for AWS resource configurations during build and deployment.
    • cdk-nag Documentation

Summary

In this article, you’ve learned the basics of AWS CDK and how it enhances your workflow when building and managing AWS resources. You’ve also gained insight into how it integrates with CloudFormation, its features, and the benefits it offers.

Additionally, you’ve explored best practices and tools to help you build systems that adhere to the highest standards without requiring excessive time and effort.

If you’re interested in diving deeper, check out AWS CDK examples and start building real-world applications using AWS CDK in the upcoming lessons. Happy coding! 🚀

References

RELATED POSTS

View all

view all