Last active
December 6, 2017 12:22
-
-
Save guss77/ed002717a0d8025129dfe481f09d9bee to your computer and use it in GitHub Desktop.
Create a YAML mapping file for CloudFormation to select an Ubuntu AMI
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/ruby | |
require 'yaml' | |
require 'json' | |
require 'net/http' | |
def get_releases | |
JSON[Net::HTTP.get_response(URI.parse "https://cloud-images.ubuntu.com/locator/ec2/releasesTable").body.gsub(/,\s*\]/,"]")]["aaData"] | |
end | |
File.open("ubuntu-images.yaml","w") do |f| | |
f << ({ | |
"Mappings" => { | |
"UbuntuRegionImages" => get_releases.select do |i| | |
i[2] =~ /LTS/ and i[3] == "amd64" and i[4] == "hvm:ebs-ssd" | |
end.sort do |a,b| | |
a[5] <=> b[5] | |
end.collect do |i| | |
i[6].gsub! /<[^>]+>/, "" | |
i | |
end.inject({}) do |m,i| | |
(m[i[0]]||={})[i[1]] ||= i[6] | |
m | |
end | |
} | |
}).to_yaml | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment