0

Is there a way to use dynamic endpoints with AWS Lambda?

From what I can tell you need to specific the endpoint in the AWS Lamda Console.

What I need to do is access the URL from database then get the JSON for that URL. The URL will be added contantly by users so I cant possible log in every second to manually add an endpoint.

Considering that I set up my Lambda to Node.JS

I thought that I could just use:

    // ----receive function----v
function get_json(url, callback) {
    http.get(url, function(res) {
        var body = '';
        res.on('data', function(chunk) {
            body += chunk;
        });

        res.on('end', function() {
            var response = JSON.parse(body);
// call function ----v
            callback(response);
        });
    });
}

         // -----------the url---v         ------------the callback---v
var mydata = get_json("http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a", function (resp) {
    console.log(resp);
});

but I get this error:

"errorMessage": "http is not defined"

What I need is a way to ue a dynamic URL for the JSON

Can someone help?

JamesG
  • 1,552
  • 8
  • 39
  • 86
  • What makes you think you can't connect to the database from within your Lambda function to retrieve the URL you need to hit? – Mark B Mar 21 '16 at 02:55
  • http://stackoverflow.com/questions/31809890/can-aws-lambda-connect-to-rds-mysql-database-and-update-the-database – Mark B Mar 21 '16 at 02:55
  • You have not understood my question – JamesG Mar 21 '16 at 03:07
  • Well it is difficult to understand as written. Perhaps stick to a single question and ask it clearly. – Mark B Mar 21 '16 at 03:19
  • There is only one question on there. Unless pedantic and count the ? - but the second refers to the first. However it no longer matters as my problem was a simple mistake in invocation. I apologise that it was unclear (my head is a little frazzled from looking through pages and pages of docs) and I do thank you for your help :) P.S Will endeavour to write clearer answers :) – JamesG Mar 21 '16 at 03:58

1 Answers1

1

You need to define var http = require('http'); before using http.get.

    var http = require('http');

    function get_json(url, callback) {
        http.get(url, function(res) {
            var body = '';
            res.on('data', function(chunk) {
                body += chunk;
            });

            res.on('end', function() {
                var response = JSON.parse(body);
    // call function ----v
                callback(response);
            });
        });
    }

             // -----------the url---v         ------------the callback---v
    var mydata = get_json("http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a", function (resp) {
        console.log(resp);
    });
NSR
  • 819
  • 7
  • 20