Skip to content

Instantly share code, notes, and snippets.

@dtenenba
Created January 31, 2016 18:46
Show Gist options
  • Save dtenenba/a8cd1efa9747d40fcce3 to your computer and use it in GitHub Desktop.
Save dtenenba/a8cd1efa9747d40fcce3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# to deregister an ami AND remove its associated snapshot
if ARGV.empty?
puts "usage: #{$0} <ami-id>"
exit
end
require 'aws-sdk'
ec2 = Aws::EC2::Client.new(region: 'us-east-1')
ami_id = ARGV.first
ami = ec2.describe_images({image_ids: [ami_id], owners: ['self']})
snapshot_id = ami.images.first.block_device_mappings.first.ebs.snapshot_id
ec2.deregister_image({dry_run: false, image_id: ami_id})
ec2.delete_snapshot({dry_run: false, snapshot_id: snapshot_id})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment