Skip to content
Visual explainer 3h 30m to learn 5 steps

Investigate an AWS incident with metrics, audit and config history

Use CloudWatch telemetry, CloudTrail events and Config history together to explain one synthetic incident.

By Invicly Tech, , in AWS Cloud Engineering. About 12 minutes to read.

Topic 27 of 38

In short

CloudWatch, CloudTrail and AWS Config answer three different questions during an incident. CloudWatch shows how the service behaved (metrics, logs and traces), CloudTrail shows who called which AWS API and when, and Config shows how a resource's settings changed over time. Used together on one timeline, they turn a vague outage into a clear story with a cause.

Think of it like this: CloudWatch is the patient's heart monitor, CloudTrail is the visitor log at the ward door, and Config is the before-and-after photos of the room.

Explained simply

When something breaks in AWS, you need answers to three questions. What is the service doing right now? Who changed something? What exactly changed? AWS gives you one tool for each. CloudWatch collects numbers and log lines from your running service. CloudTrail records every call made to the AWS APIs. AWS Config keeps a history of how each resource was set up.

In this lab we use one made-up incident in the file-processing service. Files land in S3, a message goes to an SQS queue, and a Lambda worker reads the file and writes a result to DynamoDB. At 14:05 UTC the worker starts failing, the queue fills up, and users stop seeing results. Nothing in the code changed.

CloudWatch shows the symptom: worker errors jump and the oldest message in the queue gets older every minute. The worker's logs show AccessDenied when it reads from S3. CloudTrail shows that at 14:04 a deploy role called PutRolePolicy on the worker's role. Config shows the role's policy before and after, and the s3:GetObject permission is gone.

Put these facts on one timeline and the story is clear: a deploy removed a permission, the worker could not read files, and the queue backed up. You restore the policy, replay the stuck messages, and add an alarm and a test so the same mistake is caught in minutes next time.

Press Play on a diagram to walk through it one step at a time.

CompareThree tools, three questions
CloudWatch
  • How is the service behaving?
  • Metrics, logs, alarms and traces
  • Seconds to minutes of delay
  • Retention you choose per log group
CloudTrail
  • Who called which API, and from where?
  • Management events recorded by default
  • Event history keeps 90 days
  • A trail stores events longer in S3
AWS Config
  • What did this resource look like before?
  • Configuration items per change
  • Resource timeline and relationships
  • Must be turned on and costs per item
FlowInvestigating the synthetic incident
  1. 1
    Alarm fires

    Queue age rises above 5 minutes

  2. 2
    Metrics and logs

    Worker errors start at 14:05 with AccessDenied

  3. 3
    CloudTrail

    PutRolePolicy by the deploy role at 14:04

  4. 4
    Config

    Role policy lost s3:GetObject

  5. 5
    Fix and prevent

    Restore policy, add a test and an alarm

SequenceHow the bad change spread
Deploy role
IAM
Lambda worker
SQS queue
  1. 114:04 PutRolePolicy without s3:GetObject
    Deploy role to IAM: 14:04 PutRolePolicy without s3:GetObject
  2. 214:05 Receive message for new file
    Lambda worker to SQS queue: 14:05 Receive message for new file
  3. 3GetObject fails: AccessDenied
    Lambda worker to Lambda worker: GetObject fails: AccessDenied
  4. 4Message returns after visibility timeout
    SQS queue to Lambda worker: Message returns after visibility timeout
  5. 5Oldest message age keeps rising
    SQS queue to SQS queue: Oldest message age keeps rising

In depth

CloudWatch: metrics, logs and traces

Metrics are numbers over time. AWS services publish many for free: Lambda sends Errors, Throttles and Duration, and SQS sends ApproximateAgeOfOldestMessage and ApproximateNumberOfMessagesVisible. Your own code can publish custom metrics too. Logs are the text your code prints; for Lambda, each function writes to a log group named /aws/lambda/ followed by the function name. Logs Insights lets you query those lines, for example to count errors per minute.

