node.js api proxy error

Not applicable

All,

I am trying to invoke a simple Lambda function from my node.js app which is deployed as a Proxy with all the node dependencies installed.

However, when I try out the app in Apigee I get an error (pasted below). The error seems to come from the require('proxy-agent') node module. The same invocation works fine when called from my local machine. Any ideas what could be wrong? Here is the node.js app code as well -

var express = require('express');

var bodyParser = require('body-parser');

var app = express();

var AWS = require('aws-sdk');

var proxy = require('proxy-agent');


AWS.config.accessKeyId=‘xxxxx’;

AWS.config.secretAccessKey=‘xxxxx’;

AWS.config.region = 'us-east-1';

AWS.config.update({

  httpOptions: { agent: proxy('http://<proxy>:<port>’) },

  httpsOptions: { agent: proxy('http://<proxy>:<port>') }

});


var lambda = new AWS.Lambda();

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({

    extended: true

}));

app.disable('etag');


app.get('/', function(req, res) {

    var params = {

   FunctionName: 'apigeeLambdaTest',

   InvocationType : 'RequestResponse',

   Payload: JSON.stringify(

     {

       "body" : {

          "key":req.body.key

     }

    })};

lambda.invoke(params, function(err, data) {

  console.log("In lambda invoke...");

   if (err)

     {

      res.status(500);

      res.set('Content-Type', 'application/json');

      res.send(err);

     }

   else

    {

      res.status(200);

      res.set('Content-Type', 'application/json');

      res.send(data.Payload);

    }

  });

    

});  




app.listen(9000, function() {

    console.log('Node HTTP server is listening');

});



Error:

{"fault":{"faultstring":"Script node executed prematurely: illegal character\nillegal character\n    at module.js:439\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/pac-proxy-agent\/node_modules\/socks-proxy-agent\/index.js:8\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/pac-proxy-agent\/index.js:32\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/node_modules\/proxy-agent\/index.js:14\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:364\n    at require (module.js:380)\n    at \/organization\/environment\/api\/lambda_test.js:5\n    at module.js:456\n    at module.js:474\n    at module.js:356\n    at module.js:312\n    at module.js:497\n    at startup (trireme.js:142)\n    at trireme.js:923\n","detail":{"errorcode":"scripts.node.runtime.ScriptExitedError"}}}	
1 13 3,292
13 REPLIES 13

Not strictly an answer to your question, but have you checked out https://github.com/gsjurseth/callingLambdaFromProxy ?

So in my case, I am using the proxy-agent module and that had some back ticks (`). I tried fixing those but there are still complaints of illegal characters. It is related to the version of node that Apigee is running.

The version of node running in Apigee is way too old (v0.10.32). Does anyone know a way to upgrade the version of node in Apigee?

no. and yes. The current nodejs relies on Trireme, and the version of node is quite old. You cannot update it.

Apigee is currently in trial with a new capability that will allow you to run a current version of node. @Mukundha Madhavan or @Jeremy Whitlock may be able to tell you more.

or Maybe @Srinandan Sridhar

ok thanks @Dino.

It'll be good if we can upgrade node. Please let me know how.

@Dino @Srinandan Sridhar @Mukundha Madhavan @Jeremy Whitlock

- Is there a solution to this problem?

Problem:

Unable to call Lambda using 'proxy-agent' module in node.js because of an older version of Node running in Apigee. Code works fine when invoked using a newer version of Node from local machine.

Please find code that I am trying above in this post.


Hey @Geir Sjurseth - what do you think?

@Dino @Mukundha Madhavan @Jeremy Whitlock- I am not sure if this demo uses a later version of node, but this was kind of my starting point.

https://apigee.com/about/tags/lambda


I'm curious why you're using a proxy-agent at all. Is this an on-premises installation per chance?

Also, it seems the issue from the stack trace above is from the socks agent which is included in the proxy-agent .. but can't you just use http-proxy-agent directly or if not that one then perhaps another proxy agent. At worst you could just code your own to get around the issue.

Not applicable
@Geir Sjurseth

This is an on-premises installation. We need a proxy in order to invoke the Lambda function, otherwise it just hangs. I have tried it with and without the proxy and validated it.

As for your question on using http-proxy-agent, when I searched I found this in the AWS documentation and also in StackOverflow which is why I used proxy-agent module -

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-configuring-proxies.html

The problem like I explained above lies with the old version of node that Apigee runs (v0.10.32) that is not able to parse some of the characters in the socks agent and agent-base modules. I did try removing some of the illegal characters (`) but it still doesn't help.

I don't know how to code around getting past this issue. If you have some ideas please let me know and I'd be happy to try out. I did not want to reinvent the wheel when something existed already.

Right, but the proxy-agent if just using http[s]-proxy-agent behind the scenes. Can't you just change your require to use http-proxy-agent?

I can't promise you that it will work, but it looked right when I tried it locally running 0.10

It looks like you have an illegal character in your code. The code pasted above shows this. Instead of a ', you have a ’. Easy to miss but those are two different characters. It's this line:

  httpOptions: { agent: proxy('http://<proxy>:<port>’) },

@Jeremy Whitlock and @Geir Sjurseth

Thanks for your replies but we were able to successfully call Lambda without using the proxy-agent module. We used -

tunnel = require('tunnel')
var tunAgent = tunnel.httpsOverHttp({ proxy: { host: "<proxy_host>", port: <proxy_port> } })
AWS.config.update({httpOptions: { agent: tunAgent }})

Didn't have to use a127 as well, used "apigeetool deploynodeapp....." to deploy our node.js app, used environment variables for access key, secret access key and got a response back from Lambda!

Thanks to all that replied and offered suggestions.

What should be the host string to be provided?