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

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

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

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

因为,我需要公开许多其他无服务器 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", ""]}
        }
    }
}

最佳答案

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

上面的页面链接了几个 GitHub 示例,我猜 Lambda Request Authorizer最接近你的问题。下面的代码是从 template.yaml 复制的.另请参阅 API Auth Object 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/'

关于amazon-web-services - 具有自定义授权和 Rest API 的 AWS 无服务器模板配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53104847/

相关文章:

javascript - 将 Node.js API 与 AWS Lambda 混合使用

node.js - 带有依赖项的 AWS Lambda 打包

linux - AWS 主管 spawnerr : can't find command on Ubuntu

ubuntu - nginx 已启动但端口 80 未在 AWS 上使用(根据 netstat)

amazon-web-services - 在 Amazon Redshift 中延迟执行 SQL 脚本

amazon-web-services - 如何将对象传递给亚马逊 SNS

c# - 使用 nameof() 获取 ModelState.AddModelError() 的属性路径

c# - ASP.NET Core 路由到 HttpGet 路由中除我的 Name 之外的其他方法

c# - 下载后在客户端获取文件名。 CORS

amazon-web-services - lambda 函数 IAM 角色是否需要 IAM 权限才能调用自身?