How to add a request validator in a AWS SAM template for AWS::Serverless::Api?
Asked Answered
V

1

8

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

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

Voiceful answered 9/9, 2019 at 20:12 Comment(1)
Does this answer your question? How do I set up a model to use for validation in a lambda function in a SAM template?Renounce
H
3

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
      }, 

maybe add feature request in SAM github

Hypotension answered 5/10, 2019 at 18:47 Comment(1)
I think there's no need to add a feature request, since you can append request validation adding the model definition to the Properties section of your Api Gateway in your SAM template following this How do I set up a model to use for validation in a lambda function in a SAM template?Renounce

© 2022 - 2024 — McMap. All rights reserved.