gpt4 book ai didi

aws-sam - 如何使用 AWS SAM 设置请求验证器?

转载 作者:行者123 更新时间:2023-12-03 14:42:48 26 4
gpt4 key购买 nike

我使用 JSONschema 定义了一个模型并将其设置为 lambda。我可以看到模型被添加到请求正文中,如下图所示

request method console picture

但我还需要设置请求验证器来验证它。这是我下面的示例 AWS SAM 模板。

Resources:
Api:
Type: "AWS::Serverless::Api"
Properties:
StageName: !Ref Environment
TracingEnabled: false
EndpointConfiguration: REGIONAL
Auth:
Authorizers:
Auth:
FunctionPayloadType: TOKEN
FunctionArn: !GetAtt Auth.Arn
Identity:
Header: authorization
Models:
RegisterCat:
$schema: "http://json-schema.org/draft-04/hyper-schema#"
title: RegisterCat
type: object
properties:
name:
type: string
maxLength: 32
species:
type: string
maxLength: 32
age:
type: integer
minimum: 0
maximum: 100
required:
- name
- species
- age
RegisterCat:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: !Join ["-", [example, !Ref Environment, register, cat]]
CodeUri: register_cat/
Environment:
Variables:
TABLE_NAME: !Join ["-", [!Ref Environment, cat, table]]
Policies:
- Statement:
- Sid: CatTable
Effect: Allow
Action:
- "dynamodb:PutItem"
Resource: !GetAtt CatTable.Arn
Events:
PublicApi:
Type: Api
Properties:
Path: /cat/
Method: POST
RestApiId: !Ref Api
RequestModel:
Model: RegisterCat
Required: true

我可以看到,当您在 aws cli 或 Cloudformation 中创建方法时,可以选择添加请求验证器
  put-method
--rest-api-id <value>
--resource-id <value>
--http-method <value>
--authorization-type <value>
[--authorizer-id <value>]
[--api-key-required | --no-api-key-required]
[--operation-name <value>]
[--request-parameters <value>]
[--request-models <value>]
[--request-validator-id <value>]
[--authorization-scopes <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
Type: AWS::ApiGateway::Method
Properties:
ApiKeyRequired: Boolean
AuthorizationScopes:
- String
AuthorizationType: String
AuthorizerId: String
HttpMethod: String
Integration:
Integration
MethodResponses:
- MethodResponse
OperationName: String
RequestModels:
Key : Value
RequestParameters:
Key : Value
RequestValidatorId: String
ResourceId: String
RestApiId: String

我多次阅读 Github 中的 SAM 规范文档并尝试设置请求验证器。但是我找不到任何方法来设置它与 SAM。有没有办法在方法上设置请求验证器,还是应该在 SAM 存储库中请求该功能?

感谢您阅读我的问题。

最佳答案

  • 添加类型为 AWS::ApiGateway::Method 的资源并连接到您的 Api资源与 RestApiIdResourceId .
  • 将 RequestValidatorId 值设置为
  • 对 RequestValidator 资源的引用,
  • 您已经在其他地方创建的 ID(此信息不在问题中)


  • 这是一个带有 RequestValidator 资源的示例。让我们首先添加一些新参数(contentHandling、validateRequestBody、validatorName)。然后是新资源。
    Parameters:
    ...
    Environment:
    ...
    contentHandling:
    Type: String
    operationName:
    Type: String
    Default: testoperationName
    validatorName:
    Type: String
    Default: testvalidatorName
    validateRequestBody:
    Type: String
    Default: testvalidateRequestBody

    Resources:
    ...
    Method:
    Type: AWS::ApiGateway::Method
    Properties:
    HttpMethod: POST
    ResourceId: !GetAtt Api.RootResourceId
    RestApiId: !Ref Api
    ...
    RequestValidatorId: !Ref MyRequestValidator
    OperationName: !Ref operationName

    RequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
    Name: !Ref validatorName
    RestApiId: !Ref Api
    ValidateRequestBody: !Ref validateRequestBody
    ValidateRequestParameters: !Ref validateRequestParameters
    更多信息可以在这里找到:
  • AWS::ApiGateway::Method Associated Request Validator Example (向下滚动 yaml 越过 json )
  • AWS::ApiGateway::RequestValidator Docs
  • 关于aws-sam - 如何使用 AWS SAM 设置请求验证器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59744447/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com