我已经通过JAVA API成功启动、停止和检查以前创建的EC2实例的状态。然而,我在获取此实例的公共DNS地址方面遇到了困难。由于我使用StartInstancesRequest启动实例并使用StartInstancesResponse获取响应,因此无法检索实际的Instance对象。下面是我的起始代码,它可以工作:
BasicAWSCredentials oAWSCredentials = new BasicAWSCredentials(sAccessKey, sSecretKey);
AmazonEC2 ec2 = new AmazonEC2Client(oAWSCredentials);
ec2.setEndpoint("https://eu-west-1.ec2.amazonaws.com");
List<String> instanceIDs = new ArrayList<String>();
instanceIDs.add("i-XXXXXXX");
StartInstancesRequest startInstancesRequest = new StartInstancesRequest(instanceIDs);
try {
StartInstancesResult response = ec2.startInstances(startInstancesRequest);
System.out.println("Sent! "+response.toString());
}catch (AmazonServiceException ex){
System.out.println(ex.toString());
return false;
}catch(AmazonClientException ex){
System.out.println(ex.toString());
return false;
}
除此之外,通过JSch连接到该实例并获得任何帮助将不胜感激。