List of usage examples for com.amazonaws.services.ec2.model TerminateInstancesRequest withInstanceIds
public TerminateInstancesRequest withInstanceIds(java.util.Collection<String> instanceIds)
The IDs of the instances.
From source file:EC2LaunchWaitTerminate.java
License:Open Source License
public static void main(String[] args) throws Exception { System.out.println("==========================================="); System.out.println("Welcome to the AWS Java SDK!"); System.out.println("==========================================="); init();//from w ww.j av a 2 s . c om /* * Amazon EC2 * * The AWS EC2 client allows you to create, delete, and administer * instances programmatically. * * In this sample, we use an EC2 client to get a list of all the * availability zones, and all instances sorted by reservation id, then * create an instance, list existing instances again, wait a minute and * the terminate the started instance. */ try { DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones(); System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() + " Availability Zones."); /* using AWS Ireland. * TODO: Pick the zone where you have your AMI, sec group and keys */ ec2.setEndpoint("ec2.us-west-2.amazonaws.com"); DescribeInstancesResult describeInstancesRequest = ec2.describeInstances(); List<Reservation> reservations = describeInstancesRequest.getReservations(); Set<Instance> instances = new HashSet<Instance>(); for (Reservation reservation : reservations) { instances.addAll(reservation.getInstances()); } System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running."); System.out.println("Starting a new instance."); RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); /* TODO: configure to use your AMI, key and security group */ runInstancesRequest.withImageId("ami-ede6128d").withInstanceType("t2.micro").withMinCount(1) .withMaxCount(1).withKeyName("CNV-lab-AWS").withSecurityGroups("CNV-ssh+http"); RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest); String newInstanceId = runInstancesResult.getReservation().getInstances().get(0).getInstanceId(); describeInstancesRequest = ec2.describeInstances(); reservations = describeInstancesRequest.getReservations(); instances = new HashSet<Instance>(); for (Reservation reservation : reservations) { instances.addAll(reservation.getInstances()); } System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running."); System.out.println("Waiting 4 minutes. See your instance in the AWS console..."); Thread.sleep(60000 * 4); System.out.println("Terminating the instance."); TerminateInstancesRequest termInstanceReq = new TerminateInstancesRequest(); termInstanceReq.withInstanceIds(newInstanceId); ec2.terminateInstances(termInstanceReq); } catch (AmazonServiceException ase) { System.out.println("Caught Exception: " + ase.getMessage()); System.out.println("Reponse Status Code: " + ase.getStatusCode()); System.out.println("Error Code: " + ase.getErrorCode()); System.out.println("Request ID: " + ase.getRequestId()); } }
From source file:com.hazelcast.simulator.provisioner.AwsProvisioner.java
License:Open Source License
private void terminateInstances(List<Instance> instances) { List<String> ids = new ArrayList<String>(); for (Instance instance : instances) { ids.add(instance.getInstanceId()); }/* www. j a v a 2 s . com*/ TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); terminateInstancesRequest.withInstanceIds(ids); ec2.terminateInstances(terminateInstancesRequest); }
From source file:com.rmn.qa.aws.AwsVmManager.java
License:Open Source License
/** * Terminates the specified instance.// w ww . j a v a 2 s . c o m * * @param instanceId Id of the instance to terminate */ public boolean terminateInstance(final String instanceId) { TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(); terminateRequest.withInstanceIds(instanceId); AmazonEC2Client localClient = getClient(); if (localClient == null) { throw new RuntimeException("The client is not initialized"); } TerminateInstancesResult result; try { result = localClient.terminateInstances(terminateRequest); } catch (AmazonServiceException ase) { // If the node was terminated outside of this plugin, handle the error appropriately if (ase.getErrorCode().equals("InvalidInstanceID.NotFound")) { log.error("Node not found when attempting to remove: " + instanceId); return false; } else { throw ase; } } List<InstanceStateChange> stateChanges = result.getTerminatingInstances(); boolean terminatedInstance = false; for (InstanceStateChange stateChange : stateChanges) { if (instanceId.equals(stateChange.getInstanceId())) { terminatedInstance = true; InstanceState currentState = stateChange.getCurrentState(); if (currentState.getCode() != 32 && currentState.getCode() != 48) { log.error(String.format( "Machine state for id %s should be terminated (48) or shutting down (32) but was %s instead", instanceId, currentState.getCode())); return false; } } } if (!terminatedInstance) { log.error("Matching terminated instance was not found for instance " + instanceId); return false; } log.info(String.format("Node [%s] successfully terminated", instanceId)); return true; }
From source file:de.fischer.thotti.ec2.clients.EC2ExecutorITHelper.java
License:Apache License
private void terminateInstances(Region region, List<String> instances) { if (instances.isEmpty()) return;/* w w w . ja va2 s .co m*/ AmazonEC2Client client = getAmazonClient(); client.setEndpoint(region.getEndpoint()); TerminateInstancesRequest terminateReq = new TerminateInstancesRequest(); for (String id : instances) { if (logger.isDebugEnabled()) logger.debug("Going to terminate instance {} in region {}.", new Object[] { id, region.getRegionName() }); terminateReq.withInstanceIds(id); } client.terminateInstances(terminateReq); }
From source file:de.fischer.thotti.ec2.clients.EC2Terminator.java
License:Apache License
private void prepareRequestsPerRegion(EC2RequestData<TerminateInstancesRequest> requestData, RegionType region) {// w w w . j a v a2s .c o m TerminateInstancesRequest request = new TerminateInstancesRequest(); for (InstanceType server : region.getInstances()) { request.withInstanceIds(server.getInstanceID()); } requestData.getRegion(region.getName()).addRequest(request); }
From source file:elasticity.services.AmazonService.java
License:Open Source License
/** * Terminates a VM instance./*ww w . ja v a 2s . com*/ * * @param instanceIp The IP address of the instance to be removed */ public void terminateInstance(String instanceIp) throws Exception { logger.log(Level.INFO, "Terminating instance with IP: " + instanceIp + ".."); String instanceId = ip2id.get(instanceIp); TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); terminateInstancesRequest.withInstanceIds(instanceId); ec2.terminateInstances(terminateInstancesRequest); ip2id.remove(instanceIp); logger.log(Level.INFO, "Terminated instance successfully."); }
From source file:hu.mta.sztaki.lpds.cloud.entice.imageoptimizer.iaashandler.amazontarget.EC2VirtualMachine.java
License:Apache License
@Override protected void terminateInstance() throws VMManagementException { try {/*from w ww. j a v a 2 s.c o m*/ int requests = reqCounter.decrementAndGet(); if (requests < 0) { Shrinker.myLogger.severe("Terminating shrinking process, too much VM termination requests"); Thread.dumpStack(); } Shrinker.myLogger.info("Instance " + getInstanceId() + " received a terminate request"); TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); terminateInstancesRequest.withInstanceIds(getInstanceIds()); TerminateInstancesResult res = this.amazonEC2Client.terminateInstances(terminateInstancesRequest); Shrinker.myLogger.info("Terminate request dispatched for instance " + getInstanceId()); List<InstanceStateChange> stateChanges = res.getTerminatingInstances(); InstanceStateChange state = null; for (InstanceStateChange stateChange : stateChanges) if (getInstanceId().contains(stateChange.getInstanceId())) state = stateChange; if (state != null) Shrinker.myLogger.info("State of instance " + getInstanceId() + ": " + state.getPreviousState().getName() + " -> " + state.getCurrentState().getName()); else Shrinker.myLogger.info("null state for instance " + getInstanceId()); // re-send terminate if (state == null || !TERMINATED_STATE.equals(state.getCurrentState().getName())) { int timeout = 0; int counter = 1; while (timeout < TERMINATE_TIMEOUT) { try { Thread.sleep(5000); } catch (Exception x) { } Shrinker.myLogger.info("Re-sending (" + counter + "x) terminate request dispatched for instance " + getInstanceId()); try { res = this.amazonEC2Client.terminateInstances(terminateInstancesRequest); stateChanges = res.getTerminatingInstances(); for (InstanceStateChange stateChange : stateChanges) if (getInstanceId().contains(stateChange.getInstanceId())) state = stateChange; if (state != null) Shrinker.myLogger.info("State of instance " + getInstanceId() + ": " + state.getPreviousState().getName() + " -> " + state.getCurrentState().getName()); else Shrinker.myLogger.info("null state for instance " + getInstanceId()); } catch (AmazonServiceException x) { // terminated correctly // it can happen that terminate seemingly didn't succeed for the first time (remains running), // but then the instance id is gone (correctly) so re-sending terminate will cause exception if ("InvalidInstanceID.NotFound".equals(x.getErrorCode())) break; else throw x; } if (state != null && TERMINATED_STATE.equals(state.getCurrentState().getName())) break; timeout += 5000; // repeat every 5 second counter++; } if (timeout >= TERMINATE_TIMEOUT) { Shrinker.myLogger.info("ERROR: Cannot terminate instance: " + getInstanceId()); System.exit(1); } } } catch (AmazonServiceException x) { Shrinker.myLogger.info("terminateInstance error: " + x.getMessage()); throw new VMManagementException("terminateInstance exception", x); } catch (AmazonClientException x) { Shrinker.myLogger.info("terminateInstance error: " + x.getMessage()); throw new VMManagementException("terminateInstance exception", x); } System.out.println("[T" + (Thread.currentThread().getId() % 100) + "] VM terminated: " + getInstanceId() + " " + this.ip + " (@" + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + ")"); }
From source file:jp.primecloud.auto.process.aws.AwsInstanceProcess.java
License:Open Source License
public void terminate(AwsProcessClient awsProcessClient, Long instanceNo) { AwsInstance awsInstance = awsInstanceDao.read(instanceNo); String instanceId = awsInstance.getInstanceId(); // //from ww w . j a va 2 s . co m Instance instance = instanceDao.read(instanceNo); processLogger.debug(null, instance, "AwsInstanceDelete", new Object[] { awsProcessClient.getPlatform().getPlatformName(), instanceId }); // ? TerminateInstancesRequest request = new TerminateInstancesRequest(); request.withInstanceIds(instanceId); TerminateInstancesResult result = awsProcessClient.getEc2Client().terminateInstances(request); List<InstanceStateChange> terminatingInstances = result.getTerminatingInstances(); // API?? if (terminatingInstances.size() == 0) { // throw new AutoException("EPROCESS-000107", instanceId); } else if (terminatingInstances.size() > 1) { // ????? AutoException exception = new AutoException("EPROCESS-000108", instanceId); exception.addDetailInfo("result=" + terminatingInstances); throw exception; } // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100117", instanceId)); } // awsInstance.setStatus(terminatingInstances.get(0).getCurrentState().getName()); awsInstanceDao.update(awsInstance); }
From source file:net.roboconf.iaas.ec2.IaasEc2.java
License:Apache License
@Override public void terminateVM(String instanceId) throws IaasException { try {/*from w w w . jav a2 s . c o m*/ TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); terminateInstancesRequest.withInstanceIds(instanceId); this.ec2.terminateInstances(terminateInstancesRequest); } catch (AmazonServiceException e) { this.logger.severe("An error occurred on Amazon while terminating the machine. " + e.getMessage()); throw new IaasException(e); } catch (AmazonClientException e) { this.logger.severe("An error occurred while terminating a machine on Amazon EC2. " + e.getMessage()); throw new IaasException(e); } }
From source file:net.roboconf.target.ec2.internal.Ec2IaasHandler.java
License:Apache License
@Override public void terminateMachine(TargetHandlerParameters parameters, String machineId) throws TargetException { this.logger.fine("Terminating machine '" + machineId + "'."); cancelMachineConfigurator(machineId); try {//from www .j a v a 2 s . c o m AmazonEC2 ec2 = createEc2Client(parameters.getTargetProperties()); TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(); terminateInstancesRequest.withInstanceIds(machineId); ec2.terminateInstances(terminateInstancesRequest); } catch (Exception e) { this.logger.severe("An error occurred while terminating a machine on Amazon EC2. " + e.getMessage()); throw new TargetException(e); } }