boto的get_all_instances返回空

时间:2013-06-16 09:54:57

标签: boto

我正在尝试用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>

提前感谢大家

2 个答案:

答案 0 :(得分: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 documentation - get_only_instances

答案 1 :(得分: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]