Search code examples
pythonflaskdeploymentamazon-elastic-beanstalk

"500 internal servor error" when using a Flask application deployed by AWS Elastic Beanstalk


Homepage of my Flask application is rendered correctly, but the issue arises when I click a button (submit) which takes me to another endpoint where instead of rendering that page, I get an error saying, "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."

I tried to referring to various sources, but couldn't come up with a solution


Solution

  • After several hours, I resolved it and there were 2 issues:

    1. The path specified in my python file (predict_pipeline.py): Used backslashes (‘\’) to separate the dirs, which is the convention for file paths in Windows. However, when deploying application on AWS Elastic Beanstalk, the underlying operating system in Linux which uses forward slashes (‘/’).

    Sol: modify the file paths using os.path.join(), which is a platform independent way of joining directory and file names. Change “artifacts/model.pkl” to os.path.join(“artifacts”, “model.pkl”)

    1. The version of scikit-learn==1.3.0 used in our flask application doesn’t match with the scikit-learn==1.2.2 version used by Elastic Beanstalk.

    Sol: change scikit-learn version in our virtual environment by specifying scikit-learn==1.2.2 in the requirements.txt file in our project directory and install it using pip install -r requirements.txt