4

I've cdk initialized an app. My app has a stack of ApiGateway with a 'GET' method bound to the root ('/'). A Lambda function is bound to the 'GET' method. I want when I send a 'GET' request with query parameters e.g. ?param1=uno&param2=dos to access them inside myHandler with something like

!pseudo-code alert! 
const queryParams = event.queryParams

Stack config in lib/my-app-stack.ts

import cdk = require("@aws-cdk/core");
import lambda = require("@aws-cdk/aws-lambda");
import apigateway = require("@aws-cdk/aws-apigateway");
import path = require("path");

export class ApiIntegrationStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Handlers
    const my_handler = new lambda.Function(this, "myHandler", {
      code: lambda.Code.asset(path.join(__dirname, "../src")),
      handler: "handler.myHandler",
      runtime: lambda.Runtime.NODEJS_8_10,
      memorySize: 1024
    });
    const my_integration = new apigateway.LambdaIntegration(
      my_handler
    );

    const default_handler = new lambda.Function(this, "defaultHandler", {
      code: lambda.Code.asset(path.join(__dirname, "../src")),
      handler: "handler.defaultHandler",
      runtime: lambda.Runtime.NODEJS_8_10,
      memorySize: 1024
    });

    // API Gateway set-up
    const my_api = new apigateway.LambdaRestApi(this, "myApi", {
      handler: default_handler,
      proxy: false
    });

    // Routing
    my_api.root.addMethod("GET", my_integration); /*, {
      requestParameters: {
        'page_tag': true,
        'language': true
      }
    });
    */
  }
}

Handler config in src/handler.ts

"use strict";
import { Handler, Context, Callback } from "aws-lambda";

const myHandler: Handler = (
  event: any,
  context: Context,
  callback: Callback
) => {

  const queryParams = []; // ??
  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin": "*" // Required for CORS support to work
    },
    body: JSON.stringify({
      message: "Ahoy, My handler here!"
    })
  };

  callback(null, response);
};

const defaultHandler: Handler = (
  event: any,
  context: Context,
  callback: Callback
) => {
  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin": "*" // Required for CORS support to work
    },
    body: JSON.stringify({
      message: "Ahoy, Default handler here!"
    })
  };

  callback(null, response);
};

export { myHandler, defaultHandler };

Questions I find related to this question:

requestParameters returning “Invalid mapping expression specified: true”

passing query params for aws lambda function

Thank you for your attention!

1
  • 1
    const myParam = event.queryStringParameters.my_param Commented Jan 6, 2020 at 7:19

1 Answer 1

8

Somehow this also wasn't completely clear to me from (skimming) the documentation; i think it requires some contextual knowledge of api gateway itself.

the .addMethod takes 3 parameters, one of which is requestParameters where you state that you have method request parameters, i.e.

requestParameters: {
   "method.request.querystring.page_tag": true,
   "method.request.querystring.language": true,
}

then, in the integration itself, provide a requestParameters value where you map the integration request's query string params to the method request params you set up, i.e.

     requestParameters: {
          "integration.request.querystring.page_tag":
          "method.request.querystring.page_tag", 
          "integration.request.querystring.language":
          "method.request.querystring.language",
        },
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.