Open anneadb opened 2 months ago
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-
Please feel free to reach out if this does not work for you.
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.
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
}
}
]
});
}
}
`
Thank you for the entire code. Then sadly this is not an option for me because I specifically want to keep the proxy functionality.
Describe the bug
I am trying to add a request validator to my Lambda proxy API. I am setting
requestValidatorOptions
indefaultMethodOptions
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 ofLambdaRestApi
.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
: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: