Forums

I cannot connect to my ec2 hosted api from pythonanywhere

I host an ollama (https://ollama.com/) api on an ec2 instance. I would like to access it from my pythonanywhere server, but cannot.

I have set up the necessary inbound rules on ec2 instance to allow the ip address associated with my pythonanywhere URL.

I have tested the ollama api from my laptop. I enabled my home IP address in the inbound rules on the ec2 instance and it works fine.

I have also tested whether I can do any TCP connection to the ollama port (11434). I stop the ollama server, and I set netcat to listen on this port. I am able to connect to this port with netcat from my laptop successfully. I cannot connect to this port with netcat from the pythonanywhere instance.

There are two possibilities here:

  1. either I am consistently mistyping the pythonanywhere IP address in the inbound security rule on AWS (I may be, but this seems low probability)
  2. there is some reason why I cannot access the instance from my pythonanywhere instance.

Also worth noting that I am paying for a Hacker account already, and I understand this allows me to connect to external services.

Okay, so what's probably happening here, is that the IP address you use for your security group ingress rules is not the IP address of the machine where your web app is running. How did you find the IP address to use for your security group rules?

OK got it, this was the issue. I was getting the IP of the machine by pinging the URL. This is not the IP that the requests are coming from when the machine send requests to AWS. Instead, one must get the IP address by running a (python) script like the following

import requests
ip = requests.get("http://ipinfo.io/ip").content.strip()
print(f'your IP is {ip}')

[formatted by admin]

Thanks!

Glad to hear that!