AWSLogs CLI Tool

0.15.0 · active · verified Thu Apr 16

awslogs is a simple command-line interface (CLI) tool designed to read and query Amazon CloudWatch Logs. It provides an intuitive way to interact with log groups, streams, and events directly from the terminal. The current version is 0.15.0, and releases are infrequent, indicating a stable tool focused on its core CLI functionality.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to programmatically execute the `awslogs` command-line tool using Python's `subprocess` module. It attempts to retrieve recent log events from a specified CloudWatch Log Group. Users must have AWS credentials configured (e.g., via `aws configure` or environment variables) and specify an existing log group name and optionally an AWS profile and region.

import subprocess
import os

# Ensure AWS credentials are set up (e.g., via AWS CLI 'aws configure' or environment variables)
# For demonstration, we'll try to get logs from a common AWS Lambda log group.
# Replace 'my-lambda-function' with an actual Lambda function name in your AWS account.
# Set the AWS_DEFAULT_REGION environment variable or configure it via `aws configure`.
# Example: os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'

try:
    # List log groups (example for initial exploration)
    # result = subprocess.run(['awslogs', 'groups', '--profile', os.environ.get('AWS_PROFILE', 'default')], capture_output=True, text=True, check=True)
    # print('Available Log Groups:\n', result.stdout)

    # Get logs from a specific group (replace with a real log group name)
    log_group_name = '/aws/lambda/my-example-function'
    print(f"Attempting to retrieve logs from: {log_group_name}")
    
    # Run the awslogs command to get logs from a hypothetical Lambda function for the last hour
    # Using --start='1h ago' to limit output for quickstart
    cmd = ['awslogs', 'get', log_group_name, '--start', '1h ago', '--profile', os.environ.get('AWS_PROFILE', 'default')]
    
    # It's good practice to specify --region if not set via environment variables or AWS CLI config
    # For example: cmd = ['awslogs', 'get', log_group_name, '--start', '1h ago', '--region', os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')]

    # Execute the command
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    
    # Read output line by line
    for line in process.stdout:
        print(line.strip())
    
    # Check for errors
    stderr_output = process.stderr.read()
    if process.wait() != 0:
        print(f"Error executing awslogs: {stderr_output}")
    elif stderr_output:
        print(f"awslogs warnings/info: {stderr_output}")

except FileNotFoundError:
    print("Error: 'awslogs' command not found. Please ensure it is installed and in your system's PATH.")
except subprocess.CalledProcessError as e:
    print(f"Error during awslogs execution: {e.stderr}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →