aws-cdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAWS CDK
AWS CDK
Overview
概述
Domain expertise for CDK construct authoring, deployment workflows, compliance, drift, importing resources, safe refactoring, and troubleshooting CDK CLI / CloudFormation errors.
When NOT to use: Raw CloudFormation YAML/JSON. SAM. Terraform/Pulumi. CI/CD beyond CDK Pipelines. Use builtin knowledge or specialized skills for these.
专注于CDK构造编写、部署工作流、合规性、漂移问题、资源导入、安全重构,以及排查CDK CLI / CloudFormation错误的领域专业知识。
不适用于以下场景: 原生CloudFormation YAML/JSON、SAM、Terraform/Pulumi、超出CDK Pipelines范畴的CI/CD。这些场景请使用内置知识或专门技能。
Critical Warnings
重要警告
Deadly embrace: Removing a cross-stack reference deadlocks deployment. Two-deploy fix required: (1) remove consumer import + add on producer, deploy; (2) remove , deploy again. See troubleshooting-deployment.
this.exportValue()exportValue()Construct ID changes cause replacement: Renaming/moving a construct changes its logical ID → CloudFormation replaces the resource (data loss for stateful resources). Always before deploy. See refactor-and-prevent-replacement.
cdk diffUPDATE_ROLLBACK_FAILED: Stack is stuck. Fix with or . See troubleshooting-deployment.
cdk rollback $STACKcdk rollback $STACK --orphan <LogicalId>Non-empty S3 buckets persist after destroy: You MUST set both and . Versioned buckets are worse — delete markers persist even after apparent deletion.
removalPolicy: DESTROYautoDeleteObjects: true致命死锁: 移除跨堆栈引用会导致部署死锁。需要分两次部署修复:(1) 移除消费者的导入,并在生产者端添加,执行部署;(2) 移除,再次执行部署。详见troubleshooting-deployment。
this.exportValue()exportValue()构造ID变更会触发资源替换: 重命名或移动构造会改变其逻辑ID → CloudFormation将替换该资源(有状态资源会丢失数据)。部署前务必执行。详见refactor-and-prevent-replacement。
cdk diffUPDATE_ROLLBACK_FAILED: 堆栈陷入停滞状态。使用或修复。详见troubleshooting-deployment。
cdk rollback $STACKcdk rollback $STACK --orphan <LogicalId>非空S3存储桶在销毁后仍会保留: 必须同时设置和。启用版本控制的存储桶情况更糟——即使看似已删除,删除标记仍会保留。
removalPolicy: DESTROYautoDeleteObjects: trueCommon Workflows
常见工作流
| Task | Quick Command | Details |
|---|---|---|
| Bootstrap | | bootstrap-and-project-setup |
| New TS project | | bootstrap-and-project-setup |
| New Python project | | bootstrap-and-project-setup |
| Deploy | | Always diff before deploy to prod |
| cdk-nag | | compliance-and-drift |
| Drift | | compliance-and-drift |
| Import resource | | import-and-migrate |
| Refactor safely | | refactor-and-prevent-replacement |
| 任务 | 快速命令 | 详情 |
|---|---|---|
| 引导环境 | | bootstrap-and-project-setup |
| 创建新TS项目 | | bootstrap-and-project-setup |
| 创建新Python项目 | | bootstrap-and-project-setup |
| 部署 | | 生产环境部署前务必执行diff |
| cdk-nag检查 | | compliance-and-drift |
| 漂移检测 | | compliance-and-drift |
| 导入资源 | | import-and-migrate |
| 安全重构 | | refactor-and-prevent-replacement |
Troubleshooting
故障排查
| Error | Cause → Fix |
|---|---|
| DeployFailed / DeploymentError | CDK error is not root cause. Check CFN events: |
| NoCredentials / ExpiredToken / AssumeRoleFailed | |
| Asset errors (CannotFindAsset, FailedToBundleAsset, AssetBuildFailed, AssetPublishFailed) | Path wrong, Docker not running, or bootstrap bucket perms. Use |
| AppRequired | Add |
| AnnotationErrors | Fix the underlying issue; suppress with |
| ConcurrentReadLock / ConcurrentWriteLock | |
| BootstrapVersionValidation | Re-bootstrap. Match |
| DependencyCycle | Extract shared resource into third stack or use SSM for late-binding. Details |
| UnresolvedAccount | Set explicit |
| NoStacksMatched | CDK uses logical ID (2nd constructor arg), not CFN name. |
| Cannot find module (synth time) | Run |
| V1 import paths / duplicate aws-cdk-lib | V1 |
| Lambda Cannot find module (runtime) | Wrong handler value, missing SDK v3 migration, Python deps not bundled. Details |
| API Gateway multi-stage conflicts | Set |
| 错误 | 原因→修复 |
|---|---|
| DeployFailed / DeploymentError | CDK错误并非根本原因。检查CFN事件: |
| NoCredentials / ExpiredToken / AssumeRoleFailed | 执行 |
| 资源错误(CannotFindAsset、FailedToBundleAsset、AssetBuildFailed、AssetPublishFailed) | 路径错误、Docker未运行、引导存储桶权限问题。使用 |
| AppRequired | 在 |
| AnnotationErrors | 修复底层问题;仅在万不得已时使用 |
| ConcurrentReadLock / ConcurrentWriteLock | 删除 |
| BootstrapVersionValidation | 重新执行引导操作。确保所有地方的 |
| DependencyCycle | 将共享资源提取到第三个堆栈,或使用SSM进行延迟绑定。详情 |
| UnresolvedAccount | 在堆栈上显式设置 |
| NoStacksMatched | CDK使用逻辑ID(构造函数的第二个参数),而非CFN名称。执行 |
| Cannot find module(合成阶段) | 执行 |
| V1 import paths / duplicate aws-cdk-lib | 使用了V1版本的 |
| Lambda Cannot find module(运行时) | 处理程序值错误、未完成SDK v3迁移、Python依赖未打包。详情 |
| API Gateway multi-stage conflicts | 在 |
Construct Patterns
构造模式
Prefer L2. Use L1 with Mixins/Facades when L2 lacks a property. Escape hatches: → . See construct-patterns.
node.defaultChildaddPropertyOverride优先使用L2构造。当L2缺少所需属性时,结合Mixins/Facades使用L1构造。逃生舱: → 。详见construct-patterns。
node.defaultChildaddPropertyOverrideAdditional Resources
额外资源
- Search AWS documentation for "CDK Developer Guide", "CDK API Reference" and "CDK Pipelines" respectively
- 在AWS文档中分别搜索「CDK Developer Guide」「CDK API Reference」和「CDK Pipelines」
Security Considerations
安全注意事项
- OIDC for CI/CD credentials (no static keys)
- on bootstrap
--custom-permissions-boundary - for inter-resource IAM
grant*() - +
cdk-nagin CI--strict - Stateful resources in own stack with
terminationProtection: true - Commit
cdk.context.json
- 为CI/CD凭证使用OIDC(无静态密钥)
- 引导时使用参数
--custom-permissions-boundary - 资源间IAM权限使用方法
grant*() - CI中使用+
cdk-nag参数--strict - 有状态资源放在单独堆栈中,并设置
terminationProtection: true - 提交文件
cdk.context.json