How can I return a custom error message in the body through a lambda authorizer?

0

I want my lambda authorizer to return custom messages when the requests are denied (403s) instead of the default one. The only way I found to do this is with the gateway responses of the api gateway itself, either in headers or the body, the problem is that the api gateway I'm working on has many services and that would override all the 403 default errors of all the services. My question is: Is there a way to "select" specific requests to use this gateway responses template or is there another way entirely to approach this? The way I'm doing it is by returning a context object along side the policyDocument in the authorizer and then catching it calling something like $context.authorizer.customMessage

1 Answer
0

Hi, yes, it is possible. See this previous answer on exact same topic: https://repost.aws/questions/QUgItZ7VFcS1iQ5wKGeU4YqA/is-it-possible-to-throw-custom-error-message-from-api-gateway-lambda-authorizer#ANpVh0TCRnSsafEkHDhfHuow

To "select" the queries on which you want to provide your specific message, you can use the input parameters to the authorisers as per doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

A request parameter-based Lambda authorizer (also called a REQUEST authorizer) receives 
the caller's identity in a combination of headers, query string parameters, 
stageVariables, and $context variables.

In your case, the query string parameters and headers will help you select your specific queries.

Best,

Didier

profile pictureAWS
EXPERT
answered 10 months ago
  • Yes, that I have already figured out, as I mentioned in the post. My question is: How can I use this template just for specific resources and not the whole api gw

  • Hi, I edited my answer to add complements in the direction of your question. Hope it's better now!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions