Configs2026年4月16日·1 分钟阅读

AWS CDK — Define Cloud Infrastructure Using Real Programming Languages

The AWS Cloud Development Kit lets you define cloud infrastructure in TypeScript, Python, Java, Go, or C# instead of YAML templates. CDK synthesizes your code into CloudFormation and deploys it with a single command.

Introduction

AWS CDK (Cloud Development Kit) lets you define AWS infrastructure using familiar programming languages instead of declarative YAML or JSON. You write TypeScript, Python, Java, Go, or C# classes that represent cloud resources, and CDK synthesizes them into CloudFormation templates. This brings IDE autocompletion, type checking, loops, and abstractions to infrastructure as code.

What AWS CDK Does

  • Models AWS resources as object-oriented constructs in your preferred language
  • Synthesizes high-level code into CloudFormation templates automatically
  • Deploys, updates, and destroys stacks with cdk deploy and cdk destroy
  • Provides an L2 construct library with sensible defaults for 200+ AWS services
  • Supports reusable patterns through custom constructs published to package registries

Architecture Overview

CDK operates in three layers. L1 constructs are auto-generated one-to-one mappings of every CloudFormation resource. L2 constructs add opinionated defaults, helper methods, and cross-resource wiring (e.g., granting a Lambda function read access to an S3 bucket). L3 constructs (patterns) compose multiple resources into common architectures. The CDK CLI runs synthesis locally, producing a CloudFormation template and asset bundle, then deploys via the CloudFormation API.

Self-Hosting & Configuration

  • Install the CLI with npm install -g aws-cdk and bootstrap your account with cdk bootstrap
  • Initialize projects with cdk init choosing TypeScript, Python, Java, Go, or C#
  • Configure AWS credentials via environment variables, profiles, or IAM roles
  • Use cdk.json to set context values, feature flags, and synthesis options
  • Organize large projects with CDK Pipelines for CI/CD-driven multi-account deployments

Key Features

  • Full programming language power: loops, conditionals, type safety, and IDE support
  • L2 constructs with smart defaults reduce boilerplate by 60-80% vs raw CloudFormation
  • Asset bundling automatically packages Lambda code, Docker images, and static files
  • CDK Pipelines: self-mutating CI/CD pipeline that deploys your CDK app across stages
  • Construct Hub: public registry of 1,500+ reusable community constructs

Comparison with Similar Tools

  • Terraform — Multi-cloud HCL-based IaC; CDK is AWS-focused with real language support
  • Pulumi — Multi-cloud IaC in real languages; CDK generates CloudFormation, Pulumi uses its own engine
  • CloudFormation — CDK's compilation target; writing raw CFN is more verbose and error-prone
  • AWS SAM — Serverless-focused subset of CloudFormation; CDK covers all AWS services
  • CDKTF — CDK for Terraform; uses CDK constructs but targets Terraform providers instead of CloudFormation

FAQ

Q: Does AWS CDK work with non-AWS clouds? A: CDK itself targets AWS CloudFormation. For multi-cloud, look at CDKTF (CDK for Terraform) or Pulumi which share similar concepts.

Q: Is there a cost to using CDK? A: CDK is free and open source. You only pay for the AWS resources it provisions. CloudFormation itself has no additional charge.

Q: Can I import existing AWS resources into a CDK stack? A: Yes. Use cdk import to bring existing resources under CDK management, or reference them with fromXxx lookup methods.

Q: How does CDK handle state? A: CDK relies on CloudFormation for state management. Each stack's state is stored in CloudFormation, so there is no separate state file to manage.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产