How do I create an AWS Lambda function to process custom headers of messages passed through an API created in the Amazon API Gateway?

By default, a Lambda function only ingests the request body received by an API created in the API Gateway.

To pass custom headers from an Amazon API Gateway API to a Lambda function, use the curl utility to send a message to the API Gateway using a body mapping template to extract any custom headers added to the message.

The API sends the updated message to a Lambda function to process the headers, and returns one or more header values from the original message.

Create the Lambda function

Before you begin, be sure that you've configured the necessary permissions for Lambda and the Amazon API Gateway as described at Authentication and Access Control for AWS Lambda and Set IAM Permissions to Access API Gateway. Create a test role and attach the AmazonAPIGatewayInvokeFullAccess and AmazonAPIGatewayPushToCloudWatchLogs managed policies.

1.    Open the Lambda console, and then choose Create function.

2.    Choose Author from scratch. For Name, enter a meaningful name. For Runtime, choose Node.js 6.10. For Role, select the name of the test role that you created earlier.

3.    Choose Create function.

4.    For Function code, replace the code in index.js with the following:

exports.handler = (event, context, callback) => {
// TODO implement
callback(null, "This message header was processed by Amazon " +event.headers["header1"]);

5.    Choose Save.

Create the API Gateway API

1.    In the API Gateway console, choose Create API.

2.    Select New API. For API name, enter SendtoLambda, and then choose Create API.

3.    Select Resources in the navigation pane, open the Actions menu, and then choose Create Resource.

4.    For Resource Name, enter HeadersResource, and then choose Create Resource.

5.    Open Actions, and then choose Create Method.

6.    From the drop-down menu under /headersresource, choose POST, and then choose (click) the check mark.

7.    Configure your integration point:
For Integration type, choose Lambda Function.
For Lambda Region, choose your Region.
For Lambda Function, enter CustomHeaders.

8.    Choose Save.

9.    Select Integration Request.

10.   Expand Body Mapping Templates, and for Request body passthrough, choose When there are no templates defined (recommended).

11.   Choose Add mapping template.

12.   For Content-Type, enter application/json, and then choose (click) the check mark.

13.   Add the following to the input section:

    "method": "$context.httpMethod",
    "body" : $input.json('$'),
    "headers": {
        #foreach($param in $input.params().header.keySet())
        "$param": "$util.escapeJavaScript($input.params().header.get($param))"

14.   Choose Save.

15.   From the Actions menu, choose Deploy API.

16.   For Deployment stage, choose [New Stage], and then enter a Stage name (for example, "prod", "test", or "beta").

17.   Choose Deploy. Note the URL displayed next to Invoke URL at the top of the Stage Editor.

18.   Use the curl utility to send a message to the API Gateway. This example returns the value of the header1 header referenced by the Lambda function.

curl -H "Content-Type: application/json" -H "header1: API Gateway and AWS Lambda" -X POST -d "{\"API_body\": \"This is the body\"}" https://<api-id>.execute-api.<region><stage>/headersresource

If this process is successful, you'll see the following output:

"This message header was processed by Amazon API Gateway and AWS Lambda"

Note: To install curl on Windows, consider downloading Git for Windows, which includes the GNU bash shell and other Linux tools, such as curl, tail, cat, grep, gzip, and Perl.

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-10-06

Updated: 2018-07-31