Dharam
Dharam

Reputation: 479

Invoke api running on localhost from aws lambda in nodejs

I am running a api on http://localhost:80/api/test, I want to invoke this api from lambda function but I am not sure what plugin or anything I should use to access same.The reason for for doing so is, I want to do testing lambda and api in development phase

Upvotes: 3

Views: 4636

Answers (1)

Anirudh
Anirudh

Reputation: 3358

I used https://ngrok.com/ to solve it.

Below is the command for localhost https urls. You can replace the port no 3000 with your port no.

ngrok http https://localhost:3000 -host-header="localhost:3000"

Below is my Lambda Function:

var https = require('https');

exports.testJob = (event, context, callback) => {
var params = {
    host: "90abcdef.ngrok.io",
    path: "/api/test"

};

var req = https.request(params, function(res) {
    let data = '';
    console.log('STATUS: ' + res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        data += chunk;
    });
    res.on('end', function() {
        console.log("DONE");
        console.log(JSON.parse(data));
        });
    });
    req.end();
};

Upvotes: 3

Related Questions