【问题标题】:boto.rds2 connect to any region using boto profileboto.rds2 使用 boto 配置文件连接到任何区域
【发布时间】:2016-04-06 19:56:46
【问题描述】:

我有多个 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) 传递给函数,它将读取,否则它将选择在 aws boto 中设置的默认区域。同样,如果没有为配置文件提供选项,它会从 boto 获取默认配置文件。

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

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

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

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

【问题讨论】:

    标签: amazon-web-services boto


    【解决方案1】:

    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)
    

    【讨论】:

      【解决方案2】:

      这是通常的 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 非常相似。

      【讨论】:

        【解决方案3】:

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

        我使用以下方法来解决 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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 2019-05-27
          • 2013-07-06
          • 1970-01-01
          • 2015-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多