6

我希望我的 python 脚本(连接到 aws)使用来自 ~/.aws/credentials 的 aws 配置文件

所以,我有 ~/.aws/credentials 文件:

[default]
aws_access_key_id = XXX
aws_secret_access_key = AAA
region = eu-west-1

[test]
aws_access_key_id = WWW
aws_secret_access_key = ZZZ
region = eu-west-1

我的python代码

import boto.ec2

conn = boto.ec2.EC2Connection(profile_name='test')
print conn
print conn.get_all_instances()

输出是

EC2Connection:ec2.us-east-1.amazonaws.com
[]

因此,它似乎没有从配置中获取配置文件。地区错了。

env | grep -i aws <- returns nothing

我错过了什么?

谢谢你。

===== 更新 =====

我也有一个 ~/.aws/config 文件,看起来像

⟩ cat config
[default]
output = text
region = eu-west-1

[profile test]
output = text
region = eu-west-1

===== 更新 =====

显然,它需要连接细节..但不是区域。所以,如果我这样做

reg = next(x for x in boto.ec2.regions() if x.name=='eu-west-1')

conn = boto.ec2.EC2Connection(profile_name='test', region=reg)
print conn
print conn.get_all_instances()

我可以获得实例列表。

所以,问题是如何从 AWS config/credentials 中获取默认的“区域”。

4

1 回答 1

2

credentials文件应该仅用于凭据。如果您想为您的配置文件提供其他属性,您应该在~/.aws/config文件中执行此操作。因此,在您的情况下,您将创建一个配置文件,其中包含:

[default]
region = eu-west-1

[test]
region = eu-west-1
于 2015-05-19T16:11:03.450 回答