0
votes

I'm looking for the full documentation of AWS Lambda's handler function for NodeJS, and especially the result object. How can I set headers, cookies... what other options do I have, etc.?

Only the callback prototype seems to be documented, and not its arguments. http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-callback

AWS documentation is such a mess...

1
Your question seems to be about API Gateway's interpretation of the Lambda callback arguments. From Lambda's perspective, the correct answer is "any object compatible with JSON.stringify." - Michael - sqlbot
Indeed, I'm using API Gateway. So how lambda responses are transformed into plain HTTP response? - vcarel
That's what @SteveE. was attempting to illustrate, below. API Gateway has concepts of "Integration Response" and "Method Response" that will make you want to pull out all your hair until it dawns on you and you start to figure out what they were thinking, and then it starts to make sense. Most of the behavior is not so much from specific callback structures but rather is constructed from elements you pluck from the callback object contents, to build the desired response. Either that, or I'm doing it wrong. I agree, this part of the docs is murky. I'll see if I can pin down something. - Michael - sqlbot
There's more information in this guide : docs.aws.amazon.com/apigateway/latest/developerguide/… This is still not a reference, but maybe AWS doesn't have that anyway... - vcarel

1 Answers

0
votes

This blog post explains it:

The sample code is this:

'use strict';
exports.handler = function(event, context) {
  console.log("{'Cookie':event['Cookie']}");
  var date = new Date();

  // Get Unix milliseconds at current time plus 365 days
  date.setTime(+ date + (365 \* 86400000)); //24 \* 60 \* 60 \* 100

  // Generate a random cookie string
  var cookieVal = Math.random().toString(36).substring(7); 

  var cookieString = "myCookie="+cookieVal+"; domain=my.domain; 
      expires="+date.toGMTString()+";";

  context.done(null, {"Cookie": cookieString}); 

};

If using callbacks, then the response needs to be along the lines of:

callback(null, {
    "statusCode": 200,
    "headers": { "Set-Cookie": myCookie=cookieVal; domain=my.domain; 
        expires="+date.toGMTString()+"; },
    "body": "..."
});

But you also need to update the APIG to allow the cookie response as per the blog post:

In the API Gateway console, go to the GET Method page and choose Method Response. Expand the default 200 HTTP status, and choose Add Header. Add a new header called “Set-Cookie.”

On the GET Method page, choose Integration Response. Under the Header Mappings section of the default 200 HTTP status, choose the pencil icon to edit the “Set-Cookie” header. In the mapping value section, put:

integration.response.body.Cookie

Make sure to save the header by choosing the check icon!

APIG currently has a restriction that each header can only exist once in the response. This means that only one cookie can be set at a time.