Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send http request with nodejs AWS Lambda?

I'm using AWS Lambda to drive an Alexa Skill Kit development. In an attempt to track events, I'd like the script to send an HTTP request on launch, however from the cloud logs it appears as though the http.get function is being skipped during the execution process.

The code is shown below (google.com replaces the analytics tracking url - which has been tested in the browser);

exports.handler = function (event, context) {

    var skill = new WiseGuySkill();
    var http = require('http');

    var url = 'http://www.google.com';
    console.log('start request to ' + url)
    http.get(url, function(res) {
        console.log("Got response: " + res.statusCode);
        // context.succeed();
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
        // context.done(null, 'FAILURE');
    });
    console.log('end request to ' + url);

    skill.execute(event, context);
};

The context objects have been commented out to allow for 'skill.execute' to function, yet either way this HTTP request is not executing. Only the 'start' and 'end' console.logs are recorded, those internal in the function do not.

Is this a async issue? Thanks.

like image 272
user2075625 Avatar asked Oct 30 '22 16:10

user2075625


1 Answers

You need to make sure the handler is being triggered. There are two ways of accomplishing this:

  • You could set up a new API endpoint and execute a request on that.
  • You could hit the Test button and your function would be invoked with the given data.

I copied and pasted your whole snippet except for the first and the last lines (because I don't have customSkill defined anywhere). I was able to get a 200 response code.

like image 121
nick Avatar answered Nov 04 '22 07:11

nick