boto.rds2 使用 boto 配置文件连接到任何区域

boto.rds2 connect to any region using boto profile

我有多个 aws 帐户,我想通过脚本管理大部分工作。我可以使用 boto 配置文件连接到 ELB、EC2,但是我无法找到与 RDS 一起使用的相同机制。

对于 EC2 连接,我的示例函数如下所示:

def Ec2Conn(reg,profile = 'default'):
        ec2conn = ''
        try:
                ec2conn = boto.ec2.EC2Connection(profile_name=profile, region=boto.ec2.get_region(reg.strip()))
        except Exception, e:
                boto.log.error("Cannot validate provided AWS credentials: %s" % e)
        return(ec2conn)

如果将 reg(region) 传递给函数,它将读取该函数,否则它将 select 在 aws boto 中设置默认区域。同样,如果没有为配置文件提供选项,它会从 boto 获取默认配置文件。

但是我无法对 RDS 连接执行相同的操作。

示例代码,我认为它可以用于 RDS 与 boto 配置文件的连接,但不幸的是不工作:

def RDSConn(reg,profile = 'default'):
        rdsconn = ''
        try:
                rdsconn = boto.rds2.connect_to_region(region=boto.ec2.get_region(reg.strip()), profile_name=profile)
        except Exception, e:
                boto.log.error("Cannot validate provided AWS credentials: %s" % e)
        return(elbconn)

哎呀,这是敬畏! :

>>> dir(boto.rds2)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'connect_to_region', 'get_regions', 'regions']
>>>

Boto rds2 中没有任何配置文件方法。

我盒子上的运行Boto版本如下:

>>> print boto.Version
2.39.0

任何遇到同样问题的人。任何建议请。

这是通常的 AWS 总结(又名糟糕的文档),您必须找到分散的信息。 (Python help() 也没有给你太多信息)

这是有效的示例代码。假设您有一个 ~/.aws/credential 文件,文件中有一个 [default] 条目。 (解决方案示例可在此处找到 http://boto3.readthedocs.org/en/latest/guide/configuration.html

rds_conn = boto.rds2.connect_to_region("eu-central-l", profile_name="default")
#you cannot specifiy region=, and things like profile_name= not mentioned as arguments

AWS改变了boto.rds2的参数传递方式,其使用方法与boto3非常相似。

region 不是可选 key=value 参数的一部分,它是 必需的 参数。 boto.ec2.connect_to_region也是如此。

connect_to_region(region_name, **kw_params)

所以你的代码应该是:

rdsconn = boto.rds2.connect_to_region(boto.ec2.get_region(reg.strip()), profile_name=profile)

感谢大家的建议。我正在制作一个用于在 aws 上访问多个服务的模块,现在已经完成了。我不需要知道 aws 密钥、秘密密钥和所有的一切,我一劳永逸地自动化了我的东西。

我用下面的方法来解决 rds 连接问题:

def RDSConn(reg,profile = 'default'):
        rdsconn = ''
        endpt = 'rds.' + reg + '.amazonaws.com'
        reg = boto.regioninfo.RegionInfo(name=reg,endpoint=endpt)
        try:
                rdsconn=boto.connect_rds2(profile_name=profile, region=reg)
        except Exception, e:
                boto.log.error("Cannot validate provided AWS credentials: %s" % e)
        return(rdsconn)