Amazon web services 具有自定义授权和Rest API的AWS无服务器模板配置

Amazon web services 具有自定义授权和Rest API的AWS无服务器模板配置,amazon-web-services,asp.net-core,aws-lambda,aws-api-gateway,serverless-framework,Amazon Web Services,Asp.net Core,Aws Lambda,Aws Api Gateway,Serverless Framework,我是AWS Lambda服务的新手。我已经创建了一个无服务器lambda方法,并成功地将其部署到AWS云上 接下来,我创建了一个Lambda自定义授权器,并为Lambda方法和自定义授权器配置了API网关 因为,我需要公开许多其他无服务器lambda方法,所以我决定在一个无服务器的.NETAPI项目中移动我的lambda方法。我可以将此api项目部署到AWS云,然后手动设置授权者以使用我的自定义授权lambda方法 困难的部分是,我想通过serverless.template文件配置所有这些内容

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

接下来,我创建了一个Lambda自定义授权器,并为Lambda方法和自定义授权器配置了API网关

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

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

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

我不想硬编码任何东西

    "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", ""]}
        }
    }
}
AWS上周宣布了对SAM的支持(之前也可以这样做,但之后必须在SAM模板中使用内嵌式招摇过市)

上面的页面链接了几个GitHub示例,我想最接近您的问题。下面的代码是从中复制的。另请参见AWS SAM规范的部分

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:section中的有效密钥。@AWS SAM中对API网关授权程序的muhammadkashif支持仅在10天前,但应在AWS Lambda可用的所有地区提供(根据公告)。您尚未指定
Auth
无效的方式。我的猜测是,您使用的工具在部署CloudFormation模板之前对其进行验证,该工具尚未添加对
Auth
对象的支持。