Example #1
0
        public async Task <DeleteLogGroupResponse> DeleteLogGroupAsync(string name, bool throwIfNotFound = true, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var result = (throwIfNotFound) ?
                             _clientLogs.DeleteLogGroupAsync(new DeleteLogGroupRequest()
                {
                    LogGroupName = name
                }, cancellationToken) :
                             _clientLogs.DeleteLogGroupAsync(new DeleteLogGroupRequest()
                {
                    LogGroupName = name
                });

                return(await result.EnsureSuccessAsync());
            }
            catch (Amazon.CloudWatchLogs.Model.ResourceNotFoundException ex)
            {
                if (throwIfNotFound)
                {
                    throw ex;
                }

                return(new DeleteLogGroupResponse()
                {
                    HttpStatusCode = HttpStatusCode.NotFound
                });
            }
        }
Example #2
0
        protected virtual void Dispose(bool disposing)
        {
            AmazonCloudWatchLogsClient Client =
                new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2);

            foreach (var logGroupName in LogGroupNameList)
            {
                if (!(string.IsNullOrEmpty(logGroupName)))
                {
                    DescribeLogGroupsResponse describeLogGroupsResponse = Client.DescribeLogGroupsAsync(
                        new DescribeLogGroupsRequest
                    {
                        LogGroupNamePrefix = logGroupName
                    }).Result;

                    if (!(string.IsNullOrEmpty(describeLogGroupsResponse.LogGroups[0].LogGroupName)))
                    {
                        var response = Client.DeleteLogGroupAsync(new DeleteLogGroupRequest
                        {
                            LogGroupName = logGroupName
                        }).Result;
                    }
                }
            }
        }
        public void DataPipelineEndtoEndTest()
        {
            string bucketName = ResolvePhysicalId("PipelineStartBucket");
            string key        = Guid.NewGuid().ToString();

            var s3 = new AmazonS3Client();

            s3.PutObjectAsync(new PutObjectRequest()
            {
                BucketName = bucketName,
                Key        = key,
                FilePath   = Path.Combine(TEST_DATA_PATH, "sampledata.json")
            });

            Thread.Sleep(30000);

            var messages = GetLogMessages(ResolvePhysicalId("SingleEventLambda"));

            messages.Should().ContainMatch("*New York, NY E2E Test*");
            messages.Should().ContainMatch("*Manchester, UK E2E Test*");
            messages.Should().ContainMatch("*Arlington, VA E2E Test*");


            // 3. Delete object from S3 bucket (to allow a clean CloudFormation teardown)
            s3.DeleteObjectAsync(bucketName, key);

            // 4. Delete Lambda log groups
            var singleEventLambda = ResolvePhysicalId("SingleEventLambda");

            _logsClient.DeleteLogGroupAsync(
                new DeleteLogGroupRequest(GetLogGroup(singleEventLambda)));

            var bulkEventsLambda = ResolvePhysicalId("BulkEventsLambda");

            _logsClient.DeleteLogGroupAsync(
                new DeleteLogGroupRequest(GetLogGroup(bulkEventsLambda)));
        }
        // snippet-start:[CloudWatchLogs.dotnetv3.DeleteLogGroupExample]
        public static async Task Main()
        {
            var    client       = new AmazonCloudWatchLogsClient();
            string logGroupName = "cloudwatchlogs-example-loggroup";

            var request = new DeleteLogGroupRequest
            {
                LogGroupName = logGroupName,
            };

            var response = await client.DeleteLogGroupAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Successfully deleted CloudWatch log group, {logGroupName}.");
            }
        }
