AWS CDK のアイコン

AWS CDK Popular2019年〜

An IaC framework that lets you define AWS infrastructure using programming languages like TypeScript and Python

What It Does

AWS CDK (Cloud Development Kit) is an Infrastructure as Code (IaC) framework that lets you define AWS infrastructure using general-purpose programming languages such as TypeScript, Python, Java, C#, and Go. You can leverage the full power of programming languages - conditionals, loops, functions - to describe your infrastructure, and the code is compiled into CloudFormation templates for deployment. High-level abstractions called Constructs allow you to build complex infrastructure with minimal code.

Use Cases

CDK is used for defining serverless application infrastructure, setting up microservices architectures, automating CI/CD pipeline creation, and managing infrastructure across multiple environments (development, staging, production). It is especially well-suited for developers who are comfortable with programming and want to build infrastructure without writing YAML/JSON.

Everyday Analogy

Think of it like LEGO blocks. If CloudFormation is assembling individual small blocks (individual resources) one by one, CDK is combining pre-built part sets (Constructs). Choose a "house set" (web app configuration) and the walls, roof, and door (VPC, ALB, ECS) all come together, letting you finish with fewer steps.

What Is AWS CDK?

AWS CDK is a framework for defining AWS infrastructure using programming languages. Traditional CloudFormation requires writing resources in YAML or JSON, but CDK lets you use familiar languages like TypeScript or Python. With IDE autocompletion and compile-time type checking, you can catch configuration errors early and significantly improve development efficiency.

How Constructs Work

At the core of CDK are Constructs - reusable components. L1 Constructs map one-to-one with CloudFormation resources. L2 Constructs are higher-level abstractions with built-in best practices. For example, using the L2 Bucket class, you can create an S3 bucket with proper encryption and versioning in just a few lines. L3 Constructs (Patterns) bundle multiple resources into a single component.

Relationship with CloudFormation

Code written in CDK is converted (synthesized) into CloudFormation templates using the cdk synth command. Deployment is handled through CloudFormation, so you can use all of CloudFormation's features like stack management, change sets, and rollbacks. CDK is a "higher layer" on top of CloudFormation - it doesn't replace CloudFormation but makes it easier to use. To learn the fundamentals and advanced topics of the CDK-CloudFormation relationship, check out technical books on Amazon.

Getting Started

Install the CDK CLI with npm install -g aws-cdk, then create a new project with cdk init app --language typescript. Define resources in the stack file under the lib/ directory and deploy with cdk deploy. For the first time, you need to run cdk bootstrap to create the S3 bucket and IAM roles used by CDK. Use cdk diff to preview changes before deploying.

Things to Watch Out For

  • CDK itself is a free open-source tool. You only pay for the AWS resources that are deployed.
  • CDK version upgrades may include breaking changes, so pinning versions and reviewing changelogs is recommended.
  • cdk bootstrap needs to be run once per AWS account and region combination. In team development, only one person needs to run it initially.
共有するXB!