Java Code Examples for com.amazonaws.services.ec2.AmazonEC2Client#terminateInstances()

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2Client#terminateInstances() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AwsDownscaleService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void terminateInstances(List<String> instanceIds, AmazonEC2Client amazonEC2Client) {
    try {
        amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
    } catch (AmazonServiceException e) {
        if (!INSTANCE_NOT_FOUND_ERROR_CODE.equals(e.getErrorCode())) {
            throw e;
        } else {
            List<String> runningInstances = instanceIds.stream()
                    .filter(instanceId -> !e.getMessage().contains(instanceId))
                    .collect(Collectors.toList());
            if (!runningInstances.isEmpty()) {
                amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(runningInstances));
            }
        }
        LOGGER.debug(e.getErrorMessage());
    }
}
 
Example 2
Source File: TerminateInstancesExample.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * Terminate specified instances (power-on the instances).
 *
 * @param instanceIDs
 *            IDs of the instances to terminate
 * @return a list of state changes for the instances
 */
public static List<InstanceStateChange> terminateInstances(final List<String> instanceIDs) {
    // pass any credentials as aws-mock does not authenticate them at all
    AWSCredentials credentials = new BasicAWSCredentials("foo", "bar");
    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials);

    // the mock endpoint for ec2 which runs on your computer
    String ec2Endpoint = "http://localhost:8000/aws-mock/ec2-endpoint/";
    amazonEC2Client.setEndpoint(ec2Endpoint);

    // send the terminate request with args as instance IDs
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.withInstanceIds(instanceIDs);
    TerminateInstancesResult result = amazonEC2Client.terminateInstances(request);

    return result.getTerminatingInstances();
}
 
Example 3
Source File: Ec2Util.java    From s3-bucket-loader with Apache License 2.0 5 votes vote down vote up
public void terminateEc2Instance(AmazonEC2Client ec2Client, String instanceId) throws Exception {
	try {
		TerminateInstancesRequest termReq = new TerminateInstancesRequest();
		List<String> instanceIds = new ArrayList<String>();
		instanceIds.add(instanceId);
		termReq.setInstanceIds(instanceIds);
		logger.debug("Terminating EC2 instances...." + Arrays.toString(instanceIds.toArray(new String[]{})));
		ec2Client.terminateInstances(termReq);
		
	} catch(Exception e) {
		logger.error("Unexpected error terminating: " + instanceId + " "+ e.getMessage(),e);
	}
}
 
Example 4
Source File: SimpleInstanceService.java    From sequenceiq-samples with Apache License 2.0 4 votes vote down vote up
private TerminateInstancesResult terminateInstances(AmazonEC2Client client, String... instanceIds) {
	TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest().withInstanceIds(instanceIds);
	TerminateInstancesResult terminateInstancesResult = client.terminateInstances(terminateInstancesRequest);
	return terminateInstancesResult;
}