Created a lambda authorizer for AWS HTTP API Gateway
- Payload Format: 2.0
- Response Mode: IAM Policy
- Identity sources: $request.header.Authorization
- Invoke Permission: Automatically grant API Gateway permission to invoke your Lambda function
And here is the lambda function which has API Gateway as trigger
exports.handler = function(event, context, callback) {
var token = event.authorizationToken;
switch (token) {
case 'allow':
callback(null, generatePolicy('user', 'Allow', event.methodArn));
break;
case 'deny':
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
case 'unauthorized':
callback("Unauthorized"); // Return a 401 Unauthorized response
break;
default:
callback("Error: Invalid token"); // Return a 500 Invalid token response
}
};
var generatePolicy = function(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
// Optional output with custom properties of the String, Number or Boolean type.
authResponse.context = {
"stringKey": "stringval",
"numberKey": 123,
"booleanKey": true
};
return authResponse;
}
I am getting response as follows:
{"message":"Internal Server Error"}
Here is the log from CloudWatch for API Gateway
{
"requestId":"TpdEqgAZhcwEJtg=",
"ip": "103.121.69.2",
"requestTime":"29/Sep/2020:21:35:00 +0000",
"httpMethod":"POST",
"routeKey":"POST /auth/otp",
"status":"500","protocol":"HTTP/1.1",
"responseLength":"35"
}
How I do I resolve this error 500?