0
votes

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.

1
Did you set all the 5 environment variables - Arun K
@ArunmainthanKamalanathan yes - Scott
are you sure about this, await callback(null, rds.getProducts); you are awaiting on the callback. - Arun K
@ArunmainthanKamalanathan I think it should not matter as the callback is the only thing in the function block. I have tried with and without the async/await keywords, and there has been no difference in output. - Scott
Is it working in your local. you are passing "rds.getProducts" as a function reference. you are not calling the function for e.g "rds.getProducts()" . Also in the connection.js you are calling "response.status(200)", it looks like an express syntax to me. - Arun K

1 Answers

0
votes

What I see as the main issue here is how you use the call back.

The callback function accepts two parameters error and the value.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

What I think that you are doing wrong here is, passing the function as reference instead of value for e.g rds.getProducts() to get the value.