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);
}
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;
}
}
}
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]
}
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);
}
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);
}
Aggregations