Vivekh
Vivekh

Reputation: 4259

return custom message from API gateway lambda Authorizer

I have a API gateway lambda Authorizer and when it fails it all i am getting is

{
   "Message": "User is not authorized to access this resource with an explicit deny"
}

and the status code is 403

My lambda is written in dotnet core. The thing is i am doing something if my authentication failed and the status code should be 401 for that to happen. So just wanted to know if we have a way to customize this behavior

Upvotes: 1

Views: 1471

Answers (2)

YoavBZ
YoavBZ

Reputation: 198

You can configure your Access Denied response in the Gateway Responses section of your API Gateway. Both response template and status code can be configured there.

If you want the response to include custom data, you can add it to the context map returned from your Lambda Authorizer (see Output from an Amazon API Gateway Lambda authorizer for more info).

This way you can customize your Access Denied response template, for example:

{
  "message": "$context.authorizer.error_message"
}

Obviously you can include multiple properties and return more complex responses.

Upvotes: 3

ErikMuir
ErikMuir

Reputation: 181

According to this AWS documentation, that's the exact error message you will see when the Lambda authorizer function returns an IAM policy document with an explicit deny.

Upvotes: -1

Related Questions