1

I'm writing a Lambda function that returns a response for Lambda Proxy Integration in Python. The API expects headers to be a dictionary.

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

This requires each header field to be unique, so there is no way to use multiple Set-Cookie's. I've already tried to convert the dictionary to a list of tuples

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": [ ("headerName": "headerValue"), ... ],
    "body": "..."
}

but API gateway complains Malformed Lambda proxy response.

Any idea how to set headers with the same name?

Fabricator
  • 12,722
  • 2
  • 27
  • 40

2 Answers2

2

It is currently not possible to send multiple cookies with lambda integration.

If you send multiple set-cookie, then it will take the last one. ok, such a junk implementation right.

Reference, How can I send multiple Set-Cookie headers from API Gateway using a proxied Lambda

Let us see other avaialable options,

Lambda@Edge:

Here is what I find working with Lambda@Edge,

You can create a lambda function for viewer response and modify the header to set cookies.

'use strict';

exports.handler = (event, context, callback) => {
   const response = event.Records[0].cf.response;
   const headers = response.headers;

   // send via a single header and split it into multiple set cookie here.
   headers['set-Cookie'] = 'cookie1';
   headers['Set-Cookie'] = 'cookie2';

   callback(null, response);
};

API Gateway Integration Request mapping:

Here is what I found and got working with integration request,

enter image description here

enter image description here

Hope it helps.

Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
  • Your Lambda@Edge example is incorrect and will result in an error. Specifically, it is obsolete. The data structures changed a few days before L@E became generally available. – Michael - sqlbot Oct 06 '17 at 03:21
  • @Michael-sqlbot. Thanks for spotting the error if any. http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html still has the same example as it is in our production. I would like to change as well if something got changed. Can you point to the right documentation examples? I will fix the answer too. – Kannaiyan Oct 06 '17 at 03:30
1

Lambda@Edge:

It is possible to set the headers this way:

response.headers['set-cookie'] = [
  { key: 'Set-Cookie', value: 'cookie1=value1' },
  { key: 'Set-Cookie', value: 'cookie2=value2' },
]

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-response

API Gateway:

Use multiValueHeaders:

response.multiValueHeaders = {
  "Set-Cookie": [
    'cookie1=value1',
    'cookie1=value1'
  ]
}

https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format

Hugodby
  • 1,124
  • 9
  • 24
  • I am getting error on using Lambda@Edge . Struggled a lot but was not able to resolve multiValueHeaders: { 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}] } – Shahzad Dec 18 '19 at 19:56