I am performing an http request to the LibreNMS API via the following Python code:
import requests
TOKEN = './token.json'
def get_request(request_type):
url = f'http://192.168.1.141/api/v0/{request_type}'
with open(TOKEN,'r') as file:
headers = json.load(file)
response = requests.get(url,headers=headers)
if response.status_code == 200:
pass
else:
print(f"Failed to pull for {request_type}: {response.status_code}")
return response.json()
if __name__ == '__main__':
dat = get_request('alerts') # pull alerts
The script runs perfectly on my local machine and quite fast. However when I try to deploy the scrip as a Google Cloud Function then requests.get(url,headers=headers)
seems to time out. In the cloud logs of the function I get:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/urllib3/connection.py", line 203, in _new_conn
sock = connection.create_connection(
"Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request("
"Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/requests/adapters.py", line 489, in send
resp = conn.urlopen("
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 2529, in wsgi_app
response = self.full_dispatch_request()"
I have the same versions of requests==2.28.1
and urllib3==1.26.14
installed on my local machine and in the requirements.txt
file of the cloud function. I thought the problem might lie here.
I am unsure if a cloud function can perform http requests to non-Google services. Perhaps this is the issue.
Please help me understand what is wrong with my function and/or deployment.
It seems that the issue lies within the fact that the address 192.168.1.141
was local. I have since replaced it with librenms.bctechnologies.co.za:8050
. However, it seems like this too is not public as response = requests.get(url,headers=headers)
keeps timing out.
Posting @Guy comment as answer wherein running LibreNMS as a better option to access privately. The purpose of this post is to guide other users for this concern.
Just to add, if by chance you wanted to continue to access your Cloud Function using GCP Cloud VPN you can do this by creating first a connector which is Serverless VPC access.
After the connector, the next step is by associating your Cloud Function to the connector that is created. You can do this by editing the Cloud Function and follow this steps in Configure Cloud Functions to use a connector.
Once Cloud Function is using the connector, the next step is to create a Cloud VPN. Just make sure that you will choose the network you use in your Serverless VPC access to have a connection to your Cloud Function.