簡體   English   中英

如何在AWS SAM模板中為AWS :: Serverless :: Api添加請求驗證器?

[英]How to add a request validator in a AWS SAM template for AWS::Serverless::Api?

我正在嘗試使用AWS SAM將請求驗證器資源鏈接到SAM模板中的無服務器API。 我已經創建了請求驗證器,並在其RestApiId中引用了API,但在AWS控制台中未將驗證器設置為API默認驗證器選項。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    Discription for the template
Globals:
  Function:
    Timeout: 30

Resources:
  MyAPI:
    Type: AWS::Serverless::Api
    Properties:
      Name: MyAPI
      StageName: prod
      Auth:
        DefaultAuthorizer: MyAuthorizer
        Authorizers:
            MyAuthorizer:
              FunctionPayloadType: REQUEST
              FunctionArn: here goes the function Arn
              Identity:
                Context:
                  - identity.sourceIp
                ReauthorizeEvery: 60
      Models:
        RequestModel:
          $schema: 'http://json-schema.org/draft-04/mySchema#'
          type: object
          properties:
            Format:
              type: string
            Name:
              type: string
              minLength: 3
            Id:
              type: string
          required:
            - Format
            - Id

  RequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: RequestValidator
      RestApiId: !Ref MyAPI
      ValidateRequestBody: true

  LambdaFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: NameOfTheFunction
      CodeUri: ./src/folder/
      Handler: Project::nameSpace.class::Handler
      Runtime: dotnetcore2.1
      Environment: 
        Variables:
          variableA : value
          variableB : value
          variableC : value
          variableD : value
      Events:
        ApiEndpoint:
          Type: Api
          Properties:
            RestApiId: !Ref MyAPI
            Path: /path
            Method: post
            RequestValidatorId: !Ref RequestValidator
            Auth:
              Authorizer: MyAuthorizer
            RequestModel: 
              Model: RequestModel
              Required: true

創建驗證器,如果我單擊API上的“請求驗證器”下拉菜單,則可以看到它。 但是,請求驗證器不會默認使用我定義的驗證器。 它只是沒有作為選定選項

我在源代碼中搜索了api轉換函數,沒有辦法附加請求驗證。 SAM將AWS :: Serverless :: Api轉換為內聯主體swagger / openapi定義,但對於x-amazon-apigateway-request-validator沒有任何意義。 我不明白為什么只有在api方法中附加架構部分時,才能在lambda事件中定義模型

"paths": {
  "/post": {
    "post": {
      "requestBody": {
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/user"
            }
          }
        }, 
        "required": true
      }, 

也許在SAM github中添加功能請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM