I'm having trouble doing some very basic error handling in Lambda to identify connection issues with some databases (ElasticSearch and Neo4J).
I'm using a fake IP to force a connection error.
When I run the code locally I get the expected connection error, but in Lambda, I get nothing, only a timeout. I already increased the timeout to 90s to try and pickup the connection failure but it doesn't work. Anyone has ideas for me?
This is my code:
from py2neo import Graph
from elasticsearch import Elasticsearch
ip_local = '10.23.154.89'
ES_HOST = ip_local
ES_PORT = '9200'
NEO4J_HOST = ip_local
NEO4J_PORT = '7687'
ES_AUTH = None
NEO4J_AUTH = ('neo4j', 'test')
print('Iniciando conexao com ElastiSearch')
try:
es = Elasticsearch(
['http://'+ES_HOST+':'+ES_PORT],
http_auth=ES_AUTH,
)
except Exception as e:
es = None
print('ERROR:')
print(e)
print('Iniciando conexao com Neo4J')
try:
data_base_connection = Graph(uri = 'bolt://'+NEO4J_HOST+':'+NEO4J_PORT, auth=NEO4J_AUTH)
except Exception as e:
data_base_connection = None
print('ERROR:')
print(e)
Running on Windows locally I get this:
(venv) C:\Users\cdecher\Desktop\Code\amundsen-export>python lambda_function.py
Iniciando conexao com ElastiSearch
Iniciando conexao com Neo4J
ERROR:
Cannot open connection to ConnectionProfile('bolt://10.23.154.89:7687')
Traceback (most recent call last):
File "lambda_function.py", line 4, in <module>
from BD import es, data_base_connection
ImportError: cannot import name 'data_base_connection' from 'BD' (C:\Users\cdecher\Desktop\Code\amundsen-export\BD.py)
On Lambda all I get is this, which doesn't help me know what is going on:
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
Iniciando conexao com ElastiSearch
Iniciando conexao com Neo4J
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
Iniciando conexao com ElastiSearch
Iniciando conexao com Neo4J
START RequestId: fbea12db-2a57-4930-90b1-2c7c32da11d5 Version: $LATEST
2022-11-04T16:31:29.357Z fbea12db-2a57-4930-90b1-2c7c32da11d5 Task timed out after 90.10 seconds
Ok, after some debugging, trials and errors I managed to find a "solution". I added some code using socket to test the connection before attempting to use py2neo (which, as it seems, has no timeout treatment whatsoever in its methods).
Here's my testing code, which works both locally and on Lambda:
But I'm still bugged. I found a workaround but still haven't figured out why this happens. Why do I get a connection error locally but on Lambda it just keeps waiting for the connection forever until the Lambda timeout?