How to View CloudWatch Logs in AWS Console and CLI
To view
CloudWatch logs, open the AWS Management Console, navigate to CloudWatch, then select Logs and choose the log group and stream you want to see. Alternatively, use the AWS CLI command aws logs get-log-events with the log group and stream names to fetch logs from the terminal.Syntax
Use the AWS CLI command to fetch logs from CloudWatch logs:
aws logs get-log-events: Command to retrieve log events.--log-group-name: Specify the log group name.--log-stream-name: Specify the log stream name.--limit: (Optional) Number of log events to return.
bash
aws logs get-log-events --log-group-name <log-group-name> --log-stream-name <log-stream-name> --limit 10
Example
This example shows how to view the last 10 log events from a specific log group and stream using AWS CLI.
bash
aws logs get-log-events --log-group-name /aws/lambda/my-function --log-stream-name 2024/06/01/[$LATEST]abcdef1234567890 --limit 10
Output
{
"events": [
{
"timestamp": 1710000000000,
"message": "START RequestId: abcdef12-3456-7890-abcd-ef1234567890 Version: $LATEST",
"ingestionTime": 1710000001000
},
{
"timestamp": 1710000002000,
"message": "Processing event data...",
"ingestionTime": 1710000003000
}
],
"nextForwardToken": "f/1234567890",
"nextBackwardToken": "b/1234567890"
}
Common Pitfalls
Common mistakes when viewing CloudWatch logs include:
- Using incorrect
log-group-nameorlog-stream-name, which returns no logs. - Not having proper IAM permissions to access CloudWatch logs.
- Trying to view logs before they are ingested, causing empty results.
- Confusing log groups with log streams; log groups contain multiple streams.
bash
Wrong command example: aws logs get-log-events --log-group-name wrong-group --log-stream-name wrong-stream Correct command example: aws logs get-log-events --log-group-name /aws/lambda/my-function --log-stream-name 2024/06/01/[$LATEST]abcdef1234567890
Quick Reference
| Action | Description |
|---|---|
| Open AWS Console | Go to CloudWatch > Logs to browse log groups and streams. |
| List Log Groups | aws logs describe-log-groups |
| List Log Streams | aws logs describe-log-streams --log-group-name |
| Get Log Events | aws logs get-log-events --log-group-name |
Key Takeaways
Use AWS Console CloudWatch Logs to easily browse logs visually.
AWS CLI commands require correct log group and stream names to fetch logs.
Ensure you have IAM permissions to read CloudWatch logs.
Log groups contain multiple log streams; pick the right stream to view logs.
Use the --limit option to control how many log events you retrieve.