Traces follow one request across services. AWS X-Ray collects them and the CloudWatch console shows the trace map; new instrumentation is usually done with OpenTelemetry. Traces tell you where time went, which logs alone often cannot.

An alarm watches one metric (or a metric math expression) and moves between OK, ALARM and INSUFFICIENT_DATA. Actions such as an SNS notification run when the state changes.

  • New log groups never expire unless you set retention
  • Logs Insights bills by data scanned, so narrow the time range
  • Alarm on the user's pain: queue age, error rate, latency
  • Composite alarms combine several alarms to cut noise

CloudTrail: the API audit record

Every console click, CLI command and SDK call becomes an API request, and CloudTrail records it as an event. Each event has the time, the event name such as PutRolePolicy, the caller in userIdentity, the source IP address, the request parameters and any error code. Calls made by AWS services on your behalf appear too, with the service as the caller.

Event history is on by default and keeps 90 days of management events per Region, which you can search for free in the console or with lookup-events. To keep events longer, or to record data events such as S3 GetObject or Lambda Invoke, you create a trail that writes to an S3 bucket, or use CloudTrail Lake. Data events are charged per event, so enable them only for the buckets or functions you need.

Events usually arrive within about five minutes, so a very recent change may not be visible yet.

  • Management events: create, change and delete resources
  • Data events: object reads and writes, function invokes; extra cost
  • IAM and other global services log to US East (N. Virginia)
  • Look for errorCode AccessDenied to find failed attempts

AWS Config: what the resource looked like

Config keeps a configuration item each time a recorded resource changes. A configuration item holds the full settings of that resource at that moment, its relationships to other resources and a link to the CloudTrail event that caused the change. In the console, the resource timeline shows these items in order, so you can compare the role policy at 14:00 with the one at 14:05.

Config only knows what it recorded. You must turn on the configuration recorder in each Region and choose which resource types to record. Recording can be continuous or daily, and you pay per configuration item, so very busy resources can cost more than expected.

Config rules check resources against a rule, for example that every S3 bucket blocks public access. Rule results are a useful early warning: a rule turning noncompliant is often the first sign of a risky change.

LayersWhich layer each tool sees
  1. 1
    User experience

    Results stop appearing; CloudWatch alarm on queue age

  2. 2
    Application behaviour

    Errors, logs and traces in CloudWatch

  3. 3
    Resource settings

    Role policy versions in AWS Config

  4. 4
    Control-plane actions

    PutRolePolicy event in CloudTrail

Building the timeline and closing the loop

Write a timeline as a table with UTC time, source, fact and link to the evidence. Start with the first user-visible symptom, then work backwards to the change, then forwards to recovery. Keep facts separate from guesses: mark a line as a hypothesis until a log, event or configuration item proves it.

A useful timeline answers: when did impact start and end, what changed just before, who or what made the change, how was it detected, and how was it fixed. The gap between the change and detection is the detection time; shorten it with better alarms.

Finish with actions that make the next incident smaller: an alarm on the right metric, a pipeline check that runs a smoke test after IAM changes, a Config rule, or retention settings so the evidence is still there when you need it.

  • Use UTC everywhere
  • Link each fact to a log query, event ID or configuration item
  • Measure time to detect and time to recover
  • Every action item has an owner and a date

Learn it in 5 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Read metrics, logs and traces in CloudWatch~40m
  2. 2
    Find the API call in CloudTrail~40m
  3. 3
    See what changed in Config~40m
  4. 4
    Build a timeline of the incident~40m
  5. 5
    Add an alarm for next time~40m

Key points

  • Start from the symptom in CloudWatch, then look for the change that caused it.
  • CloudTrail answers who and when; Config answers what changed and what it looked like before.
  • Event history is free for 90 days of management events; data events need a trail and cost extra.
  • Write every finding on one timeline with UTC times, so tools with different delays line up.
  • Finish each incident with an alarm or test that would have caught it earlier.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
# Who changed IAM role policies in the last hour?
aws cloudtrail lookup-events \
  --lookup-attributes AttributeKey=EventName,AttributeValue=PutRolePolicy \
  --start-time "$(date -u -d '-1 hour' +%FT%TZ)" \
  --query 'Events[].[EventTime,Username,EventName]' --output table

