Java Code Examples for com.amazonaws.services.ec2.model.DescribeInstancesRequest#withFilters()

The following examples show how to use com.amazonaws.services.ec2.model.DescribeInstancesRequest#withFilters() . 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: AutomationReaperTask.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void doWork() {
    log.info("Running " + AutomationReaperTask.NAME);
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    Filter filter = new Filter("tag:LaunchSource");
    filter.withValues("SeleniumGridScalerPlugin");
    describeInstancesRequest.withFilters(filter);
    List<Reservation> reservations = ec2.describeInstances(describeInstancesRequest);
    for(Reservation reservation : reservations) {
        for(Instance instance : reservation.getInstances()) {
            // Look for orphaned nodes
            Date threshold = AutomationUtils.modifyDate(new Date(),-30, Calendar.MINUTE);
            String instanceId = instance.getInstanceId();
            // If we found a node old enough AND we're not internally tracking it, this means this is an orphaned node and we should terminate it
            if(threshold.after(instance.getLaunchTime()) && !AutomationContext.getContext().nodeExists(instanceId)) {
                log.info("Terminating orphaned node: " + instanceId);
                ec2.terminateInstance(instanceId);
            }
        }
    }
}
 
Example 2
Source File: AwsEc2SeedHostsProvider.java    From crate with Apache License 2.0 6 votes vote down vote up
private DescribeInstancesRequest buildDescribeInstancesRequest() {
    final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
        .withFilters(
            new Filter("instance-state-name").withValues("running", "pending")
        );

    for (final Map.Entry<String, List<String>> tagFilter : tags.entrySet()) {
        // for a given tag key, OR relationship for multiple different values
        describeInstancesRequest.withFilters(
            new Filter("tag:" + tagFilter.getKey()).withValues(tagFilter.getValue())
        );
    }

    if (!availabilityZones.isEmpty()) {
        // OR relationship amongst multiple values of the availability-zone filter
        describeInstancesRequest.withFilters(
            new Filter("availability-zone").withValues(availabilityZones)
        );
    }

    return describeInstancesRequest;
}
 
Example 3
Source File: AWSProvider.java    From testgrid with Apache License 2.0 5 votes vote down vote up
private static DescribeInstancesResult getDescribeInstancesResult(String stackName, String region) {
    final AmazonEC2 amazonEC2 = getAmazonEC2(region);

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.withFilters(
            new Filter("tag:" + STACK_NAME_TAG_KEY).withValues(stackName));

    return amazonEC2.describeInstances(request);
}
 
Example 4
Source File: FindRunningInstances.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[ec2.java1.running_instances.main]
        AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

        try {
            //Create the Filter to use to find running instances
            Filter filter = new Filter("instance-state-name");
            filter.withValues("running");

            //Create a DescribeInstancesRequest
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.withFilters(filter);

            // Find the running instances
            DescribeInstancesResult response = ec2.describeInstances(request);

            for (Reservation reservation : response.getReservations()){

                for (Instance instance : reservation.getInstances()) {

                    //Print out the results
                    System.out.printf(
                            "Found reservation with id %s, " +
                                    "AMI %s, " +
                                    "type %s, " +
                                    "state %s " +
                                    "and monitoring state %s",
                            instance.getInstanceId(),
                            instance.getImageId(),
                            instance.getInstanceType(),
                            instance.getState().getName(),
                            instance.getMonitoring().getState());
                }
            }
            System.out.print("Done");

        } catch (SdkClientException e) {
            e.getStackTrace();
        }
        // snippet-end:[ec2.java1.running_instances.main]
    }