/// <summary> /// List all volumes found in region /// </summary> public static void ListVolumes() { AmazonEC2 ec2 = Ec2Helper.CreateClient(); DescribeVolumesRequest rq = new DescribeVolumesRequest(); DescribeVolumesResponse rs = ec2.DescribeVolumes(rq); foreach (Volume v in rs.DescribeVolumesResult.Volume) { Console.WriteLine(v.VolumeId); DescribeTagsRequest trq = new DescribeTagsRequest(); trq.WithFilter(new Filter() { Name = "resource-id", Value = new List <string>() { v.VolumeId } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (ResourceTag t in trs.DescribeTagsResult.ResourceTag) { Console.WriteLine(" " + t.Key + "=" + t.Value); } } }
/// <summary> /// Get the name of the instances with the given instanceID from EC2 /// </summary> /// <param name="instanceId"></param> /// <returns></returns> public static string GetInstanceName(string instanceId) { AmazonEC2 ec2 = CreateClient(); DescribeTagsRequest rq = new DescribeTagsRequest(); rq.WithFilter(new Filter() { Name = "resource-id", Value = new List <string>() { instanceId } }); DescribeTagsResponse rs = ec2.DescribeTags(rq); string name = ""; ResourceTag tag = rs.DescribeTagsResult.ResourceTag.Find(item => item.Key == "Name"); if (tag != null) { name = tag.Value; } return(name); }
public static void Backup(string name, string description, string volumeid, string volumename, string instancename, string expires) { Console.WriteLine("Creating snapshot of " + volumeid + " / " + volumename + " / " + instancename); AmazonEC2 ec2 = Ec2Helper.CreateClient(); CreateSnapshotRequest rq = new CreateSnapshotRequest(); rq.VolumeId = volumeid; rq.Description = description; CreateSnapshotResponse rs = ec2.CreateSnapshot(rq); string snapshotid = rs.CreateSnapshotResult.Snapshot.SnapshotId; // build tags for snapshot List <Tag> tags = new List <Tag>(); tags.Add(new Tag { Key = "Name", Value = name }); tags.Add(new Tag { Key = "source", Value = "scheduler" }); tags.Add(new Tag { Key = "instance", Value = instancename }); tags.Add(new Tag { Key = "volume", Value = volumename }); tags.Add(new Tag { Key = "expires", Value = expires.ToString() }); // get tags from volume to be applied to snapshot DescribeTagsRequest trq = new DescribeTagsRequest(); trq.WithFilter(new Filter() { Name = "resource-id", Value = new List <string>() { volumeid } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (ResourceTag t in trs.DescribeTagsResult.ResourceTag) { if (t.Key != "nextSnapshot" && t.Key != "lastSnapshot" && t.Key != "Name") { tags.Add(new Tag { Key = t.Key, Value = t.Value }); } } // apply tags to snapshopt CreateTagsRequest rqq = new CreateTagsRequest(); rqq.WithResourceId(snapshotid); rqq.WithTag(tags.ToArray()); var createTagResponse = ec2.CreateTags(rqq); }