Skip to content
Advertisement

Python + FastAPI + OracleCloud: How to expose my python fastapi endpoint on Oracle Cloud to Internet

I have a Python + FastAPI restful API project running the free tier of Oracle Cloud VM instance.

I use Gunicorn to serve the api and also installed Nginx just in case it’s needed.

I have tested my running project with

curl http://localhost:8000

and I can see my API response.

Now my question is : how can I expose this api endpoint outside on the Internet?


Update 1

I started my Python API project with this command:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --timeout 1200 -b 0.0.0.0

I saw the messages below:

[2021-05-23 00:40:28 +0000] [3850] [INFO] Starting gunicorn 20.0.2
[2021-05-23 00:40:28 +0000] [3850] [INFO] Listening at: http://0.0.0.0:8000 (3850)
[2021-05-23 00:40:28 +0000] [3850] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2021-05-23 00:40:28 +0000] [3853] [INFO] Booting worker with pid: 3853
[2021-05-23 00:40:28 +0000] [3854] [INFO] Booting worker with pid: 3854
[2021-05-23 00:40:28 +0000] [3857] [INFO] Booting worker with pid: 3857
[2021-05-23 00:40:28 +0000] [3858] [INFO] Booting worker with pid: 3858
[2021-05-23 00:42:04 +0000] [3853] [INFO] Started server process [3853]
[2021-05-23 00:42:04 +0000] [3857] [INFO] Started server process [3857]
[2021-05-23 00:42:04 +0000] [3857] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3858] [INFO] Started server process [3858]
[2021-05-23 00:42:04 +0000] [3858] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3858] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3853] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3853] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3857] [INFO] Application startup complete.
[2021-05-23 00:42:04 +0000] [3854] [INFO] Started server process [3854]
[2021-05-23 00:42:04 +0000] [3854] [INFO] Waiting for application startup.
[2021-05-23 00:42:04 +0000] [3854] [INFO] Application startup complete.

Then I copied the IP address from the Compute >> Instances >> Instance Details panel and accessed it from my Chrome. Straightaway, it shows me

Unable to connect

Also read through several articles about using Nginx and tried without any luck.


Update 2

Using curl to access the website from my local machine

$ curl http://168.138.12.192:8000/
curl: (7) Failed to connect to 168.138.12.192 port 8000: No route to host

However, when access the IP directly using curl, I was able to get the default Nginx website. $ curl http://168.138.12.192

Advertisement

Answer

Finally, I found out what I missed:

sudo iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 8000 -j ACCEPT 

I have to run this command to open the port 8000(yes, my website is using port 8000).

I thought I have added Ingress Rule to accept tcp 8000, but it turns out that I still need to run the aforementioned command.

I do not quite understand why I need to do it, but it solves the problem.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement