2
votes

I am trying to invoke an AWS Lambda function (simple Hello World) from node.js using aws-sdk. Here is my code:

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

var AWS = require('aws-sdk');
AWS.config.accessKeyId=‘xxxxxxxx’;
AWS.config.secretAccessKey=‘xxxxxxxx’;
AWS.config.region = 'us-east-1';

var lambda = new AWS.Lambda();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));


app.get('/', function(req, res) {
var params = {
FunctionName: ‘myLambdaFunction’,
InvocationType : 'Event',
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');
});

For some reason the code hangs at the lambda.invoke method and doesn't even print the "In lambda invoke".

Things that I have tried:

1) Run aws lambda from the command line like this:

#!/bin/bash
aws lambda invoke \
--invocation-type RequestResponse \
--function-name myLambdaFunction \
--region us-east-1 \
--log-type Tail \
outputfile.txt

The above code works fine and returns "Hello World".

2) Set/Unset corporate proxy

3) Tried similar code in Java using the Java AWS SDK. Same problem, just hangs at the invoke method and times out with a Socket connection error.

4) Changed InvocationType to RequestResponse and Event both with similar results.

Anyone have any idea why I am not able to execute the node or java code?

TIA

Update:

After setting the proxy in the code I was able to run it from my local machine and get a response from the Lambda.

However, when I try to invoke the same code from Apigee (as a node proxy) I am getting an error like this -

{"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"}}}

So it looks like there is some illegal character in one of the proxy-agent modules. But I am not sure why it works fine from my local machine. Is it because of node versions?

1
You have a network issue you need to resolve. Wherever you are running the code, it doesn't have access to the AWS API. - Mark B
doesn't even print the "In lambda invoke". That's not an accurate label for that step in the code. You'd be inside the response callback after lambda invoke here -- execution reaches this point only after the Lambda API has returned a response or an exception has been thrown, not while the request is being attempted or processed because the callback function does not execute until something (response or error) is available. @MarkB seems correct. - Michael - sqlbot

1 Answers

2
votes

2) Set/Unset corporate proxy

If you're behind a proxy you will need to configure your aws client correctly for this:

nodejs:

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

AWS.config.update({
  httpOptions: { agent: proxy('http://{your_proxy_goes_here}') }
});

For java, have a look here.