使用Ruby获取AWS区域名称

时间:2012-07-26 02:07:24

标签: ruby amazon-web-services

我正在为AWS开发Rails应用程序,并希望为区域名称创建下拉菜单,例如“us-east-1”等。

如果某人已经创建了gem来获取它们,我想使用它。有谁知道这样的宝石或有用的API?

3 个答案:

答案 0 :(得分:4)

适用于Ruby的AWS开发工具包(aws-sdk gem)支持枚举区域名称:

require 'aws-sdk'

ec2 = AWS::EC2.new(:access_key_id => '...', :secret_access_key => '...')
ec2.regions.map(&:name)
=> ["eu-west-1", "sa-east-1", "us-east-1", "ap-northeast-1", "us-west-2", "us-west-1", "ap-southeast-1"] 

您还可以使用DescribeRegions调用的客户端界面:

ec2.client.describe_regions
=> { :region_info=>[
    {:region_name=>"eu-west-1", :region_endpoint=>"ec2.eu-west-1.amazonaws.com"}, 
    {:region_name=>"sa-east-1", :region_endpoint=>"ec2.sa-east-1.amazonaws.com"}, 
    {:region_name=>"us-east-1", :region_endpoint=>"ec2.us-east-1.amazonaws.com"}, 
    {:region_name=>"ap-northeast-1", :region_endpoint=>"ec2.ap-northeast-1.amazonaws.com"}, 
    {:region_name=>"us-west-2", :region_endpoint=>"ec2.us-west-2.amazonaws.com"}, 
    {:region_name=>"us-west-1", :region_endpoint=>"ec2.us-west-1.amazonaws.com"}, 
    {:region_name=>"ap-southeast-1", :region_endpoint=>"ec2.ap-southeast-1.amazonaws.com"}
  ], 
  :request_id=>"04458cac-bdf2-4847-bf1f-c7ea65813777"
} 

您可以在此处查看gem文档:http://docs.amazonwebservices.com/AWSRubySDK/latest/frames.html

答案 1 :(得分:1)

我可以这样做:

>> `ec2-describe-regions`.scan(/REGION\s+(\S+)/).flatten
=> ["eu-west-1", "sa-east-1", "us-east-1", "ap-northeast-1", "us-west-2", "us-west-1", "ap-southeast-1"]

答案 2 :(得分:1)