AI Can't Fix What It Can't See: How cdk diagnose Enables Autonomous CDK Remediation
Patch: Add `cdk --unstable=diagnose diagnose MyStack` to your CI pipeline to automatically retrieve construct path, error, and source location after a deployment failure.
Patch: Enable cdk diagnose in your CI, run it after failures, and use the construct path and source location to patch the CDK code.
Summary
When a CDK deployment fails in a CI pipeline, developers currently have to manually navigate the pipeline UI, open the CloudFormation console, locate the failed change set, and translate the raw error back into CDK construct code. This manual loop is time‑consuming and prevents AI agents from fixing the issue automatically, because the AI has no access to the construct path or source location. The new cdk diagnose subcommand, available via `cdk --unstable=diagnose diagnose MyStack`, queries CloudFormation’s DescribeChangeSet API and enriches the raw error with CDK metadata such as aws:cdk:path, mapping the CloudFormation logical ID back to the construct tree and source file. The output includes the failed resource, a human‑readable error, the construct path (e.g., MyStack/MyFunction/LogGroup), and the exact file and line number (lib/my-stack.ts:8:5). Because cdk diagnose works regardless of how the stack was deployed—whether through CDK Pipelines, CodePipeline, or a manual CloudFormation API call—it enables a single command that both humans and AI agents can use to diagnose, fix, and redeploy failures autonomously. The article demonstrates a real P0 issue where a Lambda function’s LogGroup already existed after a CDK upgrade; cdk diagnose pinpointed the exact construct and source location, allowing the developer to patch the code quickly. By exposing actionable context, cdk diagnose removes the need for developers to manually map errors to code, accelerating remediation and opening the door to AI‑driven CDK maintenance. The tool is currently in beta and requires the `--unstable=diagnose` flag to enable the subcommand.
Key changes
- cdk diagnose subcommand queries CloudFormation DescribeChangeSet and enriches errors with aws:cdk:path metadata
- Output includes failed resource, human‑readable error, construct path, and source file/line
- Works regardless of deployment method (CDK Pipelines, CodePipeline, manual CFN)
- Enables single command for humans and AI agents to diagnose, fix, and redeploy
- Demonstrated on a P0 issue where a Lambda LogGroup already existed after CDK upgrade
- Requires `--unstable=diagnose` flag to enable the subcommand