Home > Uncategorized > Clearing out #AWS #Cloudwatch in C# – Coded with help from #ChatGPT

Clearing out #AWS #Cloudwatch in C# – Coded with help from #ChatGPT

If you’re like me, you use AWS Cloudwatch when testing and debugging your Lambda functions on AWS, and then whenever your code is working, you end up leaving them in, “just in case” – Of course, that means you ignore them, until you get a bill from AWS for holding GB’s of logs, for no reason whatsoever.

So, here’s some C# code (Coded with the help of ChatCPT OpenAI with some modifications), to clear out all your Cloudwatch logs in all regions. You can be less heavy-handed, but I wanted to delete everything.

using Amazon;
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Amazon.Runtime;

class Program
{
    static void Main(string[] args)
    {
        const string accessKey = "xxxxxxxxxxxxxxx";
        const string secretKey = "xxxxxxxxxxxxxxx";

        var credentials = new BasicAWSCredentials(accessKey, secretKey);
        foreach (var region in Amazon.RegionEndpoint.EnumerableAllRegions)
        {
            Console.WriteLine(region.SystemName);
            var client = new AmazonCloudWatchLogsClient(credentials, region);
            try
            {
                // Get a list of all log groups
                DescribeLogGroupsResponse logGroupsResponse = null;
                try
                {
                    logGroupsResponse = client.DescribeLogGroupsAsync().Result;
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
                var logGroups = logGroupsResponse.LogGroups;

                // Iterate through each log group and delete it
                foreach (var logGroup in logGroups)
                {
                    // Get a list of all streams in the log group
                    var logStreamsResponse = client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest
                    {
                        LogGroupName = logGroup.LogGroupName
                    }).Result;
                    var logStreams = logStreamsResponse.LogStreams;

                    // Iterate through each stream and delete it
                    foreach (var logStream in logStreams)
                    {
                        client.DeleteLogStreamAsync(new DeleteLogStreamRequest
                        {
                            LogGroupName = logGroup.LogGroupName,
                            LogStreamName = logStream.LogStreamName
                        });
                        Thread.Sleep(TimeSpan.FromMilliseconds(50));
                        Console.WriteLine("Deleted log stream: " + logStream.LogStreamName + " in log group: " + logGroup.LogGroupName);
                    }

                    client.DeleteLogGroupAsync(new DeleteLogGroupRequest
                    {
                        LogGroupName = logGroup.LogGroupName
                    });
                    Thread.Sleep(TimeSpan.FromMilliseconds(50));
                    Console.WriteLine("Deleted log group: " + logGroup.LogGroupName);
                }

                Console.WriteLine("Deleted all log groups and streams in region: " + region.SystemName);
            }
            catch (AmazonCloudWatchLogsException e)
            {
                Console.WriteLine("Error while processing region " + region.SystemName);
                Console.WriteLine("Caught Exception: " + e.Message);
                Console.WriteLine("Response Status Code: " + e.StatusCode);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Type: " + e.ErrorType);
                Console.WriteLine("Request ID: " + e.RequestId);
            }
        }
    }
}

Hope this helps someone!

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: