Skip to content

Instantly share code, notes, and snippets.

@dreamnite
Created June 13, 2017 16:53
Show Gist options
  • Select an option

  • Save dreamnite/ab3751341d04d3a7bdaa95eef4b8e4dc to your computer and use it in GitHub Desktop.

Select an option

Save dreamnite/ab3751341d04d3a7bdaa95eef4b8e4dc to your computer and use it in GitHub Desktop.
Create a snapshot and share it to other accounts
my_instance = 'instance-id'
client = Aws::EC2::Client.new()
resp = client.create_image(instance_id: my_instance, name: 'sdksnaptest', description: 'Jp testing things with the sdk', no_reboot: true)
new_ami_id = resp.image_id
count = 0
begin
new_ami = client.describe_images(image_ids:[new_ami_id]).images.first
end while new_ami.block_device_mappings.empty?
snapshots = new_ami.block_device_mappings.map { |cur_device| cur_device.ebs.snapshot_id }
client.describe_snapshots(snapshot_ids: snapshots)
count = 0
while client.describe_snapshots(snapshot_ids: snapshots).snapshots.map { |snap| snap.progress }.select { |prog| prog == '100%' }.length != snapshots.length
p count
count = count + 1
sleep 1
raise 'taking way too long' if count == 3600
end
share_accounts = [ ]
share_accounts.each do |cur_account|
resp = client.modify_image_attribute(image_id: new_ami_id, launch_permission: { add: [ { user_id: cur_account } ] } )
resp = client.client.modify_snapshot_attribute(snapshot_id: new_ami.snapshot_id, create_volume_permission: { add: [ { user_id: cur_account } ] } )
p resp
end
### Current issues: hits share_accounts while snap is still pending - figure out how to return status of ami
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment