Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access custom header from AWS Lambda Authorizer?

I have created an Authorizer in AWS API Gateway. This Authorizer refers to a Lambda Function.

I am passing the following values in header, to the API Endpoint using Postman.

 {   
 "type":"TOKEN",  
 "authorizationToken": "testing2",   
  "methodArn": "arn:aws:execute-api:us-west-2:444456789012:ymy8tbxw7b/*/GET/" 
 }

The above header values are received in the Lambda Function. I can see this through the logs in CloudWatch.

I want to pass additional value 'clientID' in the header. So I pass the following values in the header from postman.

{
  "type":"TOKEN",
  "authorizationToken": "testing2",
  "methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/",
  "clientID" : "1000"
}

In this case, the Lambda function does not get the clientID. I checked various threads in SO, and understood that this can be achieved mapping header. So I did the following.

In the "Method Execution" section of the API method, I created a new header clientID. In the "Integration Request" section, under "HTTP Headers" section I provided the following value

Name: clientID Mapped from: method.request.header.clientID

After doing the above, I deployed the API and tried to call the method from Postman, but the clientID is shown undefined. Following is the code that I have written in Lambda Function

exports.handler = function(event, context, callback) {


  var clientid = event.clientID;

//I always get event.clientID undefined 
 console.log("The client ID is:" + event.clientID);

}

EDIT

Following is the error from the CloudWatch Log.

START RequestId: 274c6574-dea5-4009-b777-a929f84b9a9d Version: $LATEST
2019-09-19T09:40:25.944Z 274c6574-dea5-4009-b777-a929f84b9a9d INFO The client ID is:undefined
2019-09-19T09:40:25.968Z 274c6574-dea5-4009-b777-a929f84b9a9d ERROR Invoke Error
{
    "errorType": "Error",
    "errorMessage": "Unauthorized",
    "stack": [
        "Error: Unauthorized",
        "    at _homogeneousError (/var/runtime/CallbackContext.js:13:12)",
        "    at postError (/var/runtime/CallbackContext.js:30:51)",
        "    at callback (/var/runtime/CallbackContext.js:42:7)",
        "    at /var/runtime/CallbackContext.js:105:16",
        "    at Runtime.exports.handler (/var/task/index.js:40:4)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)",
        "    at process._tickCallback (internal/process/next_tick.js:68:7)"
    ]
}
like image 762
KurioZ7 Avatar asked Jun 27 '26 15:06

KurioZ7


1 Answers

I have understood why I was not getting the value in the header. I have done the following

1) Instead of type TOKEN, I used type REQUEST in the header. I understood this by reading the following link. This link also contains code for Request type.

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

2) I removed all the mapping from Method Request and Integration Request.

3) Deployed the API.

like image 52
KurioZ7 Avatar answered Jun 30 '26 17:06

KurioZ7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!