Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Requests in an AWS Lambda

Tags:

I'm new to Lambdas so perhaps there is something I've not caught on to just yet, but I've written a simple Lambda function to do an HTTP request to an external site. For some reason, whether I use Node's http or https modules, I get an ECONNREFUSED.

Here's my Lambda:

var http = require('http');  exports.handler = function (event, context) {     http.get('www.google.com', function (result) {         console.log('Success, with: ' + result.statusCode);         context.done(null);     }).on('error', function (err) {         console.log('Error, with: ' + err.message);         context.done("Failed");     }); }; 

Here's the log output:

START RequestId: request hash 2015-08-04T14:57:56.744Z    request hash                Error, with: connect ECONNREFUSED 2015-08-04T14:57:56.744Z    request hash                {"errorMessage":"Failed"} END RequestId: request hash 

Is there a role permission I need to have to do HTTP requests? Does Lambda even permit plain old HTTP requests? Are there special headers I need to set?

Any guidance is appreciated.

like image 260
kevin628 Avatar asked Aug 04 '15 15:08

kevin628


People also ask

Does AWS Lambda have requests?

Update (April 26, 2022): The version of the AWS SDK included in the AWS Lambda runtimes for Python 2.7, Python 3.6 and Python 3.7 will continue to include the 'requests' module in Botocore.

How many requests can a single Lambda handle?

With increased concurrent execution limit, there is still one more limit the Burst Concurrency limit. This will limit lambda to serve only 3000 concurrent request at time. If it receives more than 3000 concurrent requests some of them will be throttled until lambda scales by 500 per minute.


1 Answers

I solved my own problem.

Apparently, if you decide to feed the URL in as the first parameter to .get(), you must include the http:// up front of the URL, e.g., http://www.google.com.

var http = require('http');  exports.handler = function (event, context) {   http.get('http://www.google.com', function (result) {     console.log('Success, with: ' + result.statusCode);     context.done(null);   }).on('error', function (err) {     console.log('Error, with: ' + err.message);     context.done("Failed");   }); }; 

Alternatively, you can specify the first parameter as a hash of options, where hostname can be the simple form of the URL. Example:

var http = require('http');  exports.handler = function (event, context) {   var getConfig = {     hostname: 'www.google.com'   };   http.get(getConfig, function (result) {     console.log('Success, with: ' + result.statusCode);     context.done(null);   }).on('error', function (err) {     console.log('Error, with: ' + err.message);     context.done("Failed");   }); }; 
like image 61
kevin628 Avatar answered Oct 06 '22 04:10

kevin628