/pyawslog

Primary LanguagePython

AWS CloudWatch Logs Analyzer

A Python script to analyze AWS CloudWatch log groups, showing their size, retention period, and other metrics in a formatted table.

Features

  • List all CloudWatch log groups in a specified AWS region
  • Display log group size in human-readable format (B, KB, MB, GB, TB)
  • Show retention period for each log group
  • Filter log groups using regex patterns
  • Configurable limit (default: 50 log groups)
  • Formatted table output with summary statistics

Requirements

  • Python 3.6+
  • AWS CLI configured or AWS credentials available
  • Required Python packages (see requirements.txt)

Installation

  1. Clone or download this repository
  2. Install required dependencies:
    pip install -r requirements.txt

AWS Configuration

Ensure your AWS credentials are configured. You can use any of these methods:

  1. AWS Profile (recommended):

    export AWS_PROFILE=your-profile-name
  2. Environment Variables:

    export AWS_ACCESS_KEY_ID=your-access-key
    export AWS_SECRET_ACCESS_KEY=your-secret-key
  3. AWS CLI Default Profile:

    aws configure

Usage

Basic Usage

python aws-log-summary.py --region us-east-1

Filter by Log Group Name

python aws-log-summary.py --region us-east-1 --filter "abcd*"

Custom Limit

python aws-log-summary.py --region us-east-1 --limit 100

Complete Example

export AWS_PROFILE=foobar
python aws-log-summary.py --region us-east-1 --filter "production-*" --limit 25

Command Line Options

  • --region (required): AWS region to analyze
  • --filter (optional): Regex pattern to filter log group names
  • --limit (optional): Maximum number of log groups to analyze (default: 50)

Sample Output

Analyzing CloudWatch logs in region: us-east-1
Limit: 50 log groups

+------------------+--------+------------+
| Log Group        | Size   | Retention  |
+==================+========+============+
| aaa-bbb          | 2.1MB  | 7 days     |
| production-api   | 15.3GB | 30 days    |
| staging-web      | 456.2KB| Never expire|
+------------------+--------+------------+

Summary:
Total log groups: 3
Total size: 15.3GB

IAM Permissions

Your AWS user/role needs the following permissions:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:DescribeLogGroups"
            ],
            "Resource": "*"
        }
    ]
}

Error Handling

The script includes error handling for:

  • AWS authentication issues
  • Network connectivity problems
  • Invalid regions
  • Permission denied errors

Dependencies

  • boto3: AWS SDK for Python
  • tabulate: Pretty-print tabular data

License

This project is provided as-is for educational and operational purposes.