EC2-使用API连接到运行中的实例 [英] EC2 - Connect to running instance by using the API
问题描述
我通过提供的接口创建一个EC2实例,并且我正在使用AWS API连接到现有的正在运行的实例,但是当我运行以下代码时,我得到您正在运行0个Amazon EC2实例". :
DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() +
" Availability Zones.");
DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
List<Reservation> reservations = describeInstancesRequest.getReservations();
Set<Instance> instances = new HashSet<Instance>();
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
您对可能是什么问题有任何想法吗?
如果您再次检查实例是否确实在运行,则它们很可能不在"us-east-1"实例区域(即默认为AWS开发工具包采用的默认值).
因此,将您的AmazonEC2Client
实例设置为指向正确的端点,一切都应该正常,例如欧洲(爱尔兰):
ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
在此 SO答案中,有更多详细信息以及指向可在其中找到端点字符串的链接... >
I create an EC2 instance via the provided interface, and I am using the AWS API to connect to the existing running instance, but when I run the following code I get "You have 0 Amazon EC2 instance(s) running.":
DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() +
" Availability Zones.");
DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
List<Reservation> reservations = describeInstancesRequest.getReservations();
Set<Instance> instances = new HashSet<Instance>();
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
Do you have any ideas about what might be the problem?
If you double checked that your instances are actually up and running, they most likely are not in the "us-east-1" instance region (which is the default one that the AWS SDK assumes).
So set your AmazonEC2Client
instance to point to the correct endpoint and everything should be fine, e.g. for Europe (Ireland):
ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
More details, as well as links to where you can find the endpoint strings, in this SO answer.
这篇关于EC2-使用API连接到运行中的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!