Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this HTTP request not working on AWS Lambda?

I'm getting started with AWS Lambda and I'm trying to request an external service from my handler function. According to this answer, HTTP requests should work just fine, and I haven't found any documentation that says otherwise. (In fact, people have posted code that use the Twilio API to send SMS.)

My handler code is:

var http = require('http');  exports.handler = function(event, context) {   console.log('start request to ' + event.url)   http.get(event.url, function(res) {     console.log("Got response: " + res.statusCode);   }).on('error', function(e) {     console.log("Got error: " + e.message);   });    console.log('end request to ' + event.url)   context.done(null); } 

and I see the following 4 lines in my CloudWatch logs:

2015-02-11 07:38:06 UTC START RequestId: eb19c89d-b1c0-11e4-bceb-d310b88d37e2 2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 start request to http://www.google.com 2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 end request to http://www.google.com 2015-02-11 07:38:06 UTC END RequestId: eb19c89d-b1c0-11e4-bceb-d310b88d37e2 

I'd expect another line in there:

2015-02-11 07:38:06 UTC eb19c89d-b1c0-11e4-bceb-d310b88d37e2 Got response: 302 

but that's missing. If I'm using the essential part without the handler wrapper in node on my local machine, the code works as expected.

The inputfile.txt I'm using is for the invoke-async call is this:

{    "url":"http://www.google.com" } 

It seems like the part of the handler code that does the request is skipped entirely. I started out with the request lib and fell back to using plain http to create a minimal example. I've also tried to request a URL of a service I control to check the logs and there's no requests coming in.

I'm totally stumped. Is there any reason Node and/or AWS Lambda would not execute the HTTP request?

like image 784
awendt Avatar asked Feb 11 '15 08:02

awendt


People also ask

Which TCP port traffic is blocked in AWS Lambda?

TCP port 25 traffic is also blocked as an anti-spam measure. Q: How do I create an AWS Lambda function using the Lambda console? If you are using Node.

How many requests Lambda can 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

Of course, I was misunderstanding the problem. As AWS themselves put it:

For those encountering nodejs for the first time in Lambda, a common error is forgetting that callbacks execute asynchronously and calling context.done() in the original handler when you really meant to wait for another callback (such as an S3.PUT operation) to complete, forcing the function to terminate with its work incomplete.

I was calling context.done way before any callbacks for the request fired, causing the termination of my function ahead of time.

The working code is this:

var http = require('http');  exports.handler = function(event, context) {   console.log('start request to ' + event.url)   http.get(event.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 ' + event.url); } 

Update: starting 2017 AWS has deprecated the old Nodejs 0.10 and only the newer 4.3 run-time is now available (old functions should be updated). This runtime introduced some changes to the handler function. The new handler has now 3 parameters.

function(event, context, callback) 

Although you will still find the succeed, done and fail on the context parameter, AWS suggest to use the callback function instead or null is returned by default.

callback(new Error('failure')) // to return error callback(null, 'success msg') // to return ok 

Complete documentation can be found at http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

like image 187
awendt Avatar answered Oct 21 '22 10:10

awendt