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:
AWS Account: You need an AWS account with access to CloudWatch Logs.
AWS IAM Permissions: Ensure that the AWS credentials you are using have the following permissions:
logs:GetLogEvents
logs:DescribeLogGroups
logs:DescribeLogStreams
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.
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
Initialize a Boto3 Client: The code first initializes the
boto3
client for CloudWatch Logs using:client = boto3.client('logs')
Specify Log Group and Log Stream: You need to specify the
log_group_name
andlog_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
Fetch Logs: The function
get_cloudwatch_logs()
retrieves logs from the specified log stream by calling theget_log_events
method. This method fetches log events, and you can control the order of retrieval by settingstartFromHead=True
to get logs from the beginning, orstartFromHead=False
to fetch the most recent logs first.Print Logs: The logs are printed using:
for event in response['events']: print(event['message'])
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:
Replace the
log_group_name
andlog_stream_name
variables with your actual CloudWatch log group and stream names.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.