According to this AWS documentation page covering authorizers for AWS API Gateway it is possible to define authorizer as lambda function returning a boolean value in isAuthorized response field to allow/deny the API request.
However after numerous attempts I can't understand how to define it in serverless.yml (or at least in AWS console)
I'm new to AWS and serverless framework. I've decided not to dive into IAM access policies or Cognito just yet, thus I'm trying to build a very simple lambda authorizer function that just yields a boolean value to allow/deny API access.
I have defined my functions section in serverless.yml as follows:
functions:
add_content:
handler: src.handler.add
module: src/handler
events:
- http:
path: /content
method: post
authorizer: customAuthorizer
customAuthorizer:
handler: src.authorizer.authorize
module: src/authorizer
The authorizer function is as simple as:
def authorize(event, context):
response = {"isAuthorized": True}
return response
However if I try to test it I see the following CloudWatch stacktrace:
Mon May 03 20:06:11 UTC 2021 : Endpoint request body after transformations: {"type":"TOKEN","methodArn":"arn:aws:execute-api:eu-central-1:238758647165:ww9wzq8hp8/ESTestInvoke-stage/GET/","authorizationToken":"123456789"}
Mon May 03 20:06:11 UTC 2021 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:238758647165:function:sls-playground-dev-customAuthorizer/invocations
Mon May 03 20:06:11 UTC 2021 : Authorizer result body before parsing: {"isAuthorized": true}
Mon May 03 20:06:11 UTC 2021 : Execution failed due to configuration error: Invalid JSON in response: Unrecognized field "isAuthorized" , not marked as ignorable
Mon May 03 20:06:11 UTC 2021 : AuthorizerConfigurationException
In addition to that:
Develop section as shown in documentation.response mode section.
There are two possible problems.
http event is in serverless). Output from an Amazon API Gateway Lambda authorizer shows what the output should look like, as well as what is required.event to httpApi: Announcing Support for AWS HTTP APIsEnable simple responses.
authorizer:
payloadVersion: 2.0
enableSimpleResponses: true
Handler should do a callback instead of returning the value.
def authorize(event, context, callback):
response = {"isAuthorized": True}
callback(null, response)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With