0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lambda Authorizerでカスタムエラーメッセージを出力する

Posted at

Lambda Authorizerでカスタムのエラーメッセージを出力したい時。

Lambda Proxy統合の場合ステータスコードが変更できないのでDenyポリシーを返す403限定ではありますが、context変数にエラーメッセージ等を格納し、ゲートウェイレスポンスのレスポンステンプレートを編集することで可能です。

context変数はこちらのドキュメントにそって定義します。
Amazon API Gateway Lambda オーソライザーからの出力 - Amazon API Gateway

レスポンステンプレートはCloudFormation的にはこんな感じで定義します。
この例でいうと、context.messagecontext.codeをLambdaの方で定義します。

  GatewayResponse:
    Type: "AWS::ApiGateway::GatewayResponse"
    Properties:
      ResponseTemplates:
        application/json: >
          {"message":"$context.authorizer.message", "code":"$context.authorizer.code"}
      ResponseType: DEFAULT_4XX
      RestApiId: !Ref ResourceApi
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?