AWS .NET SDK Jump start

Last week was a bit easy iin terms of workloads so I thought i should clean up the mess which is created by all developers in AWS universe. there were hundreds of EC2 instances without any information of cost center. i m an old school developer who only knows .net. everything else is an alien manuscript. after wasting few hours I realized to stick in my court. so i created a .net application to add custom tags to all resources

well you need to download .net SDK 3.0 first and then a sample AWS program.

private static void ApplyTags(AmazonEC2Client ec2Client) {
 DescribeInstancesResponse allinstances = ec2Client.DescribeInstances();
 //List<Reservation> res = allinstances.Reservation;
 string instanceId = string.Empty;
 foreach(Reservation res in allinstances.Reservations) {
   instanceId = res.Instances[0].InstanceId;
   Instance ins = res.Instances[0];
   Amazon.EC2.Model.Tag tag = ins.Tags.Find(t => t.Key == "cc");

   if (tag != null) {
    Console.WriteLine("Tag is " + ins.InstanceId + "   " + tag.Value);
    var response = ec2Client.DescribeVolumes(new DescribeVolumesRequest {
     Filters = new List < Amazon.EC2.Model.Filter > {
      new Amazon.EC2.Model.Filter {
       Name = "attachment.instance-id", Values = new List < string > {
        ins.InstanceId
       }
      }
     }
    }); //, new Amazon.EC2.Model.Filter { Name = "attachment.delete-on-termination", Values = new List<string> { "false" } }
    List < Volume > volumes = response.Volumes;
    foreach(Volume vol in volumes) {
     var volTag = vol.Tags.Find(x => x.Key == "cc");
     if (volTag == null) {
      ec2Client.CreateTags(new CreateTagsRequest {
       Resources = new List < string > {
         vol.VolumeId
        },
        Tags = new List < Amazon.EC2.Model.Tag > {
         new Amazon.EC2.Model.Tag {
          Key = tag.Key, Value = tag.Value
         }
        }
      });
     }

     var snapshotresponse = ec2Client.DescribeSnapshots(new DescribeSnapshotsRequest {
      Filters = new List < Amazon.EC2.Model.Filter > {
       new Amazon.EC2.Model.Filter {
        Name = "volume-id", Values = new List < string > {
         vol.VolumeId
        }
       }
      }
     });
     List < Snapshot > snapshots = snapshotresponse.Snapshots;
     foreach(Snapshot item in snapshots) {
      var snaptag = item.Tags.Find(x => x.Key == "cc");
      if (snaptag == null) {
       CreateTagsResponse crtags = ec2Client.CreateTags(new CreateTagsRequest {
        Resources = new List < string > {
          item.SnapshotId
         },
         Tags = new List < Amazon.EC2.Model.Tag > {
          new Amazon.EC2.Model.Tag {
           Key = tag.Key, Value = tag.Value
          }
         }
       });
       Console.WriteLine(crtags.ToString());
      }

     }
    }

    var networkresponse = ec2Client.DescribeNetworkInterfaces(new DescribeNetworkInterfacesRequest {
     Filters = new List < Amazon.EC2.Model.Filter > {
      new Amazon.EC2.Model.Filter {
       Name = "attachment.instance-id", Values = new List < string > {
        ins.InstanceId
       }
      },
      new Amazon.EC2.Model.Filter {
       Name = "attachment.delete-on-termination", Values = new List < string > {
        "true"
       }
      }
     }
    });
    List < Amazon.EC2.Model.NetworkInterface > lans = networkresponse.NetworkInterfaces;
    foreach(Amazon.EC2.Model.NetworkInterface lan in lans) {
     var volTag = lan.TagSet.Find(x => x.Key == "cc");
     if (volTag == null) {
      ec2Client.CreateTags(new CreateTagsRequest {
       Resources = new List < string > {
         lan.NetworkInterfaceId
        },
        Tags = new List < Amazon.EC2.Model.Tag > {
         new Amazon.EC2.Model.Tag {
          Key = tag.Key, Value = tag.Value
         }
        }
      });

     }
    }
   }

  }
  //for (int i=0; i<= InstanceState.)
  //Console.WriteLine("Launch Instance {0}", instanceId);
}

on similar pattern i wrote more code to get rid of old images, AMI and rest of the junks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s