【问题标题】:AWS Serverless Template Configurations with Custom Authorization and Rest API具有自定义授权和 Rest API 的 AWS 无服务器模板配置
【发布时间】:2019-04-05 21:18:48
【问题描述】:

我是 AWS Lambda 服务的新手。我创建了一个无服务器 lambda 方法并成功将其部署在 AWS 云上。

接下来,我创建了一个 Lambda 自定义 Authorizer,并为 Lambda 方法和自定义 Authorizer 配置了 API Gateway。

因为,我需要公开许多其他无服务器的 lambda 方法,因此我决定将我的 lambda 方法移动到无服务器的 .Net API 项目中。我可以将此 api 项目部署到 AWS 云,然后手动设置授权方以使用我的自定义 Authorize lambda 方法。

困难的部分是,我想通过 serverless.template 文件配置所有这些东西。

我正在努力为我的自定义授权方法获取 RESTAPIID,以及如何使用 serverless.template 文件为我的 lambda 函数设置授权。下面是我做过的配置。 另外,如何获取AuthorizerUri

我不想硬编码任何东西。

    "Resources" : {
**//How I can create this serverless function to use my custom authorizer?**
    "Create" : {
      "Type" : "AWS::Serverless::Function",      
      "Properties": {
        "Handler": "Osn.Ott.Telco.Connector.UI.Web.Controllers.V10::Osn.Ott.Telco.Connector.UI.Web.Controllers.V10.SubscriptionController::Create",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "FunctionName" : "CreateCustomer",
        "Policies": [ "AWSLambdaBasicExecutionRole" ],
        "Events": {
          "PutResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/create",
              "Method": "POST"
            }            
          }
        }
      }
    },
    "CustomAuthorizer" : {
        "Type" : "AWS::ApiGateway::Authorizer",
        "Properties" : {
            "AuthorizerUri" : {"Fn::GetAtt" : [ "Create", "Arn"]},
            "IdentitySource" : "method.request.header.Authorization,method.request.context.resourcePath, method.request.context.path",
            "Name"           : "CustomAuthorizer",
            "Type"           : "REQUEST",
**//How I can get this id?**
            "RestApiId" : {"Fn::GetAtt" : [ "ServerlessRespApi", ""]}
        }
    }
}

【问题讨论】:

    标签: amazon-web-services asp.net-core aws-lambda aws-api-gateway serverless-framework


    【解决方案1】:

    AWS 上周宣布支持AWS Serverless Application Model Supports Amazon API Gateway Authorizers(以前也可以这样做,但必须在 SAM 模板中使用内联 Swagger)。

    上面的页面链接了一些 GitHub 示例,我猜Lambda Request Authorizer 最接近您的问题。下面的代码是从template.yaml 复制而来的。另请参阅 AWS SAM 规范的 API Auth Object 部分。

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Description: API Gateway with Lambda Token Authorizer
    Resources:
      MyApi:
        Type: AWS::Serverless::Api
        Properties:
          StageName: Prod
          Auth:
            DefaultAuthorizer: MyLambdaRequestAuthorizer
            Authorizers:
              MyLambdaRequestAuthorizer:
                FunctionPayloadType: REQUEST
                FunctionArn: !GetAtt MyAuthFunction.Arn
                # FunctionInvokeRole: !Ref MyRole
                Identity:
                  QueryStrings:
                    - auth
                  # NOTE: Additional options:
                  # Headers:
                  #   - Authorization
                  # StageVariables:
                  #   - AUTHORIZATION
                  # Context:
                  #   - authorization
                  # ReauthorizeEvery: 100 # seconds
    
      MyFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ./src
          Handler: index.handler
          Runtime: nodejs8.10
          Events:
            GetRoot:
              Type: Api
              Properties:
                RestApiId: !Ref MyApi
                Path: /
                Method: get
                Auth:
                  Authorizer: NONE
            GetUsers:
              Type: Api
              Properties:
                RestApiId: !Ref MyApi
                Path: /users
                Method: get
    
      MyAuthFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ./src
          Handler: authorizer.handler
          Runtime: nodejs8.10
    
    Outputs:
      ApiURL:
        Description: "API URL"
        Value: !Sub 'https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'
    

    【讨论】:

    • 在我的情况下,auth 不是 MyApi: 部分中的有效密钥。
    • @muhammadkashif 在 AWS SAM 中对 API 网关授权方的支持仅在 10 天前为 announced,但应该在所有提供 AWS Lambda 的区域都可用(根据公告)。您尚未指定Auth 无效的方式。我的猜测是,您使用了一个在部署 CloudFormation 模板之前验证它的工具,该工具尚未添加对 Auth 对象的支持。
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2019-12-15
    • 2021-03-13
    • 2022-07-30
    • 2020-06-30
    • 2019-05-01
    • 2020-05-28
    • 2018-03-08
    相关资源
    最近更新 更多