# What did the role look like before and after?
aws configservice get-resource-config-history \
  --resource-type AWS::IAM::Role --resource-id <role-id> --limit 2

lookup-events reads the free 90-day event history. Config needs its recorder turned on before the incident.

Finding the first error with Logs Insights

Step 1: open CloudWatch Logs Insights and pick the log group /aws/lambda/file-worker. Step 2: set the time range to 13:30 to 15:00 UTC. Step 3: run the query below, which counts lines containing AccessDenied in one-minute bins. Step 4: read the first non-zero bin: 14:05. Step 5: open one matching line to get the request ID and the S3 key, and add both to the timeline as evidence.

sql
fields @timestamp, @requestId, @message
| filter @message like /AccessDenied/
| stats count(*) as errors by bin(1m)
| sort @timestamp asc

Adding the alarm that would have caught it

The queue backlog started at 14:05 but a user noticed at 14:40. Step 1: pick the metric that shows user pain, ApproximateAgeOfOldestMessage for the jobs queue. Step 2: choose a threshold, 300 seconds, since a normal file is processed in under 30. Step 3: require 5 one-minute periods to avoid noise. Step 4: send the alarm to an SNS topic the on-call person subscribes to. Expected evidence: after the next bad change, the alarm moves to ALARM within about 10 minutes. Cleanup: delete the alarm and topic when the lab ends.

bash
aws cloudwatch put-metric-alarm --alarm-name jobs-queue-age \
  --namespace AWS/SQS --metric-name ApproximateAgeOfOldestMessage \
  --dimensions Name=QueueName,Value=jobs \
  --statistic Maximum --period 60 --evaluation-periods 5 \
  --threshold 300 --comparison-operator GreaterThanThreshold \
  --alarm-actions arn:aws:sns:eu-west-1:111122223333:oncall

Quick reference

  • CloudWatch = behaviour, CloudTrail = who called which API, Config = resource settings over time
  • CloudTrail event history: 90 days of management events per Region, free
  • Data events (S3 objects, Lambda invokes) need a trail or CloudTrail Lake and cost extra
  • Config shows nothing for periods when its recorder was off
  • aws logs put-retention-policy --log-group-name /aws/lambda/file-worker --retention-in-days 14
  • Alarm states: OK, ALARM, INSUFFICIENT_DATA
  • SQS backlog signal: ApproximateAgeOfOldestMessage

Common mistakes

  • Turning on Config after the incident: it cannot show history it never recorded.
  • Searching CloudTrail in the wrong Region; most events are recorded in the Region where the call happened.
  • Keeping logs forever by default; new log groups never expire unless you set retention.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A Lambda worker suddenly gets AccessDenied reading S3. Which tool shows who changed its role?

Question 2

2You need to see the full role policy as it was before the change. Where do you look?

Question 3

3Which metric best shows that users are waiting for their files to be processed?

Question 4

4How long does free CloudTrail event history keep management events?

Question 5

5Your new Lambda log group has grown for two years. What did the team forget?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    In a sandbox account, list the last 10 management events in your Region and identify the caller of each.

  2. Exercise 2medium

    Reproduce the synthetic incident: remove s3:GetObject from a lab worker role, upload a file, then find the error in logs, the change in CloudTrail and the before and after policy in Config. Restore the policy afterwards.

  3. Exercise 3hard

    Write a one-page incident report for the synthetic incident with a UTC timeline, time to detect, time to recover, root cause and three action items.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is CloudTrail turned on by default?
Yes. Event history records management events for 90 days in every account. A trail is only needed to keep events longer, send them to S3 or record data events.
Do I need both CloudTrail and Config?
They complement each other. CloudTrail shows the call that made a change; Config shows the resulting settings and history, and can check them against rules.
Why does my Logs Insights query cost money?
Logs Insights charges by the amount of log data scanned. Pick only the log groups you need and the shortest useful time range.

Go deeper

ShareXLinkedInWhatsApp