I am using Lambda function to query a RDS MySQL database. To fetch a row, I am passing the primary key as parameter in the URL (with AWS API Gateway). Example URL is:
https://aaaaaaa.execute-api.aaaaaaa.amazonaws.com/default/getresult?pk=1245
In the Lambda function,
exports.handler = (event, context, callback) => {
//prevent timeout from waiting event loop
callback(null, event);
};
I am getting output as {}
for the url.
Note: Lambda proxy integration is enabled.
Lambda Proxy Integration should be enabled on the API Gateway in order for API Gateway to pass the event details, including the params, to Lambda.
See this image here for how to do this in the console:
Go to your API, then your Resources, then your Method Execution, and then select "Integration Request". From there tick the box that says "Use Lambda Proxy Integration".
Enabling this allows API Gateway to proxy the request to Lambda with the request details, including the params, available in the event.
Make sure, you check the Use Lambda Proxy Integration check box, which will establish an integration of type Lambda-Proxy between API Gateway’s method and the associated Lambda function.
With the Lambda proxy integration, Lambda is required to return an output of the following format (doc):
{
"isBase64Encoded" : "boolean",
"statusCode": "number",
"headers": { ... },
"body": "JSON string"
}
This mean if you want to send back event
object to client, you have to put to callback
a object with above format.
exports.handler = (event, context, callback) => {
//prevent timeout from waiting event loop
const response = {
statusCode: 200,
headers: {
"x-custom-header": "my custom header value"
},
body: JSON.stringify({
message: 'Your function executed successfully!',
input: event,
}),
};
// success response
callback(null, response);
};
This problem can also occur if you go most of the way through the API Gateway setup modification process but do not click on Deploy. Until we clicked on Deploy, we got test requests populated with event information, while real HTTPS connections created empty events ({}).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With