Example #1
0
        static void DescribeSnapshots(AmazonEC2Client client, Volume volume)
        {
            int generation           = 7;
            var describeSnapshotsReq = new DescribeSnapshotsRequest()
            {
                Filters = new List <Filter>()
                {
                    new Filter()
                    {
                        Name   = "volume-id",
                        Values = new List <string>()
                        {
                            volume.VolumeId
                        }
                    }
                }
            };
            var resp      = client.DescribeSnapshotsAsync(describeSnapshotsReq);
            var snapshots = resp.Result.Snapshots.OrderBy(i => i.StartTime).ToList();

            if (snapshots.Count >= generation)
            {
                var req = new DeleteSnapshotRequest(snapshots.First().SnapshotId);
                client.DeleteSnapshotAsync(req).Wait();
            }
        }
Example #2
0
        private static async Task DeleteOrphanSnapshots()
        {
            var client = new AmazonEC2Client(RegionEndpoint.APSoutheast2);
            var describeSnapshotRequest = new DescribeSnapshotsRequest();

            describeSnapshotRequest.OwnerIds.Add(ownerId);

            var snapshots = await client.DescribeSnapshotsAsync(describeSnapshotRequest);

            var describeImagesRequest = new DescribeImagesRequest();

            describeImagesRequest.Owners.Add(ownerId);

            var images = await client.DescribeImagesAsync(describeImagesRequest);

            const int numberOfdaysToKeepImage = 30;

            foreach (var snapshot in snapshots.Snapshots.Where(s =>
                                                               !images.Images.Any(i => i.BlockDeviceMappings.Any(d => d.Ebs.SnapshotId == s.SnapshotId)) &&
                                                               DateTime.Today.Subtract(s.StartTime).Days > numberOfdaysToKeepImage))
            {
                Console.WriteLine($"Deleting snapshot {snapshot.Description}...");
                await client.DeleteSnapshotAsync(new DeleteSnapshotRequest(snapshot.SnapshotId));
            }
        }
        public String CleanupFromReport(EBSCleanupInput input, ILambdaContext context)
        {
            AmazonEC2Client ec2Client = new AmazonEC2Client();

            String[] lines = new AWSCommon().GetS3ContextAsText(input.BucketName, input.Key).Split("\n".ToCharArray());
            List <Task <DeleteSnapshotResponse> > deleteTasks = new List <Task <DeleteSnapshotResponse> >();
            int index = 0;

            foreach (String line in lines)
            {
                if (input.StartIndex > index)
                {
                    index++;
                    continue;
                }

                //check if lambda timeout is near, if so invoke function recursively
                if (context.RemainingTime.Seconds < 20)
                {
                    context.Logger.LogLine("Lambda timouet near end, starting Lambda recursivly...");
                    var lambdaClient = new Amazon.Lambda.AmazonLambdaClient();
                    input.StartIndex = index;
                    lambdaClient.InvokeAsync(new Amazon.Lambda.Model.InvokeRequest()
                    {
                        InvocationType = Amazon.Lambda.InvocationType.Event,
                        FunctionName   = context.FunctionName,
                        Payload        = JsonConvert.SerializeObject(input)
                    }).Wait();
                    return("Started recursively with index=" + index);
                }

                string[] cells = line.Split(',');
                if (cells.Length > 1)
                {
                    string snapshotId = cells[1];
                    //check if snapshot id in appropriate format
                    if (new Regex("snap-(.*)").Match(snapshotId).Success)
                    {
                        context.Logger.LogLine($"CSV-L#{index} Deleting snapshot {snapshotId}..");
                        try {
                            var response = (ec2Client.DeleteSnapshotAsync(new DeleteSnapshotRequest()
                            {
                                SnapshotId = snapshotId
                            }));
                            response.Wait();
                        }catch (Exception ex) {
                            context.Logger.LogLine($"failed deleting snapshot {snapshotId}:\n{ex}");
                        }
                    }
                    else
                    {
                        context.Logger.LogLine($"Snapshot id {snapshotId} not in snap-xx format");
                    }
                }
                index++;
            }

            return("OK");
        }
