How can I set Request Validator with AWS SAM?
Asked Answered
N

1

7

I defined a model with JSONschema and set it to the lambda. I could see that the model was added in Request Body like the picture below

request method console picture

But I also need to set Request Validator to validate it. This is my example AWS SAM template below.

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

I can see that there is an option to add request validator when you create method in aws cli or 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

I read SAM specification document in Github several times and tried setting the request validator. However I couldn't find any way to set it up with SAM. Is there a way to set the request validator on method or should I request the feature in SAM repo?

Thank you for reading my question.

Name answered 15/1, 2020 at 2:35 Comment(0)
A
2
  1. Add a resource of type AWS::ApiGateway::Method and connect to your Api resource with RestApiId and ResourceId.
  2. Set the RequestValidatorId value to be either
    • a reference to a RequestValidator resource,
    • the id of the one you already have created somewhere else (this information wasn't in the question)

Here is an example with a RequestValidator resource. Lets start first by adding some new params (contentHandling, validateRequestBody, validatorName). And then the new resources.

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

More info can be found here:

Ap answered 29/7, 2020 at 19:39 Comment(1)
@Mr.Leshem maybe you should open your own question?Ap

© 2022 - 2024 — McMap. All rights reserved.