Last active
August 29, 2015 14:10
-
-
Save mryoshio/0732493f052db5ee9dd6 to your computer and use it in GitHub Desktop.
Create EC2 Image (AMI) of running instances
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
require 'aws-sdk' | |
def main | |
ec2 = AWS::EC2.new | |
ec2.regions.each do |r| | |
puts "# region: #{r.name}" | |
r.instances.each do |i| | |
puts "## instance: #{i.id} #{i.tags[:Name]}" | |
if i.status != :running | |
puts "- [skipped] name: #{i.tags[:Name]} because it's not running." | |
next | |
end | |
name = "#{Time.now.strftime('%Y%m%d%H%M')}-#{i.tags[:Name].gsub(/\s+/, '-')}-#{i.id}" | |
desc = "AMI of #{i.id}, #{i.tags[:Name]} at #{Time.now}" | |
puts "- [begin to create AMI] #{name} at #{Time.now}" | |
# NOTE: `no_reboot` should be set false for data integrity | |
i.create_image(name, { description: desc, no_reboot: false}) | |
puts "- [finished] #{name} at #{Time.now}" | |
end | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment