Sub topic 14
Access logs, metrics and CloudWatch Logs
This chapter assumes you can deploy an HTTP API with SAM and have a stage that serves traffic. It covers where an HTTP API sends its logs and metrics, and how to use them to find failed requests.
Three sources of signals
An HTTP API request leaves traces in three places:
- Access logs record one line per request at the API Gateway layer. You choose the fields and the destination log group.
- Lambda logs hold what your function prints, plus the
START,ENDandREPORTlines. They go to/aws/lambda/<function-name>. The execution role needs permission to write them, whichAWSLambdaBasicExecutionRolegrants. - Metrics are counters and timers that API Gateway publishes to CloudWatch in the
AWS/ApiGatewaynamespace.
HTTP APIs have no execution logs. REST APIs do. For an HTTP API, the access log plus your function's own logs give the full picture.
Enabling access logs in SAM
Access logging is a stage setting. Create a log group, then point the stage at it with a JSON format string.
Resources:
ApiAccessLogs:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 14
ItemsApi:
Type: AWS::Serverless::HttpApi
Properties:
AccessLogSettings:
DestinationArn: !GetAtt ApiAccessLogs.Arn
Format: '{"requestId":"$context.requestId","routeKey":"$context.routeKey","status":$context.status,"responseLatency":"$context.responseLatency","integrationLatency":"$context.integration.latency","integrationError":"$context.integrationErrorMessage","sourceIp":"$context.identity.sourceIp"}'
DefaultRouteSettings:
DetailedMetricsEnabled: trueSet RetentionInDays. Log groups keep data forever by default, and storage is billed.
Quote the latency and error fields. When a request never reaches Lambda, for example a 404 on an unknown route, those variables are empty, and unquoted they would produce invalid JSON.
Reading metrics
The main HTTP API metrics are:
| Metric | Meaning |
|---|---|
| `Count` | Requests received |
| `4xx` and `5xx` | Client and server error counts |
| `Latency` | Time from request received to response sent |
| `IntegrationLatency` | Time spent waiting on Lambda |
If Latency is much higher than IntegrationLatency, the delay is in API Gateway, not your code. By default metrics are grouped by API and stage. DetailedMetricsEnabled adds per-route metrics, which are billed as custom metrics. Enable it when you need to see which route is failing.
Troubleshooting with Logs Insights
Because the access log is JSON, CloudWatch Logs Insights detects its fields automatically. Run this against the access log group to rank routes by server errors:
fields @timestamp, routeKey, status
| filter status >= 500
| stats count() as errors by routeKey
| sort errors descTo find the cause of one failure, copy its requestId from the access log. Search the function's log group for it. Lambda's REPORT line shows duration and memory, and a traceback shows the exception. If your handler logs event["requestContext"]["requestId"], the two logs join on that value.
Exercise
A client reports intermittent 500 errors on POST /items. The access log shows status 500 and an empty integrationError. Where do you look next, and how do you find the right entry?
Show answer
Look in the Lambda function's log group, /aws/lambda/<function-name>. Take the requestId from the access log entry and search for it there. This works if the handler logs requestContext.requestId. The traceback or REPORT line shows why the function failed.