skip to Main Content

I’m trying to pass a literal value to the called lambda function using API gateway integration requestParameters but with no luck.

My current code is:

myApigatewayResource.addMethod(
    'GET',
    new apiGateway.LambdaIntegration(
        myLambdaFunctionHandler,
        {
            requestParameters: {
                'integration.request.querystring.customvalue': '123456789'
            }
        }
    ),
    {
        methodResponses: [
            { statusCode: '200' },
        ]
    }
);

When running cdk synth && cdk deploy I get the error:

Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: 123456789

Official documentation for LambdaIntegrationOptions states:

Specify request parameters as key-value pairs (string-to-string mappings), with a destination as the key and a source as the value.

Specify the destination by using the following pattern integration.request.location.name, where location is querystring, path, or header, and name is a valid, unique parameter name.

The source must be an existing method request parameter or a static value. You must enclose static values in single quotation marks and pre-encode these values based on their destination in the request.

My code looks compliant with what stated since I used ‘integration.request.querystring.customvalue’ as destination/key and a static value enclosed in single quotes as source/value.

Where is the problem?

2

Answers


  1. The source must be an existing method request parameter or a static value. You must enclose static values in single quotation marks and pre-encode these values based on their destination in the request.

    It means that the value itself as it gets published to CloudFormation stack and later to API Gateway should be enclosed in single quotation marks.

    The quotation marks in '123456789' are a part of the JavaScript code. The value defined with this code is the string 123456789, without the quotation marks.

    Try this:

    requestParameters: {
       'integration.request.querystring.customvalue': "'123456789'"
    }
    
    Login or Signup to reply.
  2. Some time ago I has a similar issue. So, you can’t directly pass a hard-coded, literal value through requestParameters. You need to set a default value for a query parameter in your API Gateway method, and then map this parameter to your Lambda function.

    export class MyAWSomeStack extends cdk.Stack {
      constructor(...) {
        super(...);
    
        const lambdaHandler = new lambda.Function(this, 'MyFunction', {
          // lambda props here
        });
    
        const api = new apiGateway.RestApi(this, 'MyApi', {
          // apigw props here
        });
    
        const getResource = api.root.addResource('myresource');
    
        getResource.addMethod('GET', new apiGateway.LambdaIntegration(lambdaHandler, {
          requestParameters: {
            'integration.request.querystring.customvalue': 'method.request.querystring.customvalue'
          },
          passthroughBehavior: apiGateway.PassthroughBehavior.WHEN_NO_TEMPLATES
        }), {
          requestParameters: {
            'method.request.querystring.customvalue': false // false means the parameter is not required
          },
          methodResponses: [{ statusCode: '200' }]
        });
    
        getResource.addMethodResponse({
          statusCode: '200',
          responseParameters: {
            'method.response.header.customvalue': false
          }
        });
    
        getResource.addIntegrationResponse({
          statusCode: '200',
          responseParameters: {
            'method.response.header.customvalue': "'123456789'"
          }
        });
      }
    }
    

    The addMethodResponse and addIntegrationResponse set up a default value for a response header.

    P.S. I did not deploy this code, so please let me know if it’s work 😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search