public Stream UploadEBSReport(EBSReportInput input, ILambdaContext context) { this.context = context; uploadContent(getImagesAsCsv(input, context), input.BucketName, input.Key); return(new MemoryStream(Encoding.UTF8.GetBytes("OK"))); }
/// <summary> /// Return csv file content for EBS snapshot /// </summary> /// <returns>The images as csv.</returns> /// <param name="input">Input.</param> /// <param name="context">Context.</param> private String getImagesAsCsv(EBSReportInput input, ILambdaContext context) { String accountId = context.InvokedFunctionArn.Split(':')[4]; var ec2Client = new AmazonEC2Client(); var getSnapshotsRequest = new DescribeSnapshotsRequest(); getSnapshotsRequest.MaxResults = 1000; //show only owned completed snapshots getSnapshotsRequest.Filters.Add(new Amazon.EC2.Model.Filter("status", new List <string> { "completed" })); getSnapshotsRequest.Filters.Add(new Amazon.EC2.Model.Filter("owner-id", new List <string> { accountId })); List <Snapshot> snapshots = new List <Snapshot>(); do { var taskResponse = ec2Client.DescribeSnapshotsAsync(getSnapshotsRequest); taskResponse.Wait(); snapshots.AddRange(taskResponse.Result.Snapshots); context.Logger.LogLine($"Added {taskResponse.Result.Snapshots.Count} snapshots to results list"); getSnapshotsRequest.NextToken = taskResponse.Result.NextToken; } while (getSnapshotsRequest.NextToken != null); var awsCommon = new AWSCommon(); var allAMIs = awsCommon.GetAllOwnedPrivateAMIs(context); var sb = new StringBuilder(); sb.Append("DateCreated,SnapID,Name,Description,AMIRelated,AMIExists,AMI-ID,Tags\n"); snapshots.Sort((s1, s2) => - 1 * s1.StartTime.CompareTo(s2.StartTime)); snapshots.ForEach(snapshot => { var nameTag = snapshot.Tags.Find(t => t.Key.Equals("Name")); var name = nameTag != null ? nameTag.Value : ""; var notNameTags = String.Join(" ", snapshot.Tags.FindAll(t => t.Key != "Name").Select(t => $"{t.Key}={t.Value}")); bool isAmiRelated = false; bool amiExists = false; String amiId = ""; //check if ebs snapshots is related to an AMI if (snapshot.Description != null) { var amiRegex = new Regex("Created by (.*) for ami-(.*) from (.*)").Match(snapshot.Description); isAmiRelated = amiRegex.Success; amiId = isAmiRelated ? "ami-" + amiRegex.Groups[2] : ""; amiExists = allAMIs.Find(i => i.ImageId.Equals(amiId)) != null; } //if only orphans to be reported, check if orphan (related to ami, ami does not exist) if (!input.OnlyAMIOrphans) { sb.Append($"{snapshot.StartTime},{snapshot.SnapshotId},{name},{snapshot.Description},{isAmiRelated},{amiExists},{amiId},{notNameTags}\n"); } else if (isAmiRelated && !amiExists) { sb.Append($"{snapshot.StartTime},{snapshot.SnapshotId},{name},{snapshot.Description},{isAmiRelated},{amiExists},{amiId},{notNameTags}\n"); } else if (isAmiRelated && amiExists) { context.Logger.LogLine($"Skipping snap {snapshot.SnapshotId} as AMI {amiId} exists"); } else if (!isAmiRelated) { context.Logger.LogLine($"Skipping snap {snapshot.SnapshotId} as non-ami related snapshot"); } }); return(sb.ToString()); }