Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Lambda output to API Gateway header

I'm trying to map the JSON output from a Lambda function to the header of an API Gateway call. As an example here, we can consider a redirect scenario. So I'd like to add a Location header to the response from my API Gateway call.

JSON output from Lambda function:

{
    Location: 'http://google.com'
}

In "Method Response"->"Response Headers" I can add the Location header to the desired HTTP status code.

Then in "Integration Response"->"Header Mappings" I can add the mapping for the Location header to be something.

Now my question is what should this something be?

Note: I've tried integration.response.header.Location, or using "Mapping Templates" but no luck so far.

There's a chance that it might not be possible at the moment (https://forums.aws.amazon.com/thread.jspa?messageID=651482), but since API Gateway/Lambda are evolving so quickly at the moment I'll ask anyway.

like image 226
Tobias Christensen Avatar asked Jul 30 '15 12:07

Tobias Christensen


People also ask

How do you pass headers to a Lambda function from API gateway?

To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.

Does API gateway pass authorization header to Lambda?

For a Lambda authorizer of the REQUEST type, API Gateway passes request parameters to the authorizer Lambda function as part of the event object. The request parameters include headers, path parameters, query string parameters, stage variables, and some of request context variables.

Can Lambda call API gateway?

And yes, you can call this API (Lambda proxy) as any Rest API.


2 Answers

As of today (January 2016), it works by simply specifying integration.response.body.Location in your case.

like image 198
MrTJ Avatar answered Oct 16 '22 11:10

MrTJ


Today (12th, November, 2015), I was looking for how to pass the Location header from my backend API to a Location header in integration response.

This configuration worked and I could get Location header from backend api:

enter image description here

Looks like the solution proposed by Tobias is possible currently. However, the Location header points to the backend API and this is not what we want. I'll try to find a solution for this and post it here.

like image 24
Juan Carlos González Avatar answered Oct 16 '22 11:10

Juan Carlos González