Skip to content

How do I configure my API Gateway REST API to pass query string parameters to a backend Lambda function or HTTP endpoint?

3 minute read
1

I need my Amazon API Gateway REST API to pass query string parameters to a backend AWS Lambda function or an HTTP endpoint.

Short description

To configure a REST API to pass query string parameters to a backend AWS Lambda function, use a Lambda custom integration.

To pass query string parameters to an HTTP endpoint, use an HTTP custom integration.

Important: Make sure that you supply the input data as the integration request payload. It's a best practice to use a mapping template to supply the payload. For more information, see Parameter mapping examples for REST APIs in API Gateway.

Resolution

Pass query string parameters to a backend Lambda function

Complete the following steps:

  1. Open the API Gateway console, and then select your API.
  2. In the Resources pane, choose the configured HTTP method.
    Note: If there's more than one HTTP method configured for the API, then repeat these steps for each method.
  3. In the Method Execution pane, choose Method Request and then choose Edit.
  4. Expand the URL Query String Parameters dropdown list, then choose Add query string.
  5. For the Name field, enter pet.
  6. Choose the Required check box, and then choose Save.
  7. Choose the Integration Request tab, and then choose Edit.
  8. Expand Mapping templates, and then choose Add mapping template.
  9. For the Content-Type field, enter application/json.
  10. In the mapping template editor, copy and replace the current script with the following code:
    {     "pet": "$input.params('pet')"
    }
    
    Note: For more information, see the Input variables.
  11. Choose Save, and then choose Deploy the API.
  12. To test the API's new endpoint, run the following curl command:
    curl -X GET https://jp58lnf5vh.execute-api.us-west-2.amazonaws.com/dev/lambda-non-proxy?pet=dog
    
    Important: Make sure that the curl command has the query string parameter pet=dog.

Pass query string parameters to an HTTP endpoint

Complete the following steps:

  1. Open the API Gateway console, and then select your API.
  2. In the Resources pane, choose the configured HTTP method.
    Note: If there's more than one HTTP method configured for the API, then repeat these steps for each method.
  3. In the Method Execution pane, choose Method Request, then choose Edit.
  4. Expand the URL Query String Parameters dropdown list, and then choose Add query string.
  5. For the Name field, enter type, and then choose Save.
  6. Choose the Integration Request tab, and then choose Edit.
  7. Expand the URL Query String Parameters section.
  8. For Name field, enter type.
  9. For the Mapped from field, enter method.request.querystring.type.
  10. Choose Save, then choose Deploy the API.
  11. To test the API's new endpoint, run the following curl command:
    curl -X GET https://jp58lnf5vh.execute-api.us-west-2.amazonaws.com/dev/http-endpoint?pet=dog
    Important: Make sure that the curl command has the query string parameter pet=dog.

Related information

Tutorial: Create a REST API with a Lambda proxy integration

Tutorial: Create a REST API with a Lambda non-proxy integration

Tutorial: Create a REST API with an HTTP proxy integration

Tutorial: Create a REST API with an HTTP non-proxy integration