Example #5
0
        public async Task DeleteOrphanLogsAsync(bool dryRun, string awsProfile, string awsRegion)
        {
            Console.WriteLine();

            // initialize AWS profile
            await InitializeAwsProfile(awsProfile, awsRegion : awsRegion);

            var logsClient = new AmazonCloudWatchLogsClient(AWSConfigs.RegionEndpoint);

            // delete orphaned logs
            var totalLogGroups    = 0;
            var activeLogGroups   = 0;
            var orphanedLogGroups = 0;
            var skippedLogGroups  = 0;

            await DeleteOrphanLambdaLogsAsync();
            await DeleteOrphanApiGatewayLogs();
            await DeleteOrphanApiGatewayV2Logs();

            if ((orphanedLogGroups > 0) || (skippedLogGroups > 0))
            {
                Console.WriteLine();
            }
            Console.WriteLine($"Found {totalLogGroups:N0} log groups. Active {activeLogGroups:N0}. Orphaned {orphanedLogGroups:N0}. Skipped {skippedLogGroups:N0}.");

            // local functions
            async Task DeleteOrphanLambdaLogsAsync()
            {
                // list all lambda functions
                var lambdaClient        = new AmazonLambdaClient(AWSConfigs.RegionEndpoint);
                var request             = new ListFunctionsRequest {
                };
                var lambdaLogGroupNames = new HashSet <string>();

                do
                {
                    var response = await lambdaClient.ListFunctionsAsync(request);

                    foreach (var function in response.Functions)
                    {
                        lambdaLogGroupNames.Add($"/aws/lambda/{function.FunctionName}");
                    }
                    request.Marker = response.NextMarker;
                } while(request.Marker != null);

                // list all log groups for lambda functions
                await DeleteOrphanCloudWatchLogs(
                    "/aws/lambda/",
                    logGroupName => lambdaLogGroupNames.Contains(logGroupName),
                    logGroupName => Regex.IsMatch(logGroupName, @"^\/aws\/lambda\/[a-zA-Z0-9\-_]+$")
                    );
            }

            async Task DeleteOrphanApiGatewayLogs()
            {
                // list all API Gateway V1 instances
                var apiGatewayClient     = new AmazonAPIGatewayClient(AWSConfigs.RegionEndpoint);
                var request              = new GetRestApisRequest {
                };
                var apiGatewayGroupNames = new List <string>();

                do
                {
                    var response = await apiGatewayClient.GetRestApisAsync(request);

                    apiGatewayGroupNames.AddRange(response.Items.Select(item => $"API-Gateway-Execution-Logs_{item.Id}/"));
                    request.Position = response.Position;
                } while(request.Position != null);

                // list all log groups for API Gateway instances
                await DeleteOrphanCloudWatchLogs(
                    "API-Gateway-Execution-Logs_",
                    logGroupName => apiGatewayGroupNames.Any(apiGatewayGroupName => logGroupName.StartsWith(apiGatewayGroupName, StringComparison.Ordinal)),
                    logGroupName => Regex.IsMatch(logGroupName, @"^API-Gateway-Execution-Logs_[a-zA-Z0-9]+/.+$")
                    );
            }

            async Task DeleteOrphanApiGatewayV2Logs()
            {
                // list all API Gateway V2 instances
                var apiGatewayV2Client   = new AmazonApiGatewayV2Client(AWSConfigs.RegionEndpoint);
                var request              = new GetApisRequest {
                };
                var apiGatewayGroupNames = new List <string>();

                do
                {
                    var response = await apiGatewayV2Client.GetApisAsync(request);

                    apiGatewayGroupNames.AddRange(response.Items.Select(item => $"/aws/apigateway/{item.ApiId}/"));
                    request.NextToken = response.NextToken;
                } while(request.NextToken != null);

                // list all log groups for API Gateway instances
                await DeleteOrphanCloudWatchLogs(
                    "/aws/apigateway/",
                    logGroupName => (logGroupName == "/aws/apigateway/welcome") || apiGatewayGroupNames.Any(apiGatewayGroupName => logGroupName.StartsWith(apiGatewayGroupName, StringComparison.Ordinal)),
                    logGroupName => Regex.IsMatch(logGroupName, @"^/aws/apigateway/[a-zA-Z0-9]+/.+$")
                    );
            }

            async Task DeleteOrphanCloudWatchLogs(string logGroupPrefix, Func <string, bool> isActiveLogGroup, Func <string, bool> isValidLogGroup)
            {
                var describeLogGroupsRequest = new DescribeLogGroupsRequest {
                    LogGroupNamePrefix = logGroupPrefix
                };

                do
                {
                    var describeLogGroupsResponse = await logsClient.DescribeLogGroupsAsync(describeLogGroupsRequest);

                    totalLogGroups += describeLogGroupsResponse.LogGroups.Count;
                    foreach (var logGroup in describeLogGroupsResponse.LogGroups)
                    {
                        if (isActiveLogGroup(logGroup.LogGroupName))
                        {
                            // nothing to do
                            ++activeLogGroups;
                        }
                        else if (isValidLogGroup(logGroup.LogGroupName))
                        {
                            // attempt to delete log group
                            if (dryRun)
                            {
                                Console.WriteLine($"* deleted '{logGroup.LogGroupName}' (skipped)");
                                ++orphanedLogGroups;
                            }
                            else
                            {
                                try {
                                    await logsClient.DeleteLogGroupAsync(new DeleteLogGroupRequest {
                                        LogGroupName = logGroup.LogGroupName
                                    });

                                    Console.WriteLine($"* deleted '{logGroup.LogGroupName}'");
                                    ++orphanedLogGroups;
                                } catch {
                                    LogError($"could not delete '{logGroup.LogGroupName}'");
                                    ++skippedLogGroups;
                                }
                            }
                        }
                        else
                        {
                            // log group has an invalid name structure; skip it
                            Console.WriteLine($"SKIPPED '{logGroup.LogGroupName}'");
                            ++skippedLogGroups;
                        }
                    }
                    describeLogGroupsRequest.NextToken = describeLogGroupsResponse.NextToken;
                } while(describeLogGroupsRequest.NextToken != null);
            }
        }
