Subchapter 67.5
references/connectivity.mdMarkdown14 KBView on GitHub
Neptune runs inside a VPC by default, but both Neptune Database (engine ≥ 1.4.6.x) and Neptune Analytics support optional public endpoints. Public endpoints are disabled by default and require IAM authentication. When public endpoints are not enabled, every connection requires VPC access — this is the most common source of “connection refused” errors.
| Endpoint | Service | Use for |
|---|---|---|
| Cluster endpoint | Neptune Database | Write operations (points to primary instance) |
| Reader endpoint | Neptune Database | Read operations (load-balanced across replicas) |
| Instance endpoint | Neptune Database | Direct connection to a specific instance |
| Graph endpoint | Neptune Analytics | All operations ({graph-id}.{region}.neptune-graph.amazonaws.com) |
Default port: 8182 (Neptune Database), 443 (Neptune Analytics via SDK)
CloudShell VPC environments let you connect directly without a bastion host.
# Install Gremlin console (check https://tinkerpop.apache.org/downloads.html for latest version)
TINKERPOP_VERSION="3.7.2" # Update to latest stable version
curl -sL "https://downloads.apache.org/tinkerpop/${TINKERPOP_VERSION}/apache-tinkerpop-gremlin-console-${TINKERPOP_VERSION}-bin.zip" -o gremlin-console.zip
unzip gremlin-console.zip
cd "apache-tinkerpop-gremlin-console-${TINKERPOP_VERSION}"# Replace with your Neptune cluster endpoint
NEPTUNE_ENDPOINT="your-cluster.cluster-xxxx.us-east-1.neptune.amazonaws.com"
bin/gremlin.sh
# Inside the Gremlin console:
:remote connect tinkerpop.server conf/neptune-remote.yaml
:remote consoleCreate conf/neptune-remote.yaml:
hosts: [your-cluster.cluster-xxxx.us-east-1.neptune.amazonaws.com]
port: 8182
connectionPool: { enableSsl: true }
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV2d0 }⚠️ CloudShell sessions time out after 30 minutes of inactivity. Reinstall the client after a timeout.
Neptune runs in a VPC. Your Lambda function must also be in the same VPC.
Important: Neptune requires SSL/TLS (TLS 1.2+) for ALL connections. IAM authentication with SigV4 signing automatically uses encrypted connections. There is no option to connect over unencrypted protocols.
# Lambda must be configured with:
# - Same VPC as Neptune
# - Subnet with route to Neptune (same AZ recommended)
# - Security group with outbound TCP 8182 to Neptune's security groupProduction default — IAM auth: This basic handler shows the raw Gremlin connection and assumes IAM database authentication is disabled (dev/test only). For production, enable IAM auth on the cluster and SigV4-sign every request — see the “IAM authentication (recommended for production)” section below, which is the pattern to copy. An unauthenticated connection only works when IAM auth is disabled, which is not recommended for production.
import os
from gremlin_python.driver import client, serializer
NEPTUNE_ENDPOINT = os.environ['NEPTUNE_ENDPOINT'] # cluster endpoint
NEPTUNE_PORT = 8182
def get_gremlin_client():
return client.Client(
f'wss://{NEPTUNE_ENDPOINT}:{NEPTUNE_PORT}/gremlin',
'g',
message_serializer=serializer.GraphSONSerializersV2d0()
)
def lambda_handler(event, context):
gremlin_client = get_gremlin_client()
try:
result = gremlin_client.submit("g.V().limit(10).valueMap(true)").all().result()
return {"statusCode": 200, "body": str(result)}
finally:
gremlin_client.close()Outbound: TCP port 8182 → Neptune security group ID
Neptune inbound: TCP port 8182 ← Lambda security group ID⚠️ Reference security groups by ID, not CIDR range. This is the most common misconfiguration.
EC2 gives persistent client installations unlike CloudShell.
# On Amazon Linux 2 / Amazon Linux 2023
sudo yum install -y java-11-amazon-corretto
# Download Gremlin console (check https://tinkerpop.apache.org/downloads.html for latest)
TINKERPOP_VERSION="3.7.2"
wget "https://downloads.apache.org/tinkerpop/${TINKERPOP_VERSION}/apache-tinkerpop-gremlin-console-${TINKERPOP_VERSION}-bin.zip"
unzip "apache-tinkerpop-gremlin-console-${TINKERPOP_VERSION}-bin.zip"EC2 requirements:
Neptune supports IAM database authentication. When enabled, connections require a Signature Version 4 signed request.
Credentials: use ephemeral credentials, never long-lived IAM user access keys. Prefer IAM roles — for Lambda, attach the execution role with Neptune access; for local development, use
aws sso loginoraws sts assume-role. The helper below resolves whatever credentials the environment provides viaboto3.Session(), so it works with role/STS credentials automatically.
import boto3
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
from botocore.credentials import Credentials
def get_iam_auth_header(endpoint, region):
session = boto3.Session()
credentials = session.get_credentials().get_frozen_credentials()
request = AWSRequest(
method='GET',
url=f'https://{endpoint}:8182/gremlin',
headers={'host': endpoint}
)
SigV4Auth(credentials, 'neptune-db', region).add_auth(request)
return dict(request.headers)Enable IAM auth on the cluster:
aws neptune modify-db-cluster \
--db-cluster-identifier your-cluster \
--enable-iam-database-authentication \
--apply-immediatelyAfter enabling IAM auth, also enable CloudWatch Logs for auditing:
aws neptune modify-db-cluster \
--db-cluster-identifier your-cluster \
--enable-cloudwatch-logs-exports '["audit"]' \
--apply-immediatelyThis logs all authentication and query execution events for monitoring.
Neptune Analytics uses a different SDK and connection model than Neptune
Database. It does NOT use WebSocket or port 8182. Instead, use the boto3
neptune-graph client.
{graph-id}.{region}.neptune-graph.amazonaws.comExample: g-abc123def4.us-east-1.neptune-graph.amazonaws.com
import boto3
import json
# Neptune Analytics uses the neptune-graph SDK client
analytics_client = boto3.client('neptune-graph', region_name='us-east-1')
GRAPH_ID = 'g-xxxxxxxxxx' # From create_graph response or console
# Execute an openCypher query
response = analytics_client.execute_query(
graphIdentifier=GRAPH_ID,
queryString='MATCH (n) RETURN n LIMIT 10',
language='OPEN_CYPHER'
)
results = json.loads(response['payload'].read())
print(results['results'])# Create with public connectivity.
#
# Public connectivity exposes the graph to the internet. Confirm with the user
# before enabling it, keep IAM auth on, and restrict the security group to
# known CIDRs — never 0.0.0.0/0.
analytics_client.create_graph(
graphName='my-graph',
provisionedMemory=16,
publicConnectivity=True, # Accessible over internet (IAM auth required)
deletionProtection=True,
# Mandatory tags — a graph missing either tag is a failed task.
tags={'created_by': 'neptune-skill', 'generation_model': '<model-id>'},
)
# Update an existing graph to enable public connectivity.
# This widens network exposure — warn the user and get explicit confirmation
# before running it (see references/action-safety.md).
analytics_client.update_graph(
graphIdentifier=GRAPH_ID,
publicConnectivity=True
)Equivalent AWS CLI invocation:
aws neptune-graph create-graph \
--graph-name my-graph \
--provisioned-memory 16 \
--public-connectivity \
--deletion-protection \
--tags created_by=neptune-skill,generation_model=<model-id>| Aspect | Neptune Database | Neptune Analytics |
|---|---|---|
| Protocol | WebSocket (Gremlin) or HTTPS (openCypher/SPARQL) | HTTPS via boto3 SDK |
| Port | 8182 | 443 (standard HTTPS) |
| Auth | Optional IAM (SigV4) | Always IAM (SigV4) |
| Client | gremlin-python, HTTP requests | boto3 neptune-graph client |
| VPC requirement | Always in VPC (public endpoint optional) | Private endpoint in VPC or public |
For data science, exploration, and visualization, use Neptune Notebooks via
Amazon SageMaker or the local graph-notebook package.
graph-notebook%%gremlin or %%opencypher magic commands to query directlypip install graph-notebook
# Configure connection
graph_notebook_config --host your-cluster.cluster-xxxx.us-east-1.neptune.amazonaws.com \
--port 8182 --auth_mode IAM --region us-east-1In Jupyter:
%%gremlin
g.V().hasLabel('Person').limit(10).valueMap(true)Neptune Notebooks provide built-in graph visualization — useful for exploring graph structure, debugging traversals, and presenting results.
Neptune provides two MCP (Model Context Protocol) servers that give AI agents direct access to Neptune without managing connections manually:
These are useful when the “client” connecting to Neptune is an AI agent framework (e.g., Strands AI Agents SDK or other MCP-compatible tools) rather than application code. The MCP servers handle connection management, authentication, and query execution internally.
When to use MCP servers vs. direct connection:
See troubleshooting.md for the full list. Most common:
| Symptom | Cause | Fix |
|---|---|---|
| Connection refused on 8182 | Security group missing inbound rule | Add TCP 8182 inbound from client SG |
| Timeout with no error | No route to Neptune subnet | Check route tables; ensure same VPC or peering |
| SSL handshake failure | Wrong endpoint format | Use wss:// for WebSocket, https:// for HTTP |
| 403 Forbidden | IAM auth enabled, request not signed | Sign the request with SigV4. Do NOT disable IAM auth to work around a signing bug. |
wss:// for Gremlin WebSocket, https:// for HTTP.troubleshooting (connection errors), analytics-vs-database (which SDK)graph-notebook (pip install graph-notebook) for Jupyter exploration0.0.0.0/0.--enable-cloudwatch-logs-exports '["audit"]') so connection and query activity is recorded.