def terminate_and_clean(instances):
 volumes_to_delete = []
 for instance in instances:
  for bdmap in instance.block_device_mappings:
   if ('Ebs' in bdmap.keys()):
    if (not bdmap['Ebs']['DeleteOnTermination']):
     volumes_to_delete.append(bdmap['Ebs']['VolumeId'])
 for instance in instances:
  instance.terminate()
 _ids = [instance.id for instance in instances]
 all_terminated = False
 while (not all_terminated):
  all_terminated = True
  for _id in _ids:
   inst = EC2.Instance(id=_id)
   if (inst.state['Name'] != 'terminated'):
    all_terminated = False
  time.sleep(5)
 for vol_id in volumes_to_delete:
  volume = EC2.Volume(id=vol_id)
  volume.delete()
 return volumes_to_delete

def terminate_and_clean ( instances ) : volumes_to_delete = [ ] for instance in instances : for bdmap in instance . block_device_mappings : if ( 'Ebs' in bdmap . keys ( ) ) : if ( not bdmap [ 'Ebs' ] [ 'DeleteOnTermination' ] ) : volumes_to_delete . append ( bdmap [ 'Ebs' ] [ 'VolumeId' ] ) for instance in instances : instance . terminate ( ) _ids = [ instance . id for instance in instances ] all_terminated = False while ( not all_terminated ) : all_terminated = True for _id in _ids : inst = EC2 . Instance ( id = _id ) if ( inst . state [ 'Name' ] != 'terminated' ) : all_terminated = False time . sleep ( 5 ) for vol_id in volumes_to_delete : volume = EC2 . Volume ( id = vol_id ) volume . delete ( ) return volumes_to_delete
some amis specify ebs stores that wont delete on instance termination .