【问题标题】:Using aws profiles in python2 boto在 python2 boto 中使用 aws 配置文件
【发布时间】:2015-07-31 13:22:28
【问题描述】:

我希望我的 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 中获取默认的“区域”。

【问题讨论】:

  • 我注意到了同样的问题。我很好奇是否存在解决方案。

标签: python amazon-web-services amazon-ec2 boto aws-ec2


【解决方案1】:

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

[default]
region = eu-west-1

[test]
region = eu-west-1

【讨论】:

  • 谢谢。我已经有了定义区域的配置文件。用它更新了问题。
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2014-07-08
  • 2017-09-07
  • 1970-01-01
相关资源
最近更新 更多