简体   繁体   中英

How to get an Instance reference for a newly created EC2 instance in aws java sdk?

Assume I have several running EC2 instances. Now I launch a new instance using this java code:

RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
        .withInstanceType("m1.small")
        .withImageId("some-ami")
        .withMinCount(1)
        .withMaxCount(1)
        .withKeyName("some-key")
        ;
RunInstancesResult runInstancesResule = ec2.runInstances(runInstancesRequest);

I know I can iterate the whole instance list to find a pending instance, but this seems stupid. Is there any easier way to get an Instance reference to this newly created EC2 instance?

Since the RunInstancesRequest can launch more than one instance at a time, it stores the instance(s) that were successfully launched in a List . Even if only one instance is launched, you'll still have to read it from the list, which can be accessed using your runInstancesResule variable. To get the first instance in the list, simply use the following:

Instance myInstance = runInstancesResule.getReservation().getInstances().get(0);

From here, you can then use the various instance related commands to get information about the instance (more info here ). For example:

String myInstanceID = myInstance.getInstanceId();

Edit:

The same concept can be used to retrieve all of your instances by using the DescribeInstancesRequest class as shown below:

DescribeInstancesRequest describeInstanceRequest = new DescribeInstancesRequest();
DescribeInstancesResult describeInstanceResult = ec2.describeInstances(describeInstanceRequest);
List<Instance> myInstances = describeInstanceResult.getReservation().getInstances();

Note: This will also include instances that have been stopped but not terminated, so you will need to check the instance state to determine if the instance is actually running or not.

I have created two methods with will launch one or more EC2 instance and second method will retrieve the instance based on instance id:

To request EC2:

public List<Instance> requestEC2(AmazonEC2 amazonEC2, String instanceName, String imageId, String instanceType, String securityGroupName, int count) {
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
        TagSpecification tagSpecification = new TagSpecification();
        tagSpecification.withResourceType(ResourceType.Instance).withTags(new Tag().withKey("Name").withValue(instanceName));
        runInstancesRequest.withImageId(imageId).withInstanceType(instanceType).withMinCount(1).withMaxCount(count).withSecurityGroups(securityGroupName).withTagSpecifications(tagSpecification);
        RunInstancesResult runInstancesResult = amazonEC2.runInstances(runInstancesRequest);
        return runInstancesResult.getReservation().getInstances();
    }

To retrieve EC2:

public List<Instance> getEC2InstanceDetails(AmazonEC2 amazonEC2, List<String> instanceIds){
        List<Instance> instanceList = new ArrayList<>();
        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
        describeInstancesRequest.setInstanceIds(instanceIds);
        DescribeInstancesResult describeInstancesResult = amazonEC2.describeInstances(describeInstancesRequest);
        for(Reservation reservation : describeInstancesResult.getReservations()){
            for(Instance instance : reservation.getInstances()) {
                instanceList.add(instance);
            }
        }
        return instanceList;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM