How do you run RServe on AWS Lambda with NodeJS?

2019-09-14 07:35发布

问题:

While this question is quite open-ended, I'm generally trying to follow this excellent post here: https://aws.amazon.com/blogs/compute/analyzing-genomics-data-at-scale-using-r-aws-lambda-and-amazon-api-gateway/ which describes setting up R to run with python. I, on the other hand, am trying to get R to work with NodeJs.

I've packaged up my dependencies, deployed to Lambda, and can run simple Node scripts. However, I am having difficulty connecting to RServe from Node using the npm package Rio (https://www.npmjs.com/package/rio). RServe, on both my localhost and on Heroku, will accept the default connection of 127.0.0.1 and port 6331. No luck with AWS Lambda.

'use strict';

var rio = require('rio');
var Promise = require('bluebird');
var exec = require('child_process').exec;

var whenReady = new Promise(function(resolve){

    // require libraries and bootup RServe
    exec('Rscript init.R', function(error, stdout, stderr) {
        (function check() {
            // Attempt to connect to RServe through Rio using my 'up' test function
            rio.e({
                entrypoint: 'up',
                callback: function (err) {
                    console.log(err);
                    if (err) return setTimeout(check, 100);
                    // If no connection error, rserve is running
                    console.log("Rserve up");
                    resolve();
                }
            });
        })();
    });
});

exports.handler = function(event, context, callback) {

    whenReady.then(function () {
        // Call hello world
        rio.e({
            entrypoint: 'hello',
            data: {name:'Will'},
            callback: function(err, result){

                console.log("Error", err);
                callback(null, result);
            }
        });
    });
};

This ends with connection refused errors

2017-03-01T22:58:33.210Z 96f69baf-fed2-11e6-9164-e91b9773d645 { [Error: connect ECONNREFUSED 127.0.0.1:6311] code: 'ECONNREFUSED',
errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1',
port: 6311 }

Any ideas on how to fix this one? I'm hoping we don't need to get complicated: https://aws.amazon.com/blogs/aws/new-access-resources-in-a-vpc-from-your-lambda-functions/

Thank you in advance!

** Update **

init.R does the following

// Require some libraries
...
require('jsonlite');

up <- function () {
    toJSON(TRUE)
}

run.Rserve()

** Last Update **

Gave up and went to the python example as posted in the first link.

Will