Search code examples
aws-lambdaaws-sdkaws-device-farm

How to install an upload/app into Device farm remote session


I am facing issue in devicefarm.installToRemoteAccessSession.

I am successfully able to create a remote session using aws-sdk from lambda function. Next, i tried to install an already existing upload into the created remote session.

code

 let remoteSession = await devicefarm.createRemoteAccessSession(params).promise()
.then(res => {
      console.log(res);
      let installParm = {
        appArn: uploadArn,
        remoteAccessSessionArn: res.remoteAccessSession.arn
      };
      devicefarm.installToRemoteAccessSession(installParm).promise()
      .then(res => console.log(res))
      .catch(err => console.log(err));
      return res;
    })
    .catch(err => err);

I am getting the below error. [i replaced the id's with *]

message: 'Invalid parameters RemoteAccessSession or Upload with identifiers: arn:aws:devicefarm:us-west-2:*:session:**/**/00000 or arn:aws:devicefarm:us-west-2:*:upload:***/**.',
code: 'ArgumentException',

Whether I can give upload ARN for appArn. what is the difference between these two? If appARN or different where can i get?


Solution

  • So the reason in my case after I reproduced this was because the remote access session had not started yet. I had requested one, but the Device Farm service didn't acquire a device at the same time which is expected.

    After I waited for the session to begin by watching the web console the upload was successful. So the code would need to have some check there to see the status of the remote access session using the get-remote-access call and the status attribute returned.

    Update

    I wrote this javascript code to wait for the remote session to go to a status of RUNNING.

    // assume we already executed `npm install aws-sdk`
    var AWS = require('aws-sdk');
    // assumes `npm install https`
    const request = require("request");
    // assumes `npm install fs`
    const fs = require('fs');
    // https://stackoverflow.com/a/41641607/8016330
    const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
    // Device Farm is only available in the us-west-2 region
    var devicefarm = new AWS.DeviceFarm({ region: 'us-west-2' });
    
    let params = {
        name: "test of remote access"
    };
    devicefarm.createProject(params).promise().then(
        function (data) {
            /* process the data */
            console.log("created project", "Result: ", data.project.arn);
            let project = data.project;
            let params = {
                name: "app-debug.apk",
                type: "ANDROID_APP",
                projectArn: project.arn
            };
            devicefarm.createUpload(params).promise().then(
                function (data) {
                    console.log("Created upload object successfully", "upload arn: ", data.upload.arn);
                    console.log("uploading file...");
    
                    var options = {
                        method: 'PUT',
                        url: data.upload.url,
                        headers: {},
                        body: fs.readFileSync("./aws-device-farm-sample-app-for-android/app/build/outputs/apk/app-debug.apk")
                    };
    
                    new Promise(function (resolve, reject) {
                        request(options, function (error, response, body) {
                            if (error) {
                                reject(error);
                            }
                            resolve(data);
                        });
                    }).then(function (data) {
                        console.log("successfully uploaded file");
                        console.log("waiting for the sdk to finish processing");
    
                        getUploadStatusAndCreateRemoteSession(data.upload.arn,project).then(function (status) {
                            if (status == "SUCCEEDED") {
    
                            }
    
                        });
                    }).catch(function (err) {
                        console.error("Error uploading file", "Error: ", err);
                    });
                },
                function (error) {
                    console.error("Error creating upload object", "Error: ", error);
                }
            );
        },
        function (error) {
            console.error("Error creating project", "Error: ", error);
        }
    )
    
    async function getUploadStatusAndCreateRemoteSession(uploadarn,project) {
        await devicefarm.getUpload({ arn: uploadarn }).promise().then(
            function (data) {
                console.log("getting upload status is successful", "Status: ", data.upload.status);
                if (data.upload.status != "SUCCEEDED") {
                    sleep(5000).then(() => {
                        getUploadStatusAndCreateRemoteSession(data.upload.arn,project);
                    });
                } else {
                    // return data.upload.status;
                    devicefarm.createRemoteAccessSession({
                        projectArn: project.arn,
                        deviceArn: 'arn:aws:devicefarm:us-west-2::device:CF6DC11E4C99430BA9A1BABAE5B45364'
                    }).promise().then(
                        function (session) {
                            //get session status
                            getSessionStatus(session.remoteAccessSession.arn,uploadarn);
                        },
                        function (error) {
                            console.error("Error creating remote access sesssion", "Error", error);
                        }
                    );
                }
            },
            function (error) {
                console.error("Failure getting upload", "Error: ", error);
                return error;
            }
        );
    }
    
    async function getSessionStatus(sessionArn,upload) {
        await devicefarm.getRemoteAccessSession({ arn: sessionArn }).promise().then(
            function (data) {
                console.log("getting session status is successful", "Status: ", data.remoteAccessSession.status);
                if (data.remoteAccessSession.status != "RUNNING") {
                    sleep(5000).then(() => {
                        getSessionStatus(sessionArn,upload);
                    });
                } else {
    
                    console.log("Remote access session started!", "Installing app...");
                    let installParm = {
                        appArn: upload,
                        remoteAccessSessionArn: sessionArn
                    };
                    devicefarm.installToRemoteAccessSession(installParm, function (err, data) {
                        if (err) console.error(err);
                        else console.log(data);
                    });
    
                }
            },
            function (error) {
                console.error("Failure getting session", "Error: ", error);
                return error;
            }
        );
    }
    

    Output:

    created project Result:  arn:aws:devicefarm:us-west-2:111122223333:project:42ca0449-2714-4dd8-848f-ae9ef6655efb
    Created upload object successfully upload arn:  arn:aws:devicefarm:us-west-2:111122223333:upload:42ca0449-2714-4dd8-848f-ae9ef6655efb/cd200f8e-e7f7-4d18-a4ee-32ad959a0786
    uploading file...
    successfully uploaded file
    waiting for the sdk to finish processing
    getting upload status is successful Status:  PROCESSING
    getting upload status is successful Status:  SUCCEEDED
    getting session status is successful Status:  PENDING
    getting session status is successful Status:  PENDING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  PREPARING
    getting session status is successful Status:  RUNNING
    Remote access session started! Installing app...
    { appUpload:
       { arn: 'arn:aws:aatp:us-west-2:111122223333:upload:42ca0449-2714-4dd8-848f-ae9ef6655efb/cd200f8e-e7f7-4d18-a4ee-32ad959a0786',
         name: 'app-debug.apk',
         created: 2019-05-04T00:24:32.871Z,
         type: 'ANDROID_APP',
         status: 'SUCCEEDED',
         url: 'https://prod-us-west-2-uploads.s3-us-west-2.amazonaws.com/arn%3Aaws%3Adevicefarm%3Aus-west-2%3A111122223333%3Aproject%3A42ca0449-2714-4dd8-848f-ae9ef6655efb/uploads/arn%3Aaws%3Adevicefarm%3Aus-west-2%3A111122223333%3Aupload%3A42ca0449-2714-4dd8-848f-ae9ef6655efb/cd200f8e-e7f7-4d18-a4ee-32ad959a0786/app-debug.apk?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20190504T002655Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86400&X-Amz-Credential=AKIAJSORV74ENYFBITRQ%2F20190504%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=4630cef6030405b6997281c2477722d3da24eb04ed6aab8e91735d936e532807',
         metadata: '{"device_admin":false,"activity_name":"com.amazonaws.devicefarm.android.referenceapp.Activities.MainActivity","version_name":"1.0","screens":["small","normal","large","xlarge"],"error_type":null,"sdk_version":"10","package_name":"com.amazonaws.devicefarm.android.referenceapp","version_code":"1","native_code":[],"target_sdk_version":"22"}',
         category: 'PRIVATE' } }
    

    HTH

    James