【问题标题】:How to invoke the lambda behind an API Gateway authorizer如何在 API Gateway 授权方后面调用 lambda
【发布时间】:2019-03-19 22:45:59
【问题描述】:

我有以下 CloudFormation 堆栈。 2 个 lambdas(Greeting 和 Auth),API 网关配置为使用 Auth lambda 进行授权。

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
  GreetingsApiGateway:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        DefaultAuthorizer: MyAuthorizer
        Authorizers:
          MyAuthorizer:
            FunctionArn: !GetAtt AuthLambda.Arn

  GreetingsLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: "s3://<bucket-name>/code.zip"
      Handler: src/index.greeting
      Events:
        GetRoot:
          Type: Api
          Properties:
            RestApiId: !Ref GreetingsApiGateway
            Path: /hello
            Method: get

  AuthLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: "s3://<bucket-name>/code.zip"
      Handler: src/index.auth

Globals:
  Function:
    Runtime: nodejs8.10
  # check whether I can use resources within globals

Outputs:
  ApiURL:
    Description: "OUR API URL"
    Value: !Sub "https://${GreetingsApiGateway}.execute-api.${AWS::Region}.amazonaws.com/Prod/"

我的 lambdas 代码如下:

exports.greeting = async () => ({ statusCode: 200, body: "Hello Beautiful World!" });

exports.auth = async () => ({ statusCode: 200, body: "I wanna do the authorisation!" });

我预计授权会一直失败,但也会在我的 CloudWatch 日志中看到来自 Auth lambda 的日志。

发生了什么:

从 API 网关内调用

如果我在 API Gateway 控制台中不带任何标题地进行测试,我会得到“Hello Beautiful World”结果。我的 CloudWatch 模板中没有 lambda 身份验证日志。

从 POSTMAN 调用

我尝试在 GET 请求的标头中发送此输入。在 Auth lambda CloudWatch 日志中没有任何日志的情况下,响应是“未经授权的”。

{
    "type":"TOKEN",
    "authorizationToken":"<caller-supplied-token>",
    "methodArn":"arn:aws:execute-api:<regionId>:<accountId>:<apiId>/<stage>/<method>/<resourcePath>"
}

发生了什么?

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda authorization


    【解决方案1】:

    我们需要使用 Authorization 标头来检索响应,但仍然不明白为什么来自 API Gateway 控制台的请求检索到了响应。

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 2017-04-13
      • 2018-01-15
      • 1970-01-01
      • 2017-02-15
      • 2018-01-28
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多