1

我正在尝试使用 boto 获取所有正在运行的实例,因此:

import boto
conn = boto.connect_ec2(aws_access_key_id='...',aws_secret_access_key='...')
conn.get_all_instances();

但是虽然我确实有一个正在运行的实例,但我得到了一个空列表。

将调试提升到 2 时,我可以看到以下内容:

[DEBUG]:validating server certificate: hostname=ec2.us-east-1.amazonaws.com, certificate hosts=['ec2.us-east-1.amazonaws.com', 'ec2.amazon...
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Type: text/xml;charset=UTF-8
header: Transfer-Encoding: chunked
header: Date: Sun, 16 Jun 2013 09:45:00 GMT
header: Server: AmazonEC2
2013-06-16 09:45:00,982 foo [DEBUG]:<?xml version="1.0" encoding="UTF-8"?>
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-02-01/">
  <requestId>b30bbc2f-f553-419e-87db-6e453a3051f4</requestId>
  <reservationSet/>
</DescribeInstancesResponse>

提前谢谢大家

4

2 回答 2

2

一种快速获取区域中当前正在运行的实例的方法

import boto.ec2
ec2_connection = boto.ec2.connect_to_region(region, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
instances = ec2_connection.get_only_instances(filters={'instance-state-name': 'running'})

这将返回一个 boto.ec2.instance.Instance 列表

boto API 文档 - get_only_instances

于 2014-06-11T02:58:22.360 回答
1

为避免混淆,最好指定要连接的区域,而不是依赖默认值 us-east-1。尝试这个:

>>> import boto
>>> import boto.ec2
>>> region = boto.ec2.get_region('us-west-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
>>> print region
RegionInfo:us-west-1
>>> conn = boto.connect_ec2(aws_access_key_id=access_key,aws_secret_access_key=secret_key,region=region)
>>> conn.get_all_instances()
[Reservation:r-xxxx, Reservation:r-xxxx]
于 2013-09-23T01:27:55.437 回答