Skip to content

Instantly share code, notes, and snippets.

@ueki-kazuki
Created November 11, 2015 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueki-kazuki/9abbbcc9f068b70c3ad1 to your computer and use it in GitHub Desktop.
Save ueki-kazuki/9abbbcc9f068b70c3ad1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "aws-sdk-v1"
require "json"
ami_list = { "AWSAmazonLinuxAMI" => {} }
AWS.regions.each do |region|
#next unless region.name == "ap-northeast-1"
AWS.config(:region => region.name)
ec2 = AWS::EC2.new
latest_ver_of = {}
latest_images = {}
images = ec2.images.with_owner(:amazon).
filter("state", "available").
filter("image-type", "machine").
filter("root-device-type", "ebs").
filter("architecture", "x86_64").
filter("virtualization-type", "hvm").
filter("name", "amzn-ami*")
images.each do |image|
next if image.platform == "windows"
next if image.name.include?("minimal")
next if image.name.include?("-pv-")
next if image.name.include?("vpc-nat")
next unless image.name.include?("gp2")
vers = []
begin
vers = image.name.match(/(\d+)\.(\d+)\.(\d+)/).to_a
rescue => e
$stderr.puts e
end
ami_majorver = vers.values_at(1,2).join
ami_minorver = vers.values_at(1,2,3).join
if latest_ver_of[ami_majorver].nil? || ami_minorver > latest_ver_of[ami_majorver]
latest_images[ami_majorver] = image.image_id
latest_ver_of[ami_majorver] = ami_minorver
end
end
ami_list["AWSAmazonLinuxAMI"][region.name] = Hash[latest_images.sort]
end
puts JSON.dump(ami_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment