Challenges with Deploying Streamlit Apps on AWS Elastic Beanstalk

Hello

I am facing some issues while trying to deploy my Streamlit app on AWS Elastic Beanstalk. The app works perfectly in my local environment but once deployed; I face challenges such as connection errors, environment variable misconfigurations & difficulty in setting up the requirements.txt file correctly for the environment. :upside_down_face:

I have tried tweaking configurations based on several guides but the app either crashes on start / shows a generic error page.

One specific issue seems to revolve around the correct version of Python & compatibility with the streamlit library. Even after specifying the Python version and updating my packages; I still face compatibility warnings. Additionally; I am unsure how to correctly set up the Elastic Beanstalk environment to handle Streamlit’s web server properly; especially with custom ports. :innocent:

If anyone has successfully deployed a Streamlit app on AWS Elastic Beanstalk, I would love to know your approach, including how you handled environment variables, server port configurations & ensuring dependencies are resolved without errors. :upside_down_face:
I have checked HOWTO: Deploying Streamlit app to AWS Elastic Beanstalk (no containers) Java guide for reference .

Any examples, tutorials and step-by-step instructions would be immensely helpful!

Thank you ! :slightly_smiling_face:

To deploy your Streamlit app on AWS Elastic Beanstalk (EB), you need to consider how Elastic Beanstalk interacts with Python applications and how Streamlit, as a non-WSGI framework, fits into this environment.

Understanding Elastic Beanstalk and Streamlit

AWS Elastic Beanstalk is a platform that simplifies deploying and managing applications. For Python environments, EB typically expects a WSGI-compatible application (e.g., Flask or Django) , which it runs using a WSGI server such as Gunicorn.

However, Streamlit operates differently. It’s not a WSGI application; instead, it runs its own web server, typically started with the command streamlit run app.py. This mismatch means that EB’s default behavior—looking for a WSGI entry point—won’t work directly with Streamlit. Fortunately, EB allows you to override this default behavior using a Procfile, which lets you specify a custom command to run your application.

Deploying Streamlit on Elastic Beanstalk

Since Streamlit isn’t WSGI-compatible, the simplest and most recommended approach is to use a Procfile to tell Elastic Beanstalk how to run your Streamlit app. Here’s how you can do it:

  1. Keep Your Streamlit App in app.py

    • The Streamlit code you provided should be saved as app.py. This file contains the core logic of your dashboard and is the main application you want to deploy.
  2. Create a Procfile

    • Add a file named Procfile (no extension) in the root directory of your project with the following content:
      web: streamlit run app.py --server.port=$PORT --server.headless=true
      
    • Explanation:
      • web: tells EB that this is the command for the web process.
      • streamlit run app.py is the standard command to start a Streamlit app.
      • --server.port=$PORT ensures Streamlit listens on the port assigned by EB, which is stored in the PORT environment variable (EB dynamically assigns this, often 5000, but should be 8501).
      • --server.headless=true is necessary because EB runs in a headless environment (no GUI), and Streamlit needs this flag to function properly.
  3. Install Dependencies

    • Create a requirements.txt file listing your dependencies:
      streamlit
      numpy
      matplotlib
      
    • EB will install these when deploying your application.
  4. Directory Structure
    Your project should look like this:

    your-app/
    ├── app.py          # Your Streamlit app
    ├── Procfile        # Custom command for EB
    ├── requirements.txt # Dependencies
    
  5. Create a options.config

    • Create a .ebextensions folder inside your project with the file options.config:
    option_settings:
     aws:elasticbeanstalk:container:python:
       WSGIPath: app:main
     aws:elasticbeanstalk:application:environment:
       PORT: 8501
    
  6. Deploy to Elastic Beanstalk

    • Zip your project directory and upload it to EB via the AWS console or CLI (eb deploy).
    • EB will use the Procfile to start your Streamlit app, and the app will be accessible via the EB-provided URL.

Final Recommendation

For your use case—deploying a Streamlit dashboard—stick with the Procfile approach:

  • Use app.py as your Streamlit app.
  • Add a Procfile with web: streamlit run app.py --server.port=$PORT --server.headless=true.
  • Include requirements.txt for dependencies.
  • Create options.config file for environment variables.

This setup ensures your Streamlit app runs correctly and is accessible via the EB URL.