Hello, I am trying to use Neptune DB from the nodejs codebase using aws-sdk javascript v3 through gremlin query however it seems like connection not getting build when I am executing gremlin query from my codebase.
Below I have attached the complete code and error in details, please help:
nodeController.js
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const dotenv = require('dotenv');
dotenv.config();
const ClusterEndpoint = process.env.CLUSTER_ENDPOINT;
const Port = 8182;
dc = new DriverRemoteConnection(wss://${ClusterEndpoint}:${Port}/gremlin
, {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
module.exports.getNodesList = async (req, res) => {
try {
// Execute the Gremlin query
const result = await g.V().toList();
// Extract the nodes from the result
const nodes = result.map(vertex => vertex.value);
// Send the nodes as the response
res.status(200).json({ nodes });
} catch (error) {
console.error("Error retrieving nodes:", error);
res.status(500).json({ error: "Internal Server Error" });
}
};
connection.js
const { NeptuneClient, DescribeDBClustersCommand, AddRoleToDBClusterCommand } = require("@aws-sdk/client-neptune");
const dotenv = require('dotenv');
dotenv.config();
const createNeptuneClient = () => {
const config = {
region: process.env.REGION,
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
};
return new NeptuneClient(config);
};
const checkNeptuneConnection = async () => {
const neptuneClient = createNeptuneClient();
const input = {
DBClusterIdentifier: process.env.NEPTUNE_DB_NAME,
};
const describeCommand = new DescribeDBClustersCommand(input);
try {
const response = await neptuneClient.send(describeCommand);
console.log("Successfully connected to Neptune database:", response);
return true;
} catch (error) {
console.error("Error connecting to Neptune database:", error);
return false;
}
};
module.exports = { createNeptuneClient, checkNeptuneConnection };
server.js
const express = require('express');
const clc = require('cli-color');
const { checkNeptuneConnection } = require('./src/data/connection/connection');
const { getNodesList } = require('./src/controller/nodes');
const app = express();
const dotenv = require('dotenv');
dotenv.config();
const PORT = parseInt(process.env.PORT || '3000', 10);
// Check Neptune connection before starting the server
(async () => {
const isConnected = await checkNeptuneConnection();
if (!isConnected) {
console.error("Exiting. Unable to connect to Neptune database.");
process.exit(1);
}
// Proceed with starting the server
app.get('/', (req, res) => {
res.send('Hello Neptune, Welcome to GraphDB World!');
});
app.use('/api/gremlin-query', getNodesList);
app.listen(PORT, () => {
console.log(clc.green("##############################################"));
console.log(clc.red.bold.bgWhite(`Neptune Server Running On Port ${PORT}`));
console.log(clc.green("##############################################"));
});
})();
Hello - does the location you are running the Node.js code from have access to the VPC containing the Amazon Neptune database?
Hey @AWS-KRL, when I am running the below code it shows successfully connected to database and also logs the database cluster details on terminal
const { NeptuneClient, DescribeDBClustersCommand, AddRoleToDBClusterCommand } = require("@aws-sdk/client-neptune");
const dotenv = require('dotenv'); dotenv.config();
const createNeptuneClient = () => { const config = { region: process.env.REGION, accessKeyId: process.env.ACCESS_KEY_ID, secretAccessKey: process.env.SECRET_ACCESS_KEY, }; return new NeptuneClient(config); };
const checkNeptuneConnection = async () => { const neptuneClient = createNeptuneClient();
const input = { DBClusterIdentifier: process.env.NEPTUNE_DB_NAME, };
const describeCommand = new DescribeDBClustersCommand(input);
try { const response = await neptuneClient.send(describeCommand); console.log("Successfully connected to Neptune database:", response); return true; } catch (error) { console.error("Error connecting to Neptune database:", error); return false; } };
It makes sense that you're able to do a DescribeDBClusters from local, as you're accessing Neptune's Control Plane APIs (which are available from a public endpoint). Any access to Neptune's Data Plane (as in via queries) requires accessing the Neptune Cluster Endpoints which are only accessible from within the VPC where Neptune is hosted.