Thanks David for following up.
I did further debugging with a very simple lambda to establish connectivity to Neo4j. That works. So I think there may have been too many promise chaining happening. I think I know where my mistakes are.
Now facing another problem with how AWS lambda works.
In order to share the connection across multiple promises, the driver is initialised once and re-used.
Each promise is responsible for closing the session.
At the end of the lambda execution, the lambda function is responsible for closing the driver connection.
All good and works fine.
The problem becomes when the lambda is called in quick succession (as can happen). AWS Lambda will use a 'hot' container for the previous exited lambda call and use the driver that was previously initialised and then closed. I get errors then. If I leave it a bit and re-trigger the lambda, it works fine.
I think I have just set driver initialisation wrong. Or I have to figure out how to verify connection on initialisation and then force it to re-connect.
Sample code for setting up the driver
const NEO4J = require('neo4j-driver');
const neo4jURI = 'neo4j+s://xxxxxxxxx';
const neo4jUser = 'xxxxx';
const neo4jPassword = 'xxxxxx';
// Export a module-scoped Neo4j Driver to be shared across functions.
const neo4jDriver = NEO4J.driver(neo4jURI, NEO4J.auth.basic(neo4jUser, neo4jPassword), {logging: {
level: 'debug', logger: (level: string, message: string) => console.log(level + ' ' + message)
}});
module.exports = neo4jDriver;
Code snippet for using the neo4jDriver
const neo4jDriver = require('../../services/neo4j.client');
const session = neo4jDriver.session();
// cypher is whatever the cypher statement to be ran
// cypherParam is whatever parameters to be associated with
const writeTxPromise = session.writeTransaction((tx:any) => tx.run(cypher,cypherParams));
writeTxPromise
.then((result: any) => {
session.close();
resolve(result);
})
.catch((e: any) => {
session.close();
reject(e);
});
The lambda may be running through multiple promises that has the above snippets.
At the end of those promises, the lambda issues this statement:
Promises....
.then( .... )
.catch( ...)
.finally(() => {
// Close the neo4jDriver as all transactions have completed.
neo4jDriver.close();
});
Any ideas of how to verify connection? Error only occurs when the lambda is called in quick succession.