def create_and_attach_ebs_vol(
    ec2conn, availability_zone, instance_id, seed_snapshot_id, size, device,
):
    # Make a volume using the seed snapshot if given
    vol = None
    while not vol:
        try:
            if seed_snapshot_id:
                logger.info(
                    u"Creating an EBS volume from snapshot [%s]",
                    seed_snapshot_id
                )
                vol = ec2conn.create_volume(
                    size,
                    availability_zone,
                    snapshot=seed_snapshot_id)
            else:
                logger.info(u"Creating a new blank EBS volume")
                vol = ec2conn.create_volume(
                    size,
                    availability_zone)
        except:
            logging.info(
                "Error trying to create the volume for device [%s]",
                device)
            logging.info("Sleeping for [%s] and trying again", LAUNCH_REFRESH)
            time.sleep(LAUNCH_REFRESH)

    # Attach the volume, waiting until the volume is available first
    while not vol.status == 'available':
        logger.info(
            "vol [%s] still pending. Trying again in %ss",
            vol.id,
            LAUNCH_REFRESH)
        time.sleep(LAUNCH_REFRESH)
        vols = ec2conn.get_all_volumes(volume_ids=[vol.id])
        vol = vols[0]

    # First Instance in the first Reservation
    instance = ec2conn.get_all_instances(
        instance_ids=[instance_id])[0].instances[0]
    while instance.state != 'running':
        logger.info(
            "Waiting until ec2 instance %s is running for EBS attachment",
            instance_id)
        time.sleep(LAUNCH_REFRESH)
        instance.update()

    logger.info("Attaching the volume [%s] to [%s]", vol.id, device)
    attached = False
    while not attached:
        try:
            ec2conn.attach_volume(vol.id, instance_id, device=device)
            attached = True
            logger.info(
                "Successfully attached volume [%s] to [%s]",
                vol.id,
                device)
        except Exception, e:
            logger.warning(e)
            logger.warning(
                "Error attaching volume [%s] to instance [%s] on device [%s]",
                vol.id,
                instance_id,
                device)
            logger.info("Waiting %ss and retrying", LAUNCH_REFRESH)
            time.sleep(LAUNCH_REFRESH)

    return vol
Create and attach a volume to an instance, while tolerating some common ec2
dataset/ETHPy150Open winhamwr/neckbeard/neckbeard/cloud_provisioners/aws/ec2.py/create_and_attach_ebs_vol