Search in sources :

Example 1 with Monitoring

use of com.amazonaws.services.ec2.model.Monitoring in project aws-doc-sdk-examples by awsdocs.

the class MonitorInstance method unmonitorInstance.

public static void unmonitorInstance(String instance_id) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DryRunSupportedRequest<UnmonitorInstancesRequest> dry_request = () -> {
        UnmonitorInstancesRequest request = new UnmonitorInstancesRequest().withInstanceIds(instance_id);
        return request.getDryRunRequest();
    };
    DryRunResult dry_response = ec2.dryRun(dry_request);
    if (!dry_response.isSuccessful()) {
        System.out.printf("Failed dry run to disable monitoring on instance %s", instance_id);
        throw dry_response.getDryRunResponse();
    }
    UnmonitorInstancesRequest request = new UnmonitorInstancesRequest().withInstanceIds(instance_id);
    ec2.unmonitorInstances(request);
    System.out.printf("Successfully disabled monitoring for instance %s", instance_id);
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DryRunResult(com.amazonaws.services.ec2.model.DryRunResult) UnmonitorInstancesRequest(com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)

Example 2 with Monitoring

use of com.amazonaws.services.ec2.model.Monitoring in project aws-doc-sdk-examples by awsdocs.

the class DescribeInstances method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while (!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);
        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                System.out.printf("Found instance 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());
            }
        }
        request.setNextToken(response.getNextToken());
        if (response.getNextToken() == null) {
            done = true;
        }
    }
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 3 with Monitoring

use of com.amazonaws.services.ec2.model.Monitoring in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method main.

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]
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) SdkClientException(com.amazonaws.SdkClientException) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 4 with Monitoring

use of com.amazonaws.services.ec2.model.Monitoring in project camel by apache.

the class EC2Producer method monitorInstances.

private void monitorInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    MonitorInstancesRequest request = new MonitorInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withInstanceIds(instanceIds);
    } else {
        throw new IllegalArgumentException("Instances Ids must be specified");
    }
    MonitorInstancesResult result;
    try {
        result = ec2Client.monitorInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Monitor Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Start Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) MonitorInstancesResult(com.amazonaws.services.ec2.model.MonitorInstancesResult) MonitorInstancesRequest(com.amazonaws.services.ec2.model.MonitorInstancesRequest)

Example 5 with Monitoring

use of com.amazonaws.services.ec2.model.Monitoring in project camel by apache.

the class EC2Producer method createAndRunInstance.

private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
    String ami;
    InstanceType instanceType;
    int minCount;
    int maxCount;
    boolean monitoring;
    String kernelId;
    boolean ebsOptimized;
    Collection securityGroups;
    String keyName;
    String clientToken;
    Placement placement;
    RunInstancesRequest request = new RunInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
        ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
        request.withImageId(ami);
    } else {
        throw new IllegalArgumentException("AMI must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
        instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
        request.withInstanceType(instanceType.toString());
    } else {
        throw new IllegalArgumentException("Instance Type must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
        minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
        request.withMinCount(minCount);
    } else {
        throw new IllegalArgumentException("Min instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
        maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
        request.withMaxCount(maxCount);
    } else {
        throw new IllegalArgumentException("Max instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
        monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
        request.withMonitoring(monitoring);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
        kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
        request.withKernelId(kernelId);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
        ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
        request.withEbsOptimized(ebsOptimized);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
        securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
        request.withSecurityGroups(securityGroups);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
        keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
        request.withKeyName(keyName);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
        clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
        request.withClientToken(clientToken);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
        placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
        request.withPlacement(placement);
    }
    RunInstancesResult result;
    try {
        result = ec2Client.runInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) Placement(com.amazonaws.services.ec2.model.Placement) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceType(com.amazonaws.services.ec2.model.InstanceType)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 Collection (java.util.Collection)3 Message (org.apache.camel.Message)3 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 DryRunResult (com.amazonaws.services.ec2.model.DryRunResult)2 Instance (com.amazonaws.services.ec2.model.Instance)2 InstanceMonitoring (com.amazonaws.services.ec2.model.InstanceMonitoring)2 MonitorInstancesRequest (com.amazonaws.services.ec2.model.MonitorInstancesRequest)2 MonitorInstancesResult (com.amazonaws.services.ec2.model.MonitorInstancesResult)2 Monitoring (com.amazonaws.services.ec2.model.Monitoring)2 Reservation (com.amazonaws.services.ec2.model.Reservation)2 UnmonitorInstancesRequest (com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)2 UnmonitorInstancesResult (com.amazonaws.services.ec2.model.UnmonitorInstancesResult)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 SdkClientException (com.amazonaws.SdkClientException)1 Filter (com.amazonaws.services.ec2.model.Filter)1 InstanceType (com.amazonaws.services.ec2.model.InstanceType)1