0
0
AwsHow-ToBeginner · 4 min read

How to Use CloudWatch Log Group in AWS: Simple Guide

To use a CloudWatch Log Group, first create the log group in AWS, then configure your applications or AWS services to send logs to it. You can view, search, and monitor these logs in the AWS Management Console or via AWS CLI.
📐

Syntax

A CloudWatch Log Group is a container for log streams from your applications or AWS services. You create it by specifying a name and optional retention period. Logs are sent to log streams within this group.

Key parts:

  • logGroupName: The name of the log group.
  • retentionInDays: How long logs are kept before automatic deletion.
  • logStreamName: A stream inside the group where logs are stored.
bash
aws logs create-log-group --log-group-name MyAppLogGroup

aws logs put-retention-policy --log-group-name MyAppLogGroup --retention-in-days 14

aws logs create-log-stream --log-group-name MyAppLogGroup --log-stream-name MyAppStream
💻

Example

This example shows how to create a CloudWatch Log Group, set a retention policy, create a log stream, and put a log event using AWS CLI.

bash
aws logs create-log-group --log-group-name ExampleLogGroup

aws logs put-retention-policy --log-group-name ExampleLogGroup --retention-in-days 7

aws logs create-log-stream --log-group-name ExampleLogGroup --log-stream-name ExampleStream

aws logs put-log-events --log-group-name ExampleLogGroup --log-stream-name ExampleStream --log-events timestamp=$(date +%s%3N),message="Hello CloudWatch Logs" --sequence-token ""
Output
Created log group ExampleLogGroup Set retention policy to 7 days Created log stream ExampleStream Uploaded log event to ExampleStream
⚠️

Common Pitfalls

Common mistakes when using CloudWatch Log Groups include:

  • Not setting a retention policy, which can cause logs to accumulate and increase costs.
  • Using incorrect sequenceToken when putting log events, causing errors.
  • Not creating a log stream before sending logs, which will fail.
  • Confusing log groups with log streams; logs must go to streams inside groups.
bash
aws logs put-log-events --log-group-name ExampleLogGroup --log-stream-name ExampleStream --log-events timestamp=$(date +%s%3N),message="Test message"
# Error: InvalidSequenceTokenException

# Correct usage requires fetching the latest sequence token:
TOKEN=$(aws logs describe-log-streams --log-group-name ExampleLogGroup --log-stream-name-prefix ExampleStream --query 'logStreams[0].uploadSequenceToken' --output text)
aws logs put-log-events --log-group-name ExampleLogGroup --log-stream-name ExampleStream --log-events timestamp=$(date +%s%3N),message="Test message" --sequence-token $TOKEN
📊

Quick Reference

Tips for using CloudWatch Log Groups effectively:

  • Always set a retention policy to control storage costs.
  • Create log streams before sending logs.
  • Use the latest sequence token when uploading log events.
  • Use AWS Console or CLI to search and filter logs easily.

Key Takeaways

Create a CloudWatch Log Group to organize your logs.
Set a retention policy to automatically delete old logs and save costs.
Create log streams inside the group before sending logs.
Use the correct sequence token when uploading log events to avoid errors.
View and search logs easily via AWS Console or CLI.