By using AWS re:Post, you agree to the Terms of Use
/Is it possible to throw custom error message from API Gateway Lambda Authorizer/

Is it possible to throw custom error message from API Gateway Lambda Authorizer

0

I have created an Lambda Authorizer for custom authorization in API Gateway. I need to throw custom error message like this.

{ errorCode: 'xyz_12#', errorType: 'Constraint error', errorMessage: 'Need permission to perform this action', statusCode: 401 }

Is it possible from lambda authorizer?

1 Answers
0

Yes. At the end of the example authorizer in the docs (https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html), there is way to output optional data. Updating with your values, it would be something like this:

    // Optional output with custom properties of the String, Number or Boolean type.
    authResponse.context = {
        "errorCode": "xyz_12#",
        "errorType": "Constraint error",
        "errorMessage": "Need permisssion to perform this action"
    };

In the body mapping template, you'd access these as follows:

{"errorCode":"$context.authorizer.context.errorCode", "errorType": "$context.authorizer.context.errorCode"}

The one caveat here is that you can't specify the HTTP error code. The authorizer has to generate a 'Deny' policy which then results in an HTTP 403.

More discussion is available here: https://stackoverflow.com/questions/47921803

answered a month ago

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