Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Last active August 29, 2015 14:10
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 mryoshio/0732493f052db5ee9dd6 to your computer and use it in GitHub Desktop.
Save mryoshio/0732493f052db5ee9dd6 to your computer and use it in GitHub Desktop.
Create EC2 Image (AMI) of running instances
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