【问题标题】:How to list AWS EC2 regions with libcloud如何使用 libcloud 列出 AWS EC2 区域
【发布时间】:2019-02-13 01:47:24
【问题描述】:

有没有办法通过 libcloud 获取 AWS 上所有可用区域的列表?

我可以使用 powershell AWS SDK:

$regions = @(Get-AWSRegion)
foreach ($region in $regions)
{
$region.Region
}

如何使用 Python 和 libcloud 做同样的事情?

非常感谢, 强尼

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 region libcloud


    【解决方案1】:

    试试这个。获取 AWS 区域的粗略方法:

    from libcloud.compute.types import Provider
    
    aws_regions = []
    for kw,reg in Provider.__dict__.iteritems():
      if 'EC2' in kw and reg not in aws_regions:
        aws_regions.append(reg)
    
    print aws_regions
    

    【讨论】:

      【解决方案2】:

      我们可以利用 libcloud API list_regions()

       (Pdb) import libcloud
       (Pdb) p libcloud.__version__
       '1.5.0'
      
       (Pdb)  p driver.list_regions()
       ['ap-northeast-2', 'us-east-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-south-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-2', 'us-gov-west-1', 'us-west-1', 'eu-central-1', 'ap-northeast-1']
      

      【讨论】:

        【解决方案3】:

        我们可以在Provider.EC2 对象上使用get_driver() 函数。 list_regions() 方法将为您提供连接到区域时要传递的可接受值的列表。 list_regions

        from libcloud.compute.types import Provider
        from libcloud.compute.providers import get_driver
        
        driver = get_driver(Provider.EC2)
        
        driver.list_regions()
        ['ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'cn-north-1', 'cn-northwest-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-gov-west-1', 'us-west-1', 'us-west-2']
        

        【讨论】:

          猜你喜欢
          • 2018-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 2020-12-01
          相关资源
          最近更新 更多