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"); }
public bool index(string username) { bool confid = false; var path = _hostingEnvironment.ContentRootPath; //make request based on //var MyIni = new IniFile(); // Or specify a specific name in a specific dir var MyIni = new IniFile(path + "\\INI.ini"); //You can write some values like so: var KeyAccess = MyIni.Read("KeyAccess"); var KeySecret = MyIni.Read("KeySecret"); HttpClient client = new HttpClient(); //client.DefaultRequestHeaders. AWSCommon comm = new AWSCommon(); Dictionary <string, string> httpHeaders = new Dictionary <string, string>(); comm.AddHeaders(httpHeaders, GetBaseUrl()); comm.AddAuthHeader("GET", GetBaseUrl(), httpHeaders, KeyAccess, KeySecret); string header = string.Empty; foreach (var item in httpHeaders) { header += ";"; header += item.Key + "." + item.Value; } //client.BaseAddress = new Uri(GetBaseUrl()); client.DefaultRequestHeaders.Add("Header", header); HttpResponseMessage response = client.GetAsync(GetBaseUrl() + "/api/Provider/validate").Result; //HttpResponseMessage response = client.GetAsync("api/customer/GetAll").Result; // Blocking call! if (response.IsSuccessStatusCode) { confid = true; } return(confid); }
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"); }
/// <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()); }