Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
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}")
awslogs --version
Debug
Known issues
breaking`awslogs` requires Python 3.8 or newer. Running it in older Python environments (e.g., Python 2.x or Python 3.7 and below) will result in installation or runtime errors.fixEnsure your environment uses Python 3.8 or later. Upgrade Python or use a virtual environment with a compatible version.
affects: <0.15.0 (for older Python versions), all versions requiring >=3.8
gotchaAuthentication failures are common if AWS credentials are not correctly configured. `awslogs` relies on the standard AWS credential chain (environment variables, shared credential file `~/.aws/credentials`, IAM roles).fixConfigure AWS CLI using `aws configure` or set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` environment variables.
affects: All versions
gotchaIncorrect IAM permissions for the AWS user or role accessing CloudWatch Logs can lead to 'AccessDeniedException'.fixEnsure the IAM policy associated with your credentials has `logs:GetLogEvents`, `logs:DescribeLogGroups`, and `logs:DescribeLogStreams` permissions for the relevant resources.
affects: All versions
Errors
Common errors & fixes
awslogs: command not found
The `awslogs` executable is not installed or not available in the system's PATH.
fixInstall the package using `pip install awslogs`. If installed, ensure `~/.local/bin` (or equivalent pip install location) is in your system's PATH environment variable.
No credentials found. Unable to authenticate.
The `awslogs` tool could not find valid AWS credentials in any of the expected locations (environment variables, AWS config/credentials files, IAM instance profile).
fixRun `aws configure` to set up your AWS credentials and default region, or manually set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` environment variables.
An error occurred (ResourceNotFoundException) when calling the DescribeLogGroups operation: The specified log group does not exist.
The provided log group name is incorrect, misspelled, or does not exist in the AWS region that `awslogs` is configured to use.
fixVerify the exact log group name and ensure the correct AWS region is specified (e.g., via `--region` flag, `AWS_DEFAULT_REGION` env var, or `aws configure`). Use `awslogs groups` to list available log groups.
Upgrade
Version history
0.15.0latest on PyPI · released Apr 2, 2024
Audit
Dependencies
boto3requiredAWS SDK for Python, used for interacting with AWS services like CloudWatch Logs.
python-dateutilrequiredUtilities for parsing and manipulating dates and times.
pytzrequiredPython timezone library.
clickrequiredCommand-line interface creation kit.
PygmentsrequiredSyntax highlighting for terminal output.