2

I was testing a simple fastapi backend by deploying it on aws ec2 instance. The service runs fine in the default port 8000 in the local machine. But as I ran the script on the ec2 instance with uvicorn main:app --reload it ran just fine with following return

INFO:     Will watch for changes in these directories: ['file/path']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [4698] using StatReload
INFO:     Started server process [4700]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Then in the ec2 security group configuration, the TCP for 8000 port was allowed as shown in the below image.

ec2 security group port detail

Then to test and access the service I opened the public ipv4 address with port address as https://ec2-public-ipv4-ip:8000/ in chrome.

But there is no response whatsoever.

The webpage is as below

result webpage

The error in the console is as below

VM697:6747 crbug/1173575, non-JS module files deprecated.
(anonymous) @ VM697:6747

The fastapi main file contains :->

from fastapi import FastAPI, Form, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.encoders import jsonable_encoder
import joblib 
import numpy as np
import os
from own.preprocess import Preprocess
import sklearn


col_data = joblib.load("col_bool_mod.z")
  
app = FastAPI()

@app.get("/predict")
async def test():
    return jsonable_encoder(col_data)


@app.post("/predict")
async def provide(data: list):
    print(data)
    output = main(data)
    return output


def predict_main(df):
    num_folds =  len(os.listdir("./models/"))
    result_li = []
    for fold in range(num_folds):
        print(f"predicting for fold {fold} / {num_folds}")
        model = joblib.load(f"./models/tabnet/{fold}_tabnet_reg_adam/{fold}_model.z")
        result = model.predict(df)
        print(result)
        result_li.append(result)
    return np.mean(result_li)

def main(data):
    df = Preprocess(data)
    res = predict_main(df)
    print(res)
    return {"value": f"{np.float64(res).item():.3f}" if res >=0 else f"{np.float64(0).item()}"}

The service runs fine with same steps in the react js frontend using port 3000 but the fastapi on 8000 is somehow not working.

Thank You for Your Patience

I wanted to retrieve the basic api reponses from the fastapi-uvicorn server deployed in an aws ec2 instance. But there is no response with 8000 port open and ec2 access on local ipv4 ip address.

  • This answer does explain how to connect the server within the same local area network but my issue is to connect the server using internet using public ipv4 address. – DEBASISH MOHANTY Feb 16 '23 at 06:19
  • By the way I added all pulic, private and localhost ips to the origins in the main.py file – DEBASISH MOHANTY Feb 16 '23 at 06:23
  • One way the problem is fixed is by assigning public ipv4 address followed by port 3000 in the CORS origin. But the issue is to hide the get request data on the browser that is accessed by 8000 port. – DEBASISH MOHANTY Feb 17 '23 at 10:12

0 Answers0