loading...

Http Request with AWS Lambda, Node.js 8.10, and Standard http library

scottlepp profile image Scott Lepper ・1 min read

In my previous article I showed an approach to extend a "traditional" (monolithic architecture) app using AWS Lambda: https://dev.to/scottlepp/extending-traditional-software-with-serverless-microservices-442m

Let's take a closer look at the Lambda function and how to make an http request using Node.js 8.10 and the standard http library.

In previous versions of node.js, the handler function contained a callback argument like so:

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

And when your http request finished you would execute the callback to indicate the function was finished:

const req = http.request(options, (res) => {
   callback('Success');
});

And yet even older version of node.js didn't have a callback function, instead you would use "context.succeed" like so:

const req = http.request(options, (res) => {
   context.succeed();
});

However, in node.js 8.10 this has changed. The callback argument is again not needed. Now you just wrap your function returning a Promise. Then instead of executing the callback function, you execute the Promise resolve function (or reject function if it fails) as so:

const http = require('http');

exports.handler = async (event, context) => {

    return new Promise((resolve, reject) => {
        const options = {
            host: 'ec2-18-191-89-162.us-east-2.compute.amazonaws.com',
            path: '/api/repos/r1639420d605/index?delta=true&clear=false',
            port: 8000,
            method: 'PUT'
        };

        const req = http.request(options, (res) => {
          resolve('Success');
        });

        req.on('error', (e) => {
          reject(e.message);
        });

        // send the request
        req.write('');
        req.end();
    });
};

That's it! These changes between versions of node.js tripped me up a bit so I wanted to share the latest method. Hope this helps someone!

Posted on Jun 10 '18 by:

Discussion

markdown guide
 

Hi Scott,

I'm using the request-promise library but when try insert within exports.handler this aren't work... this is part of code, your support please, I'm novice yet:

How can implement this requests into lambda function?.... in my local test this work!

Or maybe should reemplace the requet-promise reference per a simple "http.request"?? Opinions please?

Thanks very much for advance.....

Regards

 

Hi Guys,

Finally, as I not know much about "request-promise" module, use "request-promise-native" instead. I have this model...

You already have an array with the results and the code looks synchronous.

I hope that is model can use for others verbs like post and patch. I'm in testing yet. Will I do well?

 

Awesome!
thank you, this is exactly what I needed.

 
 
 
 

In case of Lambda project, what urls we put in host and path parameters ?

 

In this case I was running Voyager Search on an ec2 instance. The host was the ec2 instance and the path was to a REST call that Voyager Search supports, but you could take this approach with any REST provider.