requestParameters returning "Invalid mapping expression specified: true"

23,699

Solution 1

It looks like the requestParameters in the s-function.json file is meant for configuring the integration request section, so I ended up using:

"requestParameters": {
    "integration.request.querystring.startYear" : "method.request.querystring.startYear",
    "integration.request.querystring.startMonth" : "method.request.querystring.startMonth",
    "integration.request.querystring.startDay" : "method.request.querystring.startDay",
    "integration.request.querystring.currentYear" : "method.request.querystring.currentYear",
    "integration.request.querystring.currentMonth" : "method.request.querystring.currentMonth",
    "integration.request.querystring.currentDay" : "method.request.querystring.currentDay",
    "integration.request.querystring.totalDays" : "method.request.querystring.totalDays",
    "integration.request.querystring.volume" : "method.request.querystring.volume",
    "integration.request.querystring.userId" : "method.request.querystring.userId"
},

This ended up adding them automatically to the method request section on the dashboard as well:

Method request section with parameters automatically added

I could then use them in the mapping template to turn them into a method post that would be sent as the event into my Lambda function. Right now I have a specific mapping template that I'm using, but I may in the future use Alua K's suggested method for mapping all of the inputs in a generic way so that I don't have to configure a separate mapping template for each function.

Solution 2

First, you need to execute a put-method command for creating the Method- Request with query parameters:

aws apigateway put-method --rest-api-id "yourAPI-ID" --resource-id "yourResource-ID" --http-method GET --authorization-type "NONE" --no-api-key-required --request-parameters "method.request.querystring.paramname1=true","method.request.querystring.paramname2=true"

After this you can execute the put-integration command then only this will work. Otherwise it will give invalid mapping error

"requestParameters": {
    "integration.request.querystring.paramname1" : "method.request.querystring.paramname1",
    "integration.request.querystring.paramname2" : "method.request.querystring.paramname2",

Solution 3

You can pass query params to your lambda like

"requestTemplates": {
     "application/json": {
        "querystring": "$input.params().querystring"
      }
 }

In lambda function access querystring like this event.querystring

Share:
23,699
Seafish
Author by

Seafish

Updated on April 11, 2020

Comments

  • Seafish
    Seafish about 4 years

    I'm configuring a lambda function's API gateway integration with the Serverless Framework version 0.4.2.

    My problem is with defining an endpoint's request parameters. The AWS docs for API gateway entry says:

    requestParameters

    Represents request parameters that can be accepted by Amazon API Gateway. Request parameters are represented as a key/value map, with a source as the key and a Boolean flag as the value. The Boolean flag is used to specify whether the parameter is required. A source must match the pattern method.request.{location}.{name}, where location is either querystring, path, or header. name is a valid, unique parameter name. Sources specified here are available to the integration for mapping to integration request parameters or templates.

    As I understand it, the config in the s-function.json is given directly to the AWS CLI, so I've specified the request parameters in the format: "method.request.querystring.startYear": true. However, I'm receiving an Invalid mapping expression specified: true error. I've also tried specifying the config as "method.request.querystring.startYear": "true" with the same result.

    s-function.json:

    {
        "name": "myname",
        // etc...
        "endpoints": [
            {
                "path": "mypath",
                "method": "GET",
                "type": "AWS",
                "authorizationType": "none",
                "apiKeyRequired": false,
                "requestParameters": {
                    "method.request.querystring.startYear": true,
                    "method.request.querystring.startMonth": true,
                    "method.request.querystring.startDay": true,
                    "method.request.querystring.currentYear": true,
                    "method.request.querystring.currentMonth": true,
                    "method.request.querystring.currentDay": true,
                    "method.request.querystring.totalDays": true,
                    "method.request.querystring.volume": true,
                    "method.request.querystring.userId": true
                },
                // etc...
            }
        ],
        "events": []
    }
    

    Any ideas? Thanks in advance!

  • Alua K
    Alua K about 8 years
    other better solution stackoverflow.com/a/33602122/2318357. But it does not involve sls.
  • Alua K
    Alua K about 8 years
  • Seafish
    Seafish about 8 years
    I was more concerned about the syntax/format of the requestParameters for this question, but this is a good suggestion that I'll start using as well