Example #4
0
        private static async Task DeleteAmis()
        {
            var client    = new AmazonEC2Client(RegionEndpoint.APSoutheast2);
            var asgClient = new AmazonAutoScalingClient(RegionEndpoint.APSoutheast2);

            var request = new DescribeImagesRequest();

            request.Owners.Add(ownerId);
            var images = await client.DescribeImagesAsync(request);

            var launchConfigs = await asgClient.DescribeLaunchConfigurationsAsync();

            var dateToKeepAmisFrom = new DateTime(2018, 11, 03);

            foreach (var image in images.Images.Where(x => DateTime.Parse(x.CreationDate) < dateToKeepAmisFrom))
            {
                Console.WriteLine($"Deleting Image: {image.ImageId} - {image.Name}...");
                if (!image.Tags.Any(t => t.Key == "Master"))
                {
                    if (launchConfigs.LaunchConfigurations.Any(x => x.ImageId == image.ImageId))
                    {
                        Console.WriteLine("Skipped belonging to Launch configuration.");
                    }
                    else
                    {
                        await client.DeregisterImageAsync(new DeregisterImageRequest(image.ImageId));

                        var snapshotIds = image.BlockDeviceMappings.Select(x => x.Ebs.SnapshotId);
                        foreach (var snapshotId in snapshotIds)
                        {
                            Console.WriteLine($"Deleting Snapshot {snapshotId}...");
                            await client.DeleteSnapshotAsync(new DeleteSnapshotRequest(snapshotId));
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Skipping because having Tag of Master.");
                }
            }
        }
Example #5
0
        public String DeregisterReportedAMIs(AMICleanupInput input, ILambdaContext context)
        {
            var ec2Client = new AmazonEC2Client();
            var s3client  = new AmazonS3Client();

            String[] lines = new AWSCommon().GetS3ContextAsText(input.BucketName, input.Key).Split("\n".ToCharArray());
            int      index = 0;

            foreach (String line in lines)
            {
                if (input.StartIndex > index)
                {
                    if (index == input.StartIndex - 1)
                    {
                        context.Logger.LogLine($"Skipped processing up to index #{index}");
                    }
                    index++;
                    continue;
                }

                if (context.RemainingTime.Seconds < 10)
                {
                    context.Logger.LogLine($"Less than 10 seconds for lambda execution, starting function recursively..");
                    var lambdaClient = new Amazon.Lambda.AmazonLambdaClient();
                    input.StartIndex = index;
                    lambdaClient.InvokeAsync(new Amazon.Lambda.Model.InvokeRequest()
                    {
                        InvocationType = Amazon.Lambda.InvocationType.Event,
                        FunctionName   = context.FunctionName,
                        Payload        = JsonConvert.SerializeObject(input)
                    }).Wait();
                    return("Started recursively with index=" + index);
                }

                index = index + 1;

                String[] cells = line.Split(',');
                if (cells.Length >= 3)
                {
                    String amiId = cells[2];
                    if (amiId.StartsWith("ami-"))
                    {
                        try {
                            var describeResponse = ec2Client.DescribeImagesAsync(new DescribeImagesRequest()
                            {
                                ImageIds = new List <String>()
                                {
                                    amiId
                                }
                            });
                            describeResponse.Wait();

                            context.Logger.LogLine($"De-registering AMI {amiId}");
                            ec2Client.DeregisterImageAsync(new DeregisterImageRequest()
                            {
                                ImageId = amiId
                            }).Wait();

                            describeResponse.Result.Images[0].BlockDeviceMappings.ForEach(mapping => {
                                if (mapping.Ebs != null && mapping.Ebs.SnapshotId != null)
                                {
                                    context.Logger.LogLine($"Deleting snapshot {mapping.Ebs.SnapshotId} for ami {amiId}");
                                    ec2Client.DeleteSnapshotAsync(new DeleteSnapshotRequest()
                                    {
                                        SnapshotId = mapping.Ebs.SnapshotId
                                    }).Wait();
                                }
                            });
                        } catch (Exception ex) {
                            context.Logger.LogLine($"Failed to delete ami {amiId} with following error:");
                            context.Logger.LogLine(ex.ToString());
                        }
                    }
                    else
                    {
                        context.Logger.LogLine($"Skppingg non-ami id : {amiId}");
                    }
                }
            }


            return("OK");
        }