如何在AWS SAM模板中为AWS :: Serverless :: Api添加请求验证器? [英] How to add a request validator in a AWS SAM template for AWS::Serverless::Api?

查看:144
本文介绍了如何在AWS SAM模板中为AWS :: Serverless :: Api添加请求验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS SAM将请求验证器资源链接到SAM模板中的无服务器API。我已经创建了请求验证器,并在其RestApiId中引用了API,但是在AWS控制台中验证器未设置为API默认验证器选项。

I'm trying to use AWS SAM to link a request validator resource to a serverless API in a SAM template. I have created the request validator and referenced the API in its RestApiId but the validator doesn't get set as the API default validator option in the AWS console.

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上的请求验证器下拉菜单,则可以看到它。但是,请求验证程序不是默认使用我定义的验证程序。它只是没有None作为选择的选项

The validator gets created and if I click on the Request Validator drop down menu on the API I can see it. However, the Request Validator doesn't default to my defined validator. It just has None as an the selected option

推荐答案

我在源代码中搜索了api转换函数,但是没有办法追加请求验证。 SAM将AWS :: Serverless :: Api转换为内联主体swagger / openapi定义,但对于x-amazon-apigateway-request-validator没有任何意义。我不明白为什么只有在api方法中附加架构部分时,才能在lambda事件中定义模型

I searched through source code for api transformation functions and there is no way how to append request validation. SAM transform AWS::Serverless::Api to inline body swagger/openapi definition but has nothing for x-amazon-apigateway-request-validator. I don't understand why there is way to define model in lambda event when it only append schema section in api method

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

也许在SAM github中添加功能请求

maybe add feature request in SAM github

这篇关于如何在AWS SAM模板中为AWS :: Serverless :: Api添加请求验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