如何使用libcloud列出AWS EC2区域

问题描述 投票:0回答:3

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

使用PowerShell AWS SDK,我可以使用:

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

我怎么能用Python和libcloud做同样的事情?

非常感谢,约翰尼

python amazon-web-services amazon-ec2 region libcloud
3个回答
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

0
投票

我们可以使用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']

0
投票

我们可以在get_driver()对象上使用Provider.EC2函数。 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']
© www.soinside.com 2019 - 2024. All rights reserved.