| name | cdk |
| description | Manage AWS CDK applications, stacks, synthesis, deployment, and bootstrapping via CDK CLI. |
| metadata | {"openclaw":{"emoji":"🏗️","requires":{"bins":["cdk"]}}} |
AWS CDK
Use this skill for infrastructure-as-code with AWS CDK: initializing projects, synthesizing CloudFormation templates, deploying stacks, managing environments, and troubleshooting CDK applications.
Prerequisites
- AWS CDK CLI installed (
npm install -g aws-cdk)
- AWS CLI v2 configured with valid credentials
- Node.js (for TypeScript/JavaScript projects) or Python/Java/.NET for other languages
- CDK bootstrap completed for target account/region
Common Operations
Inspect and Status (Read-Only)
cdk --version
cdk list
cdk synth <stack-name>
cdk diff <stack-name>
cdk diff --all
cdk synth <stack-name> --quiet
cdk context
cdk metadata <stack-name>
cdk doctor
Bootstrap
cdk bootstrap
cdk bootstrap aws://<account-id>/<region>
cdk bootstrap --qualifier <qualifier> aws://<account-id>/<region>
cdk bootstrap --custom-permissions-boundary <policy-name>
cdk bootstrap aws://<target-account>/<region> \
--trust <ci-cd-account-id> \
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess
Initialize Projects
cdk init app --language typescript
cdk init app --language python
cdk init sample-app --language typescript
cdk init --list
Deploy
⚠️ Cost note: CDK itself is free. Costs come from the AWS resources you deploy. Always cdk diff before deploying to understand what resources will be created.
cdk deploy <stack-name>
cdk deploy --all
cdk deploy <stack-name> --require-approval never
cdk deploy <stack-name> --parameters Param1=value1 --parameters Param2=value2
cdk deploy <stack-name> --outputs-file outputs.json
cdk deploy Stack1 Stack2
cdk deploy <stack-name> --hotswap
cdk watch <stack-name>
cdk deploy <stack-name> --no-rollback
cdk deploy <stack-name> --notification-arns <sns-topic-arn>
Destroy / Destructive
🛑 DESTRUCTIVE — Always confirm with the user before executing any of these commands.
cdk destroy <stack-name>
cdk destroy --all
cdk destroy <stack-name> --force
cdk context --clear
⚠️ cdk destroy deletes all resources in the stack. Some resources (S3 buckets with data, DynamoDB tables) may have deletion protection or RemovalPolicy.RETAIN.
Safety Rules
- NEVER run
cdk deploy without running cdk diff first and reviewing changes.
- NEVER destroy stacks without listing resources and confirming with the user.
- NEVER use
--require-approval never outside of CI/CD pipelines.
- NEVER expose or log AWS credentials, access keys, or secret keys.
- ALWAYS recommend
RemovalPolicy.RETAIN for stateful resources (databases, S3 buckets).
- WARN that
--hotswap skips CloudFormation and should never be used in production.
Best Practices
- Always run
cdk diff before deploying to review changes.
- Use
RemovalPolicy.RETAIN for databases, buckets, and any stateful resources.
- Use separate stacks for stateful and stateless resources.
- Pin CDK library versions to avoid unexpected breaking changes.
- Use CDK context (
cdk.json) for environment-specific configuration.
Common Patterns
Pattern: Deploy Pipeline (CI/CD Safe)
cdk synth
cdk diff --all 2>&1 | tee diff.txt
cdk deploy --all --require-approval never --ci
Pattern: Multi-Environment Deployment
cdk deploy --all -c environment=dev
cdk deploy --all -c environment=prod --require-approval broadening
Pattern: Migrate from CloudFormation
cdk import <stack-name>
cdk migrate --from-path template.yaml --stack-name <stack-name> --language typescript
Troubleshooting
| Error | Cause | Fix |
|---|
CDKToolkit stack not found | Account/region not bootstrapped | Run cdk bootstrap aws://<account>/<region> |
--app is required | Not in a CDK project directory | cd to the project root with cdk.json |
Cannot find module | Dependencies not installed | Run npm install (TS) or pip install -r requirements.txt (Python) |
CREATE_FAILED on deploy | Resource creation error | Check CloudFormation events in console for details |
UPDATE_ROLLBACK_FAILED | Stack stuck in failed state | Continue rollback in CloudFormation console |
Resource already exists | Importing or naming conflict | Use cdk import or change logical IDs |
| Circular dependency | Stacks reference each other | Break the cycle with CfnOutput / Fn.importValue or restructure |