Example #6
0
        public async Task DeleteOrphanLambdaLogsAsync(bool dryRun)
        {
            Console.WriteLine();

            // list all lambda functions
            var lambdaClient         = new AmazonLambdaClient();
            var listFunctionsRequest = new ListFunctionsRequest {
            };
            var lambdaLogGroupNames  = new HashSet <string>();

            do
            {
                var listFunctionsResponse = await lambdaClient.ListFunctionsAsync(listFunctionsRequest);

                foreach (var function in listFunctionsResponse.Functions)
                {
                    lambdaLogGroupNames.Add($"/aws/lambda/{function.FunctionName}");
                }
                listFunctionsRequest.Marker = listFunctionsResponse.NextMarker;
            } while(listFunctionsRequest.Marker != null);

            // list all log groups for lambda functions
            var logsClient = new AmazonCloudWatchLogsClient();
            var describeLogGroupsRequest = new DescribeLogGroupsRequest {
                LogGroupNamePrefix = "/aws/lambda/"
            };
            var totalLogGroups   = 0;
            var deletedLogGroups = 0;
            var skippedLogGroups = 0;

            do
            {
                var describeLogGroupsResponse = await logsClient.DescribeLogGroupsAsync(describeLogGroupsRequest);

                totalLogGroups += describeLogGroupsResponse.LogGroups.Count;
                foreach (var logGroup in describeLogGroupsResponse.LogGroups)
                {
                    if (lambdaLogGroupNames.Contains(logGroup.LogGroupName))
                    {
                        // nothing to do
                    }
                    else if (System.Text.RegularExpressions.Regex.IsMatch(logGroup.LogGroupName, @"^\/aws\/lambda\/[a-zA-Z0-9\-_]+$"))
                    {
                        // attempt to delete log group
                        if (dryRun)
                        {
                            Console.WriteLine($"* deleted '{logGroup.LogGroupName}' (skipped)");
                        }
                        else
                        {
                            try {
                                await logsClient.DeleteLogGroupAsync(new DeleteLogGroupRequest {
                                    LogGroupName = logGroup.LogGroupName
                                });

                                Console.WriteLine($"* deleted '{logGroup.LogGroupName}'");
                                ++deletedLogGroups;
                            } catch {
                                LogError($"could not delete '{logGroup.LogGroupName}'");
                            }
                        }
                    }
                    else
                    {
                        // log group has an invalid name structure; skip it
                        Console.WriteLine($"SKIPPED '{logGroup.LogGroupName}'");
                        ++skippedLogGroups;
                    }
                }
                describeLogGroupsRequest.NextToken = describeLogGroupsResponse.NextToken;
            } while(describeLogGroupsRequest.NextToken != null);
            if ((deletedLogGroups > 0) || (skippedLogGroups > 0))
            {
                Console.WriteLine();
            }
            Console.WriteLine($"Found {totalLogGroups:N0} log groups. Deleted {deletedLogGroups:N0}. Skipped {skippedLogGroups:N0}.");
        }