Skip to content

Instantly share code, notes, and snippets.

@guss77
Last active December 6, 2017 12:22
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 guss77/ed002717a0d8025129dfe481f09d9bee to your computer and use it in GitHub Desktop.
Save guss77/ed002717a0d8025129dfe481f09d9bee to your computer and use it in GitHub Desktop.
Create a YAML mapping file for CloudFormation to select an Ubuntu AMI
#!/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