Created
November 11, 2015 07:04
-
-
Save ueki-kazuki/0bebfa1225e6805712b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "aws-sdk-core" | |
require "json" | |
ami_list = { "AWSAmazonLinuxAMI" => {} } | |
aws = Aws::EC2::Client.new(:region => 'us-east-1') | |
aws.describe_regions.regions.each do |region| | |
#next unless region.region_name == "ap-northeast-1" | |
ec2 = Aws::EC2::Client.new(:region => region.region_name) | |
latest_ver_of = {} | |
latest_images = {} | |
images = ec2.describe_images( | |
:owners => ["amazon"], | |
filters: [ | |
{ name: "state", values: ["available"] }, | |
{ name: "image-type", values: ["machine"] }, | |
{ name: "root-device-type", values: ["ebs"] }, | |
{ name: "architecture", values: ["x86_64"] }, | |
{ name: "virtualization-type", values: ["hvm"] }, | |
{ name: "name", values: ["amzn-ami*"] }, | |
]).images | |
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.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