Scenario
- My React (Gatsby) application requests information from a database to display a list of products.
- The database is a Postgres table on AWS RDS, in a VPC.
- My aim is for the React application to trigger an AWS Lambda function to retrieve products from AWS RDS.
Error:
- In writing my lambda function, I intend to request all products from my table.
- The error message I get is
TypeError: Wrong arguments
Code:
index.js
const rds = require('./connection.js');
exports.handler = async ( event, context, callback ) => {
await callback(null, rds.getProducts);
};
connection.js
const Pool = require('pg').Pool;
const pool = new Pool({
user: process.env.user,
host: process.env.host,
database: process.env.database,
password: process.env.password,
port: process.env.port,
});
const getProducts = ( request, response ) => {
pool.query(`SELECT * FROM product_list ORDER by id ASC`, ( error, result ) => {
if( error ) throw new Error(error);
response.status(200).json(result.rows);
})
};
module.exports = {
getProducts,
};
package.json
{
"name": "lambda3",
"version": "1.0.0",
"description": "lambda function access rds",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "scott",
"license": "ISC",
"dependencies": {
"pg": "^7.14.0"
}
}
Full error:
{
"errorType": "TypeError",
"errorMessage": "Wrong arguments",
"trace": [
"TypeError: Wrong arguments",
" at RAPIDClient.postInvocationResponse (/var/runtime/RAPIDClient.js:41:18)",
" at complete (/var/runtime/CallbackContext.js:34:12)",
" at callback (/var/runtime/CallbackContext.js:44:7)",
" at /var/runtime/CallbackContext.js:105:16",
" at Runtime.exports.handler (/var/task/index.js:9:11)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
Thoughts: I followed the AWS guide on how to upload a NodeJS deployment package. There didn't seem to be an issue with connection.js when testing locally.
Unsure how to debug this as even "AWS Lambda wrong arguments" yields few relevant results.