Skills2026年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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
AWS CDK
直接安装命令
npx -y tokrepo@latest install b9fb8a8c-398f-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Define AWS infrastructure in TypeScript, Python, Java, Go, or C# instead of YAML. CDK synthesizes to CloudFormation and deploys with one command.
§01

What it is

The AWS Cloud Development Kit (CDK) lets you define cloud infrastructure using familiar programming languages instead of YAML or JSON templates. Write TypeScript, Python, Java, Go, or C# code that describes your AWS resources, and CDK synthesizes it into CloudFormation templates and deploys them.

CDK provides high-level constructs that bundle multiple AWS resources with best-practice defaults. A single L2 construct can create a Lambda function with its IAM role, log group, and API Gateway integration.

§02

How it saves time or tokens

CloudFormation YAML is verbose and error-prone. A Lambda function with API Gateway can take 100+ lines of YAML. The same setup in CDK takes 10 lines of TypeScript with sensible defaults for IAM, logging, and error handling.

For AI code generation, CDK's type-safe interfaces mean LLMs can produce correct infrastructure code with autocomplete-friendly APIs. TypeScript CDK code benefits from full type checking before deployment.

§03

How to use

  1. Install CDK and create a project:
npm install -g aws-cdk
cdk init app --language typescript
  1. Define infrastructure in code:
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';

export class MyApiStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string) {
    super(scope, id);

    const fn = new lambda.Function(this, 'Handler', {
      runtime: lambda.Runtime.PYTHON_3_12,
      code: lambda.Code.fromAsset('lambda'),
      handler: 'index.handler',
    });

    new apigateway.LambdaRestApi(this, 'Api', { handler: fn });
  }
}
  1. Deploy:
cdk deploy
  1. CDK synthesizes CloudFormation, creates the stack, and provisions all resources.
§04

Example

// CDK creates IAM role, log group, and API Gateway automatically
const fn = new lambda.Function(this, 'Processor', {
  runtime: lambda.Runtime.NODEJS_20_X,
  code: lambda.Code.fromAsset('src'),
  handler: 'processor.handler',
  timeout: cdk.Duration.seconds(30),
  memorySize: 256,
});

// Grant the function read access to an S3 bucket
bucket.grantRead(fn);
§05

Related on TokRepo

§06

Common pitfalls

  • Not running cdk diff before cdk deploy. Always review the change set to understand what resources will be created, modified, or destroyed.
  • Using L1 constructs when L2 exists. L1 constructs are raw CloudFormation mappings. L2 constructs provide higher-level abstractions with best-practice defaults. Always check for L2 first.
  • Ignoring CDK context and environment separation. Use cdk.json context and environment-specific stacks to manage dev, staging, and production deployments cleanly.
  • Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.

常见问题

How does CDK compare to Terraform?+

CDK uses real programming languages with loops, conditions, and abstractions. Terraform uses HCL, a domain-specific language. CDK is AWS-only (though CDK for Terraform exists). Terraform supports all major cloud providers. CDK generates CloudFormation; Terraform has its own state management.

Can I use CDK with Python?+

Yes. CDK supports Python as a first-class language. Install aws-cdk-lib via pip and write stacks in Python. The API is identical in structure to TypeScript, with Python naming conventions.

What are CDK constructs?+

Constructs are the building blocks of CDK. L1 constructs map 1:1 to CloudFormation resources. L2 constructs provide higher-level abstractions with defaults. L3 constructs (patterns) bundle multiple resources for common architectures like API + Lambda + DynamoDB.

Does CDK handle IAM automatically?+

Yes. CDK generates least-privilege IAM policies automatically when you use grant methods (bucket.grantRead, table.grantWriteData). You do not need to write IAM policy JSON manually for most use cases.

Can CDK manage existing AWS resources?+

CDK can import existing resources into a stack using from* methods (like Bucket.fromBucketName). However, CDK cannot modify resources it did not create unless you explicitly import them into the stack.

引用来源 (3)

讨论

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

相关资产