1

I am not sure whats wrong here but my lambda below in node js doesnt work

'use strict';
exports.handler = function (event, context) {
    try {

    var req = require('request');
    var headers = { 'User-Agent': 'Super Agent/0.0.1' };

    var options = {
        url: 'http://google.com',
        method: 'GET',
        headers: headers
    };

    req(options, function (error, response, body) {
        console.log(body);
    });

The above is loading the module properly but it doesn't get to the console.log(body) for some reason. Anyone have an idea?

SernOne
  • 105
  • 2
  • 9
  • Probably it is error? Try adding `if (!error && response.statusCode == 200)` and `else console.log(error)` to check. – Darren Christopher Mar 17 '17 at 04:14
  • seems working! can you paste your whole code for Lambda, where you have Callback and ... – Majid Mar 17 '17 at 05:09
  • I can't paste the whole thing. The issue is the console log here for the body doesn't return at all the code continues just fine just anything in that request function doesn't return anything in the console any ideas? – SernOne Mar 18 '17 at 05:24

1 Answers1

2

I am assuming that the code you posted is the code you wrote for lambda. AWS Lambda has specific entry point/function called handler which is runs when lambda is invoked. So every lambda function must have this method to qualify as lambda.

exports.handler = (event, context, callback) => {
}

Also external dependencies of lambda is not automatically pulled. Hence, we need to zip the entire code with dependencies and upload it to lambda for it to work in case we have external dependency.

Example

lambda.js

var req = require('request');
var headers = { 'User-Agent': 'Super Agent/0.0.1' };

// Configure the request
var options = {
    url: 'http://google.com',
    method: 'GET',
    headers: headers
};

exports.handler = (event, context, callback) => {
 req(options, function (error, response, body) {
    console.log(body);
    callback(error);
 });
}

Now let us consider your directory structure to be as

node_modules/
lambda.js
package.json

Now you need to zip this content and upload it to lambda in AWS. Next thing is to point the lambda.js as handler for the present lambda.

If everything is done correctly, you can see now the log in cloudwatch after the lambda is invoked.

References:

http://docs.aws.amazon.com/lambda/latest/dg/get-started-create-function.html http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

bibek shrestha
  • 448
  • 3
  • 9
  • Thank you very much for taking the time for this response my code does have the handler and i did structure the package as required here is the rest of the code [code] 'use strict'; exports.handler = function (event, context) { try { var req = require('request'); var headers = { 'User-Agent': 'Super Agent/0.0.1' }; var options = { url: 'http://google.com', method: 'GET', headers: headers }; req(options, function (error, response, body) { console.log(body); }); [/code] – SernOne Mar 22 '17 at 02:04