Passing Parameters to AWS Lambda via Amazon API Gateway

There are two primary methods to pass query string parameters or route parameters from Amazon API Gateway to an AWS Lambda function: Lambda Custom Integration and Lambda Proxy Integration.

Lambda Custom Integration

  1. Open the API Gateway Console: Go to the Amazon API Gateway console and select your API.
  2. Select the HTTP Method: In the Resources pane, select the HTTP method you’ve set up. If multiple methods are set up, repeat the process for each.
  3. Configure Method Request:
    • Navigate to Method Request.
    • Expand URL Query String Parameters and click Add query string.
    • Enter the parameter name (e.g., pet) and mark it as required if necessary.
  4. Set Up Integration Request:
    • Go back to the Method Execution pane and select Integration Request.
    • Find Mapping Templates, click Add mapping template, and for Content-Type, enter application/json.
  5. Input Mapping Template:
    • In the mapping template editor, replace any existing code with a JSON object mapping your query string parameters to Lambda function inputs:
      json { "pet": "$input.params('pet')" }
    • Save the template.
  6. Deploy the API: After configuring the mapping template, save your changes and deploy the API to apply them.
  7. Test the API: Test the API using a curl command, including the query string parameter:
    bash curl -X GET https://<api_id>.execute-api.<region>.amazonaws.com/<stage>/lambda-non-proxy?pet=dog

Lambda Proxy Integration (Simplified Method)

Lambda Proxy Integration simplifies the process by automatically passing all the request details (headers, query string parameters, body) to your Lambda function.

  • Enable Lambda Proxy Integration: In the Integration Request section, check “Use Lambda Proxy integration”.
  • Access Parameters in Lambda: Your Lambda function receives an event object containing all request details. Access the parameters as follows:
    python queryStringParameter = event["queryStringParameters"]['queryparam1'] pathParameter = event['pathParameters']['param1']

Note: The choice between custom integration and proxy integration depends on your specific needs. Proxy integration offers simplicity by automatically passing all request details to Lambda, whereas custom integration provides more control over how request and response data are mapped and transformed.

Was this article helpful?
YesNo

Similar Posts