use of software.amazon.awssdk.services.ec2.model.Instance 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 software.amazon.awssdk.services.ec2.model.Instance 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 software.amazon.awssdk.services.ec2.model.Instance in project SimianArmy by Netflix.
the class InstanceInSecurityGroup method getInstanceSecurityGroups.
/**
* Gets the security groups for a list of instance ids of the same region. The default implementation
* is using an AWS client. The method can be overridden in subclasses to get the security groups differently.
* @param region
* the region of the instances
* @param instanceIds
* the instance ids, all instances should be in the same region.
* @return
* the map from instance id to the list of security group names the instance has
*/
protected Map<String, List<String>> getInstanceSecurityGroups(String region, String... instanceIds) {
Map<String, List<String>> result = Maps.newHashMap();
if (instanceIds == null || instanceIds.length == 0) {
return result;
}
AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
for (Instance instance : awsClient.describeInstances(instanceIds)) {
// Ignore instances that are in VPC
if (StringUtils.isNotEmpty(instance.getVpcId())) {
LOGGER.info(String.format("Instance %s is in VPC and is ignored.", instance.getInstanceId()));
continue;
}
if (!"running".equals(instance.getState().getName())) {
LOGGER.info(String.format("Instance %s is not running, state is %s.", instance.getInstanceId(), instance.getState().getName()));
continue;
}
List<String> sgs = Lists.newArrayList();
for (GroupIdentifier groupId : instance.getSecurityGroups()) {
sgs.add(groupId.getGroupName());
}
result.put(instance.getInstanceId(), sgs);
}
return result;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project SimianArmy by Netflix.
the class AWSClient method describeInstances.
/**
* Describe a set of specific instances.
*
* @param instanceIds the instance ids
* @return the instances
*/
public List<Instance> describeInstances(String... instanceIds) {
if (instanceIds == null || instanceIds.length == 0) {
LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
} else {
LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
}
List<Instance> instances = new LinkedList<Instance>();
AmazonEC2 ec2Client = ec2Client();
DescribeInstancesRequest request = new DescribeInstancesRequest();
if (instanceIds != null) {
request.withInstanceIds(Arrays.asList(instanceIds));
}
DescribeInstancesResult result = ec2Client.describeInstances(request);
for (Reservation reservation : result.getReservations()) {
instances.addAll(reservation.getInstances());
}
LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
return instances;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project SimianArmy by Netflix.
the class AWSClient method getVpcId.
/**
* Gets the VPC id for the given instance.
*
* @param instanceId
* instance we're checking
* @return vpc id, or null if not a vpc instance
*/
String getVpcId(String instanceId) {
Instance awsInstance = describeInstance(instanceId);
String vpcId = awsInstance.getVpcId();
if (Strings.isNullOrEmpty(vpcId)) {
return null;
}
return vpcId;
}
Aggregations