feat(aws-cdk): detailed ECS service and container-level logs on cdk deployment failures#1505
feat(aws-cdk): detailed ECS service and container-level logs on cdk deployment failures#1505iankhou wants to merge 13 commits into
Conversation
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
…gestions to turn on cw logging format changes
5cb6bde to
2a8c6c1
Compare
There was a problem hiding this comment.
Pull request overview
This PR enhances CDK deployment diagnostics by enriching failed CloudFormation resource errors with best-effort, service-specific investigation results—initially focused on ECS services—so users can see stopped-task reasons and recent CloudWatch Logs directly in failure output.
Changes:
- Add ECS resource investigation that gathers stopped task details, container image info, and recent CloudWatch Logs (with line caps).
- Extend stack diagnosis to attach and format additional per-resource diagnostic context.
- Wire an optional “exploration SDK” (preferably lookup-role) into deploy/diagnose flows and expand the SDK ECS surface area to support investigation calls.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/@aws-cdk/toolkit-lib/test/api/diagnosing/resource-investigation.test.ts | Adds unit tests for ECS identifier parsing and ECS investigation behaviors (stopped tasks, logs, truncation). |
| packages/@aws-cdk/toolkit-lib/test/api/diagnosing/snapshots/diagnosis-formatting.test.ts.snap | Updates snapshot header text. |
| packages/@aws-cdk/toolkit-lib/test/api/aws-auth/snapshots/sdk-logger.test.ts.snap | Updates snapshot header text. |
| packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts | Passes an exploration SDK provider into the diagnoser for toolkit-driven diagnosis. |
| packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/stack-diagnoser.ts | Enhances errors with additional investigation context and adds fallback diagnosis behavior. |
| packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/resource-investigation.ts | Implements ECS service investigation (stopped tasks + CloudWatch Logs deep links + log truncation). |
| packages/@aws-cdk/toolkit-lib/lib/api/diagnosing/diagnosis-formatting.ts | Renders additionalContext blocks beneath resource errors in the formatted diagnosis output. |
| packages/@aws-cdk/toolkit-lib/lib/api/deployments/deployments.ts | Provides a lookup-role-based exploration SDK provider for deployments. |
| packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/sdk.ts | Adds ECS methods (DescribeServices/DescribeTasks/DescribeTaskDefinition) to the SDK wrapper. |
| packages/@aws-cdk/toolkit-lib/lib/actions/diagnose/index.ts | Extends TracedResourceError with optional additionalContext and defines its shape. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const taskDefInfo = await getTaskDefinitionInfo(ecs, taskDefinitionArn, debug); | ||
|
|
||
| if (taskDefInfo && taskDefInfo.images.length > 0) { | ||
| results.push({ source: 'Container Images', messages: taskDefInfo.images }); | ||
| } | ||
|
|
||
| const logConfigs = taskDefInfo?.logConfigs ?? []; | ||
|
|
||
| if (logConfigs.length === 0) { |
| // eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism | ||
| const logResults = await Promise.all(logConfigs.map(cfg => fetchRecentLogs(cwl, cfg, region, stoppedTaskResult.taskIds, debug))); | ||
| let hasLogs = false; | ||
| for (const context of logResults) { | ||
| if (context) { | ||
| results.push(context); | ||
| hasLogs = true; | ||
| } | ||
| } | ||
|
|
||
| if (!hasLogs) { | ||
| results.push({ | ||
| source: 'CloudWatch Logs', | ||
| messages: ['No application logs found (container may not have started). Check the stopped task reasons above for details.'], | ||
| }); | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
rix0rrr
left a comment
There was a problem hiding this comment.
Good stuff! See my other comment on formatting, nearly good to go this!
| /** | ||
| * Maximum number of log lines included per CloudWatch Logs context block. | ||
| * | ||
| * The formatter renders the messages array verbatim, so this is the | ||
| * single user-visible cap. | ||
| */ | ||
| const MAX_LOG_LINES = 50; |
There was a problem hiding this comment.
From the top or from the bottom?
If there's more than 50, we should probably get the LAST 50 lines, not the FIRST 50 lines. Are we doing that?
| const taskDefInfo = await getTaskDefinitionInfo(ecs, taskDefinitionArn, debug); | ||
|
|
||
| if (taskDefInfo && taskDefInfo.images.length > 0) { | ||
| results.push({ source: 'Container Images', messages: taskDefInfo.images }); | ||
| } | ||
|
|
||
| const logConfigs = taskDefInfo?.logConfigs ?? []; | ||
|
|
||
| if (logConfigs.length === 0) { |
| 'No CloudWatch Logs configuration found. Enable logging to see container output on failure.', | ||
| 'Example (CDK):', | ||
| ' taskDefinition.addContainer("app", {', | ||
| ' image: ecs.ContainerImage.fromRegistry("my-image"),', |
There was a problem hiding this comment.
| ' image: ecs.ContainerImage.fromRegistry("my-image"),', | |
| ' // ...', |
| source: 'CloudWatch Logs', | ||
| messages: [ | ||
| 'No CloudWatch Logs configuration found. Enable logging to see container output on failure.', | ||
| 'Example (CDK):', |
There was a problem hiding this comment.
| 'Example (CDK):', | |
| 'Example:', |
| // eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism | ||
| const logResults = await Promise.all(logConfigs.map(cfg => fetchRecentLogs(cwl, cfg, region, stoppedTaskResult.taskIds, debug))); | ||
| let hasLogs = false; | ||
| for (const context of logResults) { | ||
| if (context) { | ||
| results.push(context); | ||
| hasLogs = true; | ||
| } | ||
| } | ||
|
|
||
| if (!hasLogs) { | ||
| results.push({ | ||
| source: 'CloudWatch Logs', | ||
| messages: ['No application logs found (container may not have started). Check the stopped task reasons above for details.'], | ||
| }); | ||
| } |

Pending tests and potential cleanups with custom resource implementation. Pending dual-role implementation (allow using lookup role or deploy role).
Logs from running with https://github.com/iankhou/cdk-app-with-problems. There are several branches demonstrating different failure modes:
See the following file for log examples:
lcdk deploy results.md
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license