def handle_volume()

in cookbooks/aws-parallelcluster-environment/files/default/ec2_udev_rules/manageVolume.py [0:0]


def handle_volume(volume_id, attach, detach):
    token = get_imdsv2_token()

    instance_id = get_metadata_value(token, "http://169.254.169.254/latest/meta-data/instance-id")

    identity_document = get_metadata_value(token, "http://169.254.169.254/latest/dynamic/instance-identity/document")
    identity = json.loads(identity_document)
    region = identity["region"]

    proxy_config = parse_proxy_config()

    ec2 = boto3.client("ec2", region_name=region, config=proxy_config)

    if attach and is_volume_available(ec2, volume_id):
        attach_volume(volume_id, instance_id, ec2)
    elif detach and is_volume_attached(ec2, volume_id):
        detach_volume(volume_id, ec2)