def remove_ami(ec2resource, image):
    """Remove an AMI and its underlying snapshots."""
    snapshots = []
    for device in image.block_device_mappings:
        if 'Ebs' in device:
            snapshots.append(ec2resource.Snapshot(device['Ebs']['SnapshotId']))
    print('deregistering %s' % image.id)
    image.deregister()
    for snapshot in snapshots:
        print('deleting snapshot %s' % snapshot.id)
        snapshot.delete()
