Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirhopcount/cde5b6a44061e4f191f619ead20696c9 to your computer and use it in GitHub Desktop.
Save sirhopcount/cde5b6a44061e4f191f619ead20696c9 to your computer and use it in GitHub Desktop.
#!/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