1
votes

I was trying to create a requestValidator and use it in my request by
"RequestValidatorId": { "Ref": "PostRequestValidator" } .
It should return the id of the requestValidator according to the doc.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-requestvalidator.html

But below error occurs.

Logical ID: postBannerMethod

Encountered unsupported property RequestValidatorId

resources.json

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Parameters": {
      "RolesStack": {
        "Type": "String",
        "Default": "admin-iam-roles"
      },
      "HandlerCodeS3Bucket": {
        "Type": "String",
        "Default": "admin-lambda-sourcecode"
      },
      "HandlerCodeS3BucketLayer": {
        "Type": "String",
        "Default": "admin-lambda-sourcecode/layers"
      },
      "HandlerCodeS3Key": {
        "Type": "String",
        "Default": "helloWorld.zip"
      }
    },
    "Resources": {
     
      "MyLayer": {
        "Type": "AWS::Lambda::LayerVersion",
        "Properties": {
            "CompatibleRuntimes": [
                "nodejs12.x"
            ],
            "Content": {
                "S3Bucket": {
                  "Ref": "HandlerCodeS3Bucket"
                },
                "S3Key": "imageUploadLayer.zip"
            },
            "Description": "My layer",
            "LayerName": "imageLayer",
            "LicenseInfo": "MIT"
        }
    },
      "createBannerHandler": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
          "FunctionName": "createBanner",
          "Handler": "createBanner.handler",
          "Role": {
            "Fn::ImportValue": {
              "Fn::Sub": "${RolesStack}-LambdaRoleArn"
            }
          },
          "Code": {
            "S3Bucket": {
              "Ref": "HandlerCodeS3Bucket"
            },
            "S3Key":"createBanner.zip"
          },
          "Layers": [
            {
              "Ref": "MyLayer"
            }
          ],
          "Runtime": "nodejs12.x"
        }
      },
      "HelloWorldApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": "hello-api",
          "Description": "API used for practice",
          "FailOnWarnings": true
        }
      },
      "PostRequestValidator": {
        "Type" : "AWS::ApiGateway::RequestValidator",
        "Properties" : {
            "Name" : "PostRequestValidator",
            "RestApiId" : {
                "Ref": "HelloWorldApi"
            },
            "ValidateRequestBody" : true,
            "ValidateRequestParameters" : false
        }
      },
      "BannerResource": {
        "Type": "AWS::ApiGateway::Resource",
        "Properties": {
            "RestApiId": {
                "Ref": "HelloWorldApi"
            },
            "ParentId": {
                "Fn::GetAtt": [
                    "HelloWorldApi",
                    "RootResourceId"
                ]
            },
            "PathPart": "banner"
        }
      },
      "postBannerMethod": {
        "Type": "AWS::ApiGateway::Method",
        "DependsOn": ["HelloWorldApi"],
        "Properties": {
          "RestApiId": {
            "Ref": "HelloWorldApi"
          },
          "ResourceId": {
            "Ref": "BannerResource"
          },
          "HttpMethod": "POST",
          "AuthorizationType": "NONE",
          "Integration": {
            "Credentials": {
              "Fn::ImportValue": {
                "Fn::Sub": "${RolesStack}-ApiGatewayRoleArn"
              }
            },
            "IntegrationHttpMethod": "POST",
            "Type": "AWS_PROXY",
            "RequestValidatorId": {
              "Ref": "PostRequestValidator"
            },
            "Uri": {
              "Fn::Join": ["",
                [
                  "arn:aws:apigateway:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":lambda:path/2015-03-31/functions/",
                  {
                    "Fn::GetAtt": ["createBannerHandler", "Arn"]
                  },
                  "/invocations"
                ]
              ]
            }
          }
        }
      }
    }
  }

2

2 Answers

1
votes

Your RequestValidatorId is one level to deep. It should be in AWS::ApiGateway::Method, not in Integration:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "RolesStack": {
      "Type": "String",
      "Default": "admin-iam-roles"
    },
    "HandlerCodeS3Bucket": {
      "Type": "String",
      "Default": "admin-lambda-sourcecode"
    },
    "HandlerCodeS3BucketLayer": {
      "Type": "String",
      "Default": "admin-lambda-sourcecode/layers"
    },
    "HandlerCodeS3Key": {
      "Type": "String",
      "Default": "helloWorld.zip"
    }
  },
  "Resources": {
    "MyLayer": {
      "Type": "AWS::Lambda::LayerVersion",
      "Properties": {
        "CompatibleRuntimes": [
          "nodejs12.x"
        ],
        "Content": {
          "S3Bucket": {
            "Ref": "HandlerCodeS3Bucket"
          },
          "S3Key": "imageUploadLayer.zip"
        },
        "Description": "My layer",
        "LayerName": "imageLayer",
        "LicenseInfo": "MIT"
      }
    },
    "createBannerHandler": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "FunctionName": "createBanner",
        "Handler": "createBanner.handler",
        "Role": {
          "Fn::ImportValue": {
            "Fn::Sub": "${RolesStack}-LambdaRoleArn"
          }
        },
        "Code": {
          "S3Bucket": {
            "Ref": "HandlerCodeS3Bucket"
          },
          "S3Key": "createBanner.zip"
        },
        "Layers": [
          {
            "Ref": "MyLayer"
          }
        ],
        "Runtime": "nodejs12.x"
      }
    },
    "HelloWorldApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "hello-api",
        "Description": "API used for practice",
        "FailOnWarnings": true
      }
    },
    "PostRequestValidator": {
      "Type": "AWS::ApiGateway::RequestValidator",
      "Properties": {
        "Name": "PostRequestValidator",
        "RestApiId": {
          "Ref": "HelloWorldApi"
        },
        "ValidateRequestBody": true,
        "ValidateRequestParameters": false
      }
    },
    "BannerResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": {
          "Ref": "HelloWorldApi"
        },
        "ParentId": {
          "Fn::GetAtt": [
            "HelloWorldApi",
            "RootResourceId"
          ]
        },
        "PathPart": "banner"
      }
    },
    "postBannerMethod": {
      "Type": "AWS::ApiGateway::Method",
      "DependsOn": [
        "HelloWorldApi"
      ],
      "Properties": {
        "RestApiId": {
          "Ref": "HelloWorldApi"
        },
        "ResourceId": {
          "Ref": "BannerResource"
        },
        "HttpMethod": "POST",
        "AuthorizationType": "NONE",
        "RequestValidatorId": {
          "Ref": "PostRequestValidator"
        },
        "Integration": {
          "Credentials": {
            "Fn::ImportValue": {
              "Fn::Sub": "${RolesStack}-ApiGatewayRoleArn"
            }
          },
          "IntegrationHttpMethod": "POST",
          "Type": "AWS_PROXY",
          "Uri": {
            "Fn::Join": [
              "",
              [
                "arn:aws:apigateway:",
                {
                  "Ref": "AWS::Region"
                },
                ":lambda:path/2015-03-31/functions/",
                {
                  "Fn::GetAtt": [
                    "createBannerHandler",
                    "Arn"
                  ]
                },
                "/invocations"
              ]
            ]
          }
        }
      }
    }
  }
}
0
votes

Recommend trying the CloudFormation Linter in VSCode to see some of these errors inline while authoring templates along with autocompletion and documentation links:

Visual Studio Code screenshot

Visual Studio Code screenshot of documentation link

[cfn-lint] E3002: Invalid Property Resources/postBannerMethod/Properties/Integration/RequestValidatorId