Lesson 3 of 3 Troubleshooting and Debugging Playbook

Analysing Errors with CloudWatch Investigations, Query Generation and Kiro

This lesson explains how AWS AI tools help find the cause of an error, from the first alarm to the fix in code, and why every AI suggestion must be verified.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how CloudWatch investigations produce observations and hypotheses.
  • Explain how the Logs Insights query generator is used during an incident.
  • Describe how a Kiro bugfix spec structures a fix and protects against regressions.
  • Choose the right AI tool for each stage of a root cause analysis.

CloudWatch investigations

CloudWatch investigations is a generative AI feature of Amazon CloudWatch for operational troubleshooting. An investigation starts from an alarm, a metric graph or a Logs Insights query, or automatically when an alarm fires. It examines the affected resources and their dependencies and surfaces observations (relevant metrics, log patterns, deployment events, AWS Health events and CloudTrail change events) and hypotheses about the root cause, with a diagram when several resources are involved.

Without any setup, an investigation runs once with the signed-in user's permissions and is visible only to that user. Configuring CloudWatch investigations creates an investigation group for the account, after which investigations can be shared with the team, refined by accepting or discarding suggestions, enriched with X-Ray, Application Signals and EKS data, and summarised in an incident report. Investigations can suggest a Systems Manager Automation runbook to fix the problem. Every action is recorded in CloudTrail, and no resources are changed unless someone runs a runbook.

Generating log queries

Writing a Logs Insights query from scratch is slow during an incident. The query generator in the Logs Insights editor turns a description such as "count errors by error type in the last 30 minutes and show the five most common" into a query, with a line-by-line explanation, and the developer can refine it. It requires the cloudwatch:GenerateQuery permission. The generated query, like any AI output, is read before its results are trusted, because a wrong filter can hide the events that matter.

Analysing errors in code with Kiro

Once the failing component is known, the cause is usually in its code or configuration. Pasting a stack trace, an error message or a log excerpt into a Kiro session lets the agent locate the failing line, explain the error and propose a fix. With an MCP server for CloudWatch, the agent can run the log queries and read the metrics itself.

For a real defect, a bugfix spec gives the work a structure that prevents regressions. The following steps describe it.

  1. Describe the bug

    bugfix.md records the current behaviour, the expected behaviour and the behaviour that must not change.

  2. Find the root cause

    The agent analyses the code and the evidence and explains why the current behaviour occurs.

  3. Design the fix

    The fix is designed and reviewed before it is written, including its effect on the unchanged behaviour.

  4. Prove it

    Tests reproduce the bug, pass after the fix and protect the unchanged behaviour from then on.

Verifying AI suggestions

AI tools propose; developers verify. A hypothesis from an investigation is a lead, not a conclusion: the model may correlate a change that happened at the same time without causing the problem. The developer confirms each hypothesis against the evidence, reproduces the failure where possible, makes the fix through the normal pipeline rather than directly in production, and checks afterwards that the error rate and latency have returned to normal.

The following comparison summarises which tool to use at each stage of an analysis.

CloudWatch investigations
Best atFinding which resource and change are involved
Works withMetrics, logs, traces, deployments, CloudTrail, Health
Choose when An alarm fires and the cause is unknown.
Logs Insights query generator
Best atWriting precise queries over log data
Works withOne or more log groups
Choose when The question is clear, but the query is not.
Kiro
Best atFinding and fixing the defect in code
Works withSource code, stack traces, logs and specs
Choose when The failing code path is known.
Sort into groups

Which AI tool helps?

Exam skills 4.1.5

Choose the AI tool for each stage of an analysis.

Log in to try this activity Interactive activities are free with an account.
Worked example

Errors after a release

Soon after a release, an alarm fires on the 5xx rate of an orders API.

The on-call developer starts an investigation from the alarm. It shows that errors come from one Lambda function, that a deployment of that function finished four minutes before the alarm, and suggests the hypothesis that the new version fails when calling DynamoDB. The developer uses the query generator to list the most common error messages since the deployment: AccessDeniedException on dynamodb:Query against a new index.

In Kiro, the developer opens a bugfix spec with the error and the template. The agent finds that the template grants dynamodb:Query on the table ARN but not on table/Orders/index/*. The fix adds the index ARN and a test that checks the policy, and it is deployed through the pipeline. The alarm returns to OK, which confirms the hypothesis.

Check yourself

An investigation suggests that a deployment caused an error spike. What should the developer do before rolling back?

Show answer

Confirm the hypothesis against the evidence, for example by checking that the errors started after the deployment and come from the deployed component, because the deployment may only coincide with the cause.

Exam Tips

Remember for the exam

  • Generative AI hypotheses from an alarm, using metrics, logs, deployments and CloudTrail: CloudWatch investigations.
  • Investigations can suggest Systems Manager Automation runbooks but change nothing unless one is run.
  • Write a Logs Insights query from a plain-language description: the query generator (cloudwatch:GenerateQuery).
  • Fix a defect without breaking other behaviour: a Kiro bugfix spec with regression tests.
  • Treat AI hypotheses as leads; verify them against telemetry before acting.