如何使用无服务器来配置http集成而不是lambda集成?

问题描述 投票:0回答:1

Generalities

我在Lambda身份验证器中工作。我有几个模块,并且需要一个唯一的身份验证器。这意味着客户端发送用户和密码,而自定义身份验证器返回JWT。然后调用另一个端点,验证JWT并将请求转发到最终端点。

什么起作用JWT一代还可以,我有一个用于lambda的自定义身份验证器来验证JWT。

问题我正在使用无服务器框架,并且找不到如何实现http集成。如果使用控制台,则只需选择我的自定义身份验证器,然后从集成selecting http integration from console中选择http,即可完成。接收到该请求,使用自定义身份验证器对其进行验证,并将其转发到外部端点。

如何在无服务器环境中执行此操作?我有此配置。

functions:
hello:
   handler: handler.hello
   events:
     - http:
         path: hello
         method: get
         authorizer: authorize

显然,它没有达到我的期望。自定义身份验证器完成后,将调用lambda handler.hello。我不想在lambda内调用外部端点,因为它每次都会花费。我只想使用http集成作为控制台中的集成来调用另一个端点。这可能吗?

amazon-web-services http aws-lambda serverless
1个回答
0
投票

要在Serverless Framework中使用授权创建到外部http端点的API网关代理,您需要定义api,授权者和资源方法。

这实际上分解为几个CloudFormation动作,但是您不必为此担心。但是请注意,如果部署失败,则可能仍会存在部分堆栈。

此示例假定您已经部署了lambda授权器。我确定有一种创建它并在同一serverless.yml中引用ARN的方法。我还添加了资源,因此要调用此服务,您将调用GET {api-url} / {stage} / test。

示例:

service: service-name
provider: aws

resources:
  Resources:
    ApiGatewayRestApi:           # create api gateway
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: TestAuthorizer
    ApiGatewayAuthorizer:           # assign lambda to authorizer
      Type: AWS::ApiGateway::Authorizer
      Properties:  
        IdentitySource: method.request.header.Authorization
        RestApiId: 
          Ref: ApiGatewayRestApi
        Type: TOKEN
        Name: customAuthorizer
        AuthorizerUri:
          Fn::Join: 
            - ""
            - - "arn:aws:apigateway"
              - ":us-east-1"          # or wherever you are 
              - ":lambda:path/"
              - "2015-03-31/functions/"
              - "arn:aws:lambda:us-east-1:111111111111:function:myAuthorizerlambda"     # arn of lambda
              - "/invocations"
    ExternalResource:           # create resource on API
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi # our default Rest API logical ID
            - RootResourceId
        PathPart: test           # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ApiGatewayRestApi
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId:
          Ref: ExternalResource
        RestApiId:
          Ref: ApiGatewayRestApi
        HttpMethod: GET           # or what ever the method of your proxy is
        MethodResponses:
          - StatusCode: 200
        Integration:
          IntegrationHttpMethod: POST
          Type: HTTP
          Uri: http://test.com           # the URL you want to set a proxy to
          IntegrationResponses:
            - StatusCode: 200
        AuthorizationType: Custom
        AuthorizerId: 
          Ref: ApiGatewayAuthorizer 
© www.soinside.com 2019 - 2024. All rights reserved.