AWS Builder Center
From Alarm to Recover: AI-Assisted Lambda Triage on AWS

From Alarm to Recover: AI-Assisted Lambda Triage on AWS

In this article, we rebuild a controlled AWS incident end-to-end, testing CloudWatch Investigations' hypotheses against direct log evidence and verifying recovery with the same event.

A broken checkout rarely arrives with a neat explanation; the alarm is on the screen, the logs are in another, and the metric is in a third. A malformed checkout event breaks a lambda function. CloudWatch detects it, investigates, and suggests hypotheses, and then we test them against direct log evidence and verify recovery with the same event.
Why build this?
Technical teams lose time when malformed events fail without a repeatable path from detection to root cause. Here, the whole incident is defined as code, so anyone can reproduce it, break it, fix it, and prove it was fixed.

Architecture and Prerequisites

The test event invokes the Lambda function. The function writes structured JSON logs to CloudWatch Logs, and unhandled exceptions increment the Lambda Errors metric. The alarm watches that metric, and CloudWatch investigations start from the alarm. As prerequisites, we need:
  • AWS Account with permissions to deploy CloudFormation stacks
  • Visual Studio Code
  • AWS CLI v2
  • Python
  • GitHub account.
Throughout the build, every feature is used in us-east-1, so the subsequent commands target that region.
  1. Deploy a healthy baseline
Create a workspace folder called ai-devops-incident-triage with 3 subfolders: events, artifacts, and queries, then verify the CLI and the account before deploying anything with the following commands:
`aws --version aws sts get-caller-identity --region us-east-1
`

In the main directory, create a file called template.yaml and paste the code found in the repository:  https://github.com/Dinnyuy/ai-devops-incident-triage.git   The CloudFormation template defines the log group, an execution role, the function with an inline Python code, and the alarm. Three design choices matter:
- A ValidationMode parameter (safe or unsafe) decides what happens when a required field is missing. Unsafe mode raises an exception; safe mode returns a controlled 400. The template default is safe, and the first deployment overrides it with unsafe.
- The function emits structured JSON logs through a small write_log() helper, so events such as validation_failed are searchable later.
- The alarm sums the Lambda Errors metric over 60 seconds and fires on a single error, with TreatMissingData: notBreaching.
Next, create two fixed event files so the healthy path and the incident are repeatable. events/healthy.json contains the required field:  { "action": "checkout", "customer_id": "CUST-1001" }  events/failure.json deliberately omits it: { "action": "checkout" }
Deploy the stack in temporary unsafe mode. CAPABILITY_IAM in the command acknowledges that the template creates an IAM role.
 `aws cloudformation deploy --template-file template.yaml --stack-name ai-devops-incident-triage --parameter-overrides ValidationMode=unsafe --capabilities CAPABILITY_IAM --region us-east-1 
Then invoke the function with the healthy event:
 aws lambda invoke --function-name ai-devops-incident-triage --payload fileb://events/healthy.json --region us-east-1 artifacts/healthy-response.json 
The CLI output should contain no FunctionError, and artifacts/healthy-response.json should show "statusCode": 200 with "customer_id": "CUST-1001" . The baseline is now proven.


  1. Inject the failure and detect it Controlled failure injection uses known bad input to see how a system fails under repeatable conditions. Send the unchanged malformed event three times, saving each response as a separate artifact:
 aws lambda invoke --function-name ai-devops-incident-triage --payload fileb://events/failure.json --region us-east-1 artifacts/failure-1.json aws lambda invoke --function-name ai-devops-incident-triage --payload fileb://events/failure.json --region us-east-1 artifacts/failure-2.json aws lambda invoke --function-name ai-devops-incident-triage --payload fileb://events/failure.json --region us-east-1 artifacts/failure-3.json 
In unsafe mode, the handler raises ValueError before it can return a response, so every CLI response includes FunctionError. That is the first signal, and it comes straight from the invocation. Correlate application and runtime logs
Structured logs show the application's view of the failure, while CloudWatch Logs also captures the runtime exception. Read both together:
aws logs tail /aws/lambda/ai-devops-incident-triage --since 5m --format short --region us-east-
We should see validation_failed entries next to runtime records containing ValueError. The first comes from write_log() when the handler detects the missing field. The second shows that unsafe validation let the problem escape as an unhandled exception, which is what the platform counts as an error.
Confirm the alarm
Open the CloudWatch console in US East (N. Virginia), choose Alarms, and select ai-devops-lambda-errors. The state can take more than a 60-second evaluation period to change, so wait until it reads ALARM. Three controlled failures have now become a visible incident signal.

  1. Investigate with CloudWatch Investigations
Switching manually between metrics and logs slows triage. CloudWatch Investigations scans telemetry related to the alarm and surfaces observations and hypotheses. It uses the permissions of your console session, and the default path creates no additional AWS resources. From the alarm page, with the Region set to US East (N. Virginia) and the alarm still in ALARM, choose Investigate.
Treat AI findings as leads, not facts

The rule we applied: AI-generated findings are investigative leads, and direct CloudWatch Logs evidence remains the source of truth for the root cause. For each observation or hypothesis the investigation produced, we did the following: - Reviewed the metrics surfaced for ai-devops-lambda-errors. - Looked for the validation failed application event in the surfaced logs. - Compared each finding against the runtime ValueError evidence. - Classified it as supported by the logs, useful added context, or lacking enough evidence. This discipline keeps the final root-cause statementt accurate. In this incident the direct evidence is unambiguous: the event had no customer id, and unsafe mode raised an exception instead of returning a client error.
  1. Remediate and verify recovery The handler already contains controlled validation path, and the stack parameter selects it. Redeploy the same stack in safe mode:
`aws cloudformation deploy --template-file template.yaml --stack-name ai-devops-incident-triage --parameter-overrides ValidationMode=safe --capabilities CAPABILITY_IAM --region us-east-1
 Then replay the malformed event. Keeping the event unchanged isolates the deployment parameter as the only remediation variable.   aws lambda invoke --function-name ai-devops-incident-triage --payload fileb://events/failure.json --region us-east-1 artifacts/remediated-response.json`
This time the CLI response has no FunctionError, and artifacts/remediated-response.json contains "statusCode":400 with the message customer\_id is required. Check the logs again with the same aws logs tail command. Safe mode still records validation\_failed, because the event is still malformed, but there should be no new ValueError after the remediated invocation. Older ValueError entries belong to the original incident, so compare timestamps. Finally, return to the CloudWatch console and wait for ai-devops-lamda-errors to return to OK. Alarm recovery can lag the fix by an evaluation period or so. The malformed event now produces a controlled client error without creating another Lambda function error. The one day log retention and the alarm stay active until you delete the stack. When you are done, remove everything with the command:   aws cloudformation delete-stack --stack-name ai-devops-incident-triage --region us-east-1 aws cloudformation wait stack-delete-complete --stack-name ai-devops-incident-triage --region us-east-1  

Push everything to GitHub so that the evidence remain, and the saved template gives you a reproducible rebuild path.

Conclusion
Incidents rarely fail in ways that are convenient to investigate. The signal is spread across alarms, metrics and logs, and the pressure to find a cause quickly can tempt teams to accept the first plausible explanation. This work shows a more disciplined alternative.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article