Accessing Logs from CloudWatch using Boto3

Accessing Logs from CloudWatch using Boto3

This documentation provides step-by-step instructions on how to use boto3, the AWS SDK for Python, to access logs from Amazon CloudWatch. The code retrieves and prints logs from a specified log group and log stream.

Prerequisites

Before starting, ensure the following:

  1. AWS Account: You need an AWS account with access to CloudWatch Logs.

  2. AWS IAM Permissions: Ensure that the AWS credentials you are using have the following permissions:

    • logs:GetLogEvents

    • logs:DescribeLogGroups

    • logs:DescribeLogStreams

  3. AWS Credentials Setup: Ensure you have set up AWS credentials locally via the AWS CLI or environment variables. You can configure your credentials using the AWS CLI:

     aws configure
    

    This command will prompt you to enter your AWS Access Key, Secret Access Key, and the default region.

  4. Install Boto3: Ensure you have installed boto3. You can install it via pip:

     pip install boto3
    

Code Explanation

The following Python code uses boto3 to fetch logs from a specific CloudWatch log stream.

import boto3

# Initialize a session using Amazon CloudWatch Logs
client = boto3.client('logs')

# Specify the log group name and log stream name
log_group_name = '/aws/lambda/my-log-group'  # Replace with your log group
log_stream_name = 'my-log-stream'  # Replace with your log stream

# Function to get logs from CloudWatch
def get_cloudwatch_logs(log_group_name, log_stream_name):
    try:
        response = client.get_log_events(
            logGroupName=log_group_name,
            logStreamName=log_stream_name,
            startFromHead=True  # Set to False if you want the latest logs first
        )

        # Print out the logs
        for event in response['events']:
            print(event['message'])

    except Exception as e:
        print(f"Error retrieving logs: {e}")

# Call the function
get_cloudwatch_logs(log_group_name, log_stream_name)

Steps to Use the Code

  1. Initialize a Boto3 Client: The code first initializes the boto3 client for CloudWatch Logs using:

     client = boto3.client('logs')
    
  2. Specify Log Group and Log Stream: You need to specify the log_group_name and log_stream_name. These are the CloudWatch log group and log stream from which you want to retrieve the logs. Replace the placeholders with your actual CloudWatch log group and stream names:

     log_group_name = '/aws/lambda/my-log-group'  # Your log group
     log_stream_name = 'my-log-stream'  # Your log stream
    
  3. Fetch Logs: The function get_cloudwatch_logs() retrieves logs from the specified log stream by calling the get_log_events method. This method fetches log events, and you can control the order of retrieval by setting startFromHead=True to get logs from the beginning, or startFromHead=False to fetch the most recent logs first.

  4. Print Logs: The logs are printed using:

     for event in response['events']:
         print(event['message'])
    
  5. Handle Errors: The function includes basic error handling using a try-except block. If there is an issue retrieving the logs, the error message will be printed.

Running the Script

To execute this script:

  1. Replace the log_group_name and log_stream_name variables with your actual CloudWatch log group and stream names.

  2. Run the script in your Python environment.

Example:

python cloudwatch_logs.py

If successful, the logs from the specified CloudWatch log stream will be printed in the terminal.