Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-apigateway: requestValidatorOptions does not create validator #29834

Open
anneadb opened this issue Apr 15, 2024 · 6 comments
Open

aws-apigateway: requestValidatorOptions does not create validator #29834

anneadb opened this issue Apr 15, 2024 · 6 comments
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@anneadb
Copy link

anneadb commented Apr 15, 2024

Describe the bug

I am trying to add a request validator to my Lambda proxy API. I am setting requestValidatorOptions in defaultMethodOptions but no validator is being created and added to the methods.
In the UI/console I can add a validation to the "ANY" method which is automatically created as part of LambdaRestApi.

Is there another way to add the validator to the proxy?

Expected Behavior

Validator is created and added to all methods.

Current Behavior

The cloudformation template shows the RequestParameters but no validator. Adding requestValidatorOptions is not recognized as a change during deployment.

Reproduction Steps

Create an api gateway using LambdaRestApi:

new apigateway.LambdaRestApi(this, "LambdaRestApi", {
      restApiName: `${env.project}-${subProjectName}-${env.deployer}`,
      handler: lambdaFunction,
      deployOptions: {
        loggingLevel: apigateway.MethodLoggingLevel.INFO,
        accessLogDestination: new apigateway.LogGroupLogDestination(logGroup),
        accessLogFormat: apigateway.AccessLogFormat.jsonWithStandardFields(),
      },
      defaultMethodOptions: {
        apiKeyRequired: true,
        requestParameters: {
          "method.request.querystring.name": true,
          "method.request.querystring.env": false,
        },
        requestValidatorOptions: {
          requestValidatorName: "DefaultValidator",
          validateRequestParameters: true,
        },
      },
      endpointTypes: [apigateway.EndpointType.REGIONAL],
    })

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.122.0

Framework Version

No response

Node.js Version

v20.10.0

OS

macOS 14.4.1

Language

TypeScript

Language Version

No response

Other information

"AWS::ApiGateway::Method" from template.json:

"ApiGatewaySetupLambdaRestApiproxyANYF9A30FCD": {
   "Type": "AWS::ApiGateway::Method",
   "Properties": {
    "ApiKeyRequired": true,
    "AuthorizationType": "NONE",
    "HttpMethod": "ANY",
    "Integration": {
     "IntegrationHttpMethod": "POST",
     "Type": "AWS_PROXY",
     "Uri": {
      "Fn::Join": [
       "",
       [
        "arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/",
        {
         "Fn::GetAtt": [
          "LambdaSetuplambdaE27E4408",
          "Arn"
         ]
        },
        "/invocations"
       ]
      ]
     }
    },
    "RequestParameters": {
     "method.request.querystring.name": true,
     "method.request.querystring.env": false
    },
    "ResourceId": {
     "Ref": "ApiGatewaySetupLambdaRestApiproxyD538D22D"
    },
    "RestApiId": {
     "Ref": "ApiGatewaySetupLambdaRestApi68D08033"
    }
   },
   "Metadata": {
    "aws:cdk:path": "MyStack/ApiGatewaySetup/LambdaRestApi/Default/{proxy+}/ANY/Resource"
   }
  },
@anneadb anneadb added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 15, 2024
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Apr 15, 2024
@khushail khushail added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Apr 16, 2024
@khushail khushail self-assigned this Apr 16, 2024
@khushail khushail removed the needs-triage This issue or PR still needs to be triaged. label Apr 16, 2024
@khushail
Copy link
Contributor

khushail commented May 7, 2024

Hi @anneadb , thanks for reaching out.

You could try creating a resource and then use .addMethod like this to set the requestValidatorOptions{..}. Here is the article for reference - https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RequestValidatorOptions.html.

Here is snippet of successful implementation of the requestValidator with LambdarestApi-
Screenshot 2024-05-06 at 6 57 43 PM

Please feel free to reach out if this does not work for you.

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. guidance Question that needs advice or information. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. bug This issue is a bug. labels May 7, 2024
@anneadb
Copy link
Author

anneadb commented May 7, 2024

Hi @khushail ,
do you have proxy (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.LambdaRestApi.html#proxy) set to True?
Because I thought that that was not compatible with adding methods.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 7, 2024
@khushail
Copy link
Contributor

khushail commented May 7, 2024

Hi @khushail , do you have proxy (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.LambdaRestApi.html#proxy) set to True? Because I thought that that was not compatible with adding methods.

Sharing the complete code. I kept proxy:false for adding methods -


import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigw from 'aws-cdk-lib/aws-apigateway';
import { ApiGateway } from 'aws-cdk-lib/aws-events-targets';


export class RequestValidator2Stack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myLambda = new lambda.Function(this, 'MyFunction', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline('print("Hello, CDK!") ')
    });

    const api = new apigw.LambdaRestApi(this,'ApiGateway',{
      restApiName: 'RequestValidatorApi',
        handler: myLambda,
        proxy: false
      }
    );

    const responseModel = api.addModel('ResponseModel', {
      contentType: 'application/json',
      modelName: 'ResponseModel',
      schema: { 'schema': apigw.JsonSchemaVersion.DRAFT4, 'title': 'pollResponse', 'type': apigw.JsonSchemaType.OBJECT, 'properties': { 'message': { 'type': apigw.JsonSchemaType.STRING } } }
    });

    const errorResponseModel = api.addModel('ErrorResponseModel', {
      contentType: 'application/json',
      modelName: 'ErrorResponseModel',
      schema: { 'schema': apigw.JsonSchemaVersion.DRAFT4, 'title': 'errorResponse', 'type': apigw.JsonSchemaType.OBJECT, 'properties': { 'message': { 'type': apigw.JsonSchemaType.STRING } } }
    });


    const resource = api.root.addResource('books');
    const getBookIntergration = new apigw.LambdaIntegration(myLambda);

    resource.addMethod('GET', getBookIntergration, {
      requestParameters: {
        'method.request.querystring.who': true
      },
      // we can set request validator options like below
      requestValidatorOptions: {
        requestValidatorName: 'requestValidator',
        validateRequestBody: true,
        validateRequestParameters: true
      },
      methodResponses: [
        {
          // Successful response from the integration
          statusCode: '200',

          responseParameters: {
            'method.response.header.Content-Type': true,
            'method.response.header.Access-Control-Allow-Origin': true,
            'method.response.header.Access-Control-Allow-Credentials': true
          },
          responseModels: {
            'application/json': responseModel
          }
        },
        {
          statusCode: '400',
          responseParameters: {
            'method.response.header.Content-Type': true,
            'method.response.header.Access-Control-Allow-Origin': true,
            'method.response.header.Access-Control-Allow-Credentials': true
          },
          responseModels: {
            'application/json': errorResponseModel
          }
        }
      ]
    });

  }
}

`

@khushail khushail added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 7, 2024
@anneadb
Copy link
Author

anneadb commented May 8, 2024

Thank you for the entire code.
Then sadly this is not an option for me because I specifically want to keep the proxy functionality.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 8, 2024
@khushail khushail added bug This issue is a bug. p2 and removed guidance Question that needs advice or information. labels Jul 17, 2024
@khushail
Copy link
Contributor

@anneadb are you still facing the issue ?

@khushail khushail added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 17, 2024
@anneadb
Copy link
Author

anneadb commented Jul 17, 2024

Hi @khushail ,
yes, I just checked and adding or removing requestValidatorOptions does not register as a change. We're on CDK version 2.149.0 now.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 17, 2024
@khushail khushail added the effort/medium Medium work item – several days of effort label Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants