Skip to content

Instantly share code, notes, and snippets.

@danshev
Created August 13, 2024 18:08
Show Gist options
  • Save danshev/fcf77fd1524fb5857fffe8df198e657f to your computer and use it in GitHub Desktop.
Save danshev/fcf77fd1524fb5857fffe8df198e657f to your computer and use it in GitHub Desktop.
TypeScript for connecting to AWS Neptune w/ v4 Signature
import { driver, structure, process as gremlinProcess } from 'gremlin';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { getUrlAndHeaders } from 'gremlin-aws-sigv4/lib/utils';
async function getCredentials() {
try {
const provider = fromNodeProviderChain({
clientConfig: { region: process.env.REGION },
});
const credentials = await provider();
return { ...credentials, region: process.env.REGION };
} catch (e) {
console.error('No credentials found', e);
throw e;
}
}
export const getGremlinClient = async (
neptuneUrl: string,
): Promise<gremlinProcess.GraphTraversalSource> => {
const credentials = await getCredentials();
const { url, headers } = getUrlAndHeaders(
neptuneUrl,
'8182',
credentials,
'/gremlin',
'wss',
);
const dc = new driver.DriverRemoteConnection(url, { headers });
const graph = new structure.Graph();
return graph.traversal().withRemote(dc);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment