How to Delete AWS Lambda Logs from CloudWatch using Python Scripts

Developers often have to go through application logs to find and fix problems. But as time goes on, AWS CloudWatch log groups can gather a lot of log data, making it hard to find what you need. To make debugging easier, developers might want to remove old or unnecessary log streams from a log group. This Python script offers a simple solution for that. It lets developers and AWS users delete logs from specific CloudWatch log groups quickly. Just put your log group’s name in the script, and you can get rid of logs you don’t need, making debugging smoother.

Understanding Your Existing Script

Your script is written in Python and utilizes Boto3 to interact with AWS services. Here’s a breakdown of what the script does:

  1. Initializes a Boto3 client for interacting with CloudWatch Logs.
  2. Defines the name of the log group containing the Lambda logs you wish to delete.
  3. Defines a function delete_log_streams that does the following:
    • Retrieves all log streams within the specified log group using pagination to handle large numbers of streams.
    • Iterates through each log stream and deletes it.
    • Handles any exceptions that may occur during deletion and prints out an error message.
  4. Executes the deletion function when the script is run as the main program, indicating progress and completion status via the console output.

Here’s the provided script:

import boto3

# Initialize the boto3 client for CloudWatch Logs
logs_client = boto3.client('logs', region_name='us-east-1')

# Specify the log group name (replace <your-lambda-log-group-name> with actual name)
log_group_name = '/aws/lambda/<your-lambda-log-group-name>'

def delete_log_streams(client, log_group_name):
    # Retrieve all log streams from the specified log group
    paginator = client.get_paginator('describe_log_streams')
    for page in paginator.paginate(logGroupName=log_group_name):
        # Extract log stream names from the current page
        log_streams = page['logStreams']
        
        # Iterate and delete each stream
        for log_stream in log_streams:
            try:
                # Retrieve the log stream name
                log_stream_name = log_stream['logStreamName']
                print(f"Deleting log stream: {log_stream_name}")
                
                # Delete the log stream
                client.delete_log_stream(
                    logGroupName=log_group_name,
                    logStreamName=log_stream_name
                )
            except Exception as e:
                print(f"Error deleting log stream: {log_stream_name}. Error: {e}")

if __name__ == '__main__':
    print(f"Deleting all log streams in the log group: {log_group_name}")
    delete_log_streams(logs_client, log_group_name)
    print("Done")

How to Use the Script

To use this script effectively, follow these steps:

  1. Set Up Your Environment: Ensure your machine has Python installed, along with Boto3 (pip install boto3). Also, make sure you have the necessary AWS permissions set up for the AWS account you’re accessing.
  2. Configure Credentials: Set up your AWS CLI or Boto3 configuration with the appropriate access key ID, secret access key, and default region.
  3. Customize the Log Group Name: Replace <your-lambda-log-group-name> with the actual name of the log group whose streams you want to delete.
  4. Run the Script: Execute the script by running python your_script_name.py in your terminal or command prompt.
  5. Monitor Output: The script will print messages to the console indicating progress. Once complete, it will display “Done.”

Conclusion

In summary, this Python script serves as a handy tool for developers and other AWS users who needs to clean up their AWS CloudWatch log groups. It streamlines the debugging process by programmatically removing old or unnecessary log streams. Remember to use the script responsibly and ensure that the logs being deleted are no longer needed for any operational or auditing purposes.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.