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 I To run an example with a matching tag, you can create a help method: The method getRunningInstancesByTags just matches one tag, you can improve it to support more tag matching. 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);
tag name
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; }
Comments
Post a Comment