amazon ec2 - How to stop EC2 instance using Tag name in java? -


I know how to stop an example with your ID, but it is difficult to give EC 2 example ID every time. I can stop the example with tag name

  AmazonEC2 ec2 = new AmazonEC2Client; & Lt; String & gt; InstancesToStop = New ArrayList & lt; String & gt; (); InstancesToStop.add ("INSTANCE_ID"); StopInstancesRequest stoptr = New StopInstancesRequest (); Stoptr.setInstanceIds (instancesToStop); Ec2.stopInstances (stoptr);   

I tag name

To run an example with a matching tag, you can create a help method:

  public list & lt; String & gt; GetRunningInstancesByTags (string tagname, string value) {list & lt; String & gt; Example = New Arreelist & lt; String & gt; (); For (Reservation reservation: ec2client.describeInstances (). GetReservations (for example): reservation.getInstances ()) {if (! Instance.getState (). GetName (). Equal (InstanceStateName.Running.toString () )) { to continue; } (Tags: instance.getTags ()) {if (tag.getKey (.) Equals (tagname) & tag getValue (.) Equals (value) {instances.add (instance.getInstanceId () ); }}}} Returning Examples; }   

The method getRunningInstancesByTags just matches one tag, you can improve it to support more tag matching.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -