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."); } } }
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"); }