Meritshot Tutorials

  1. Home
  2. »
  3. Deploying on Heroku

Flask Tutorial

Deploying on Heroku

Heroku is a popular cloud platform that allows developers to build, run, and scale applications quickly without having to manage infrastructure. It simplifies the deployment process, making it an ideal choice for small to medium Flask applications, especially for developers who are new to cloud deployment.

Why Use Heroku for Flask Deployment?

  1. Ease of Use:
    • Heroku provides a simple command-line interface (CLI) to deploy and manage applications. It’s designed to be user-friendly, with minimal configuration needed.
  2. Free Tier:
    • Heroku offers a free tier that is perfect for personal projects, prototypes, or small-scale applications. This tier includes 550-1000 dyno hours per month, which is enough for small traffic applications.
  3. Scalability:
    • Although the free tier has limited resources, Heroku also provides paid plans that allow you to scale your app easily as your needs grow.
  4. Automatic Deployment:
    • Heroku can integrate with GitHub or GitLab, enabling automatic deployment whenever you push updates to your code repository.

Steps to Deploy Flask Application on Heroku

  1. Prepare Your Flask Application
    • Before deploying to Heroku, ensure your Flask app is ready for production. You may need to make some modifications or additions, such as:
    • requirements.txt: This file lists all the dependencies required for your app. You can generate it by running:

pip freeze > requirements.txt

Heroku will automatically install all packages listed here when deploying the app.

    • Procfile: This file tells Heroku how to run your app. Create a Procfile in the root of your project and add the following line:

web: python app.py

Replace app.py with the name of your main application file.

    • runtime.txt (Optional): This file specifies the Python version to use on Heroku. For example, if you’re using Python 3.9, add the following line:

python-3.9.5

    • Handle Static Files: If your app includes static files (e.g., images, CSS), you may need to set up an additional step to handle static file serving. Heroku uses a read-only file system, so ensure your static files are stored in an appropriate location.
  1. Set Up a Git Repository
    • Heroku relies on Git for deployment, so ensure your Flask app is inside a Git repository. If you haven’t already initialized a repository, do so by running the following commands:

git init

git add .

git commit -m “Initial commit”

  1. Create a Heroku Account and Install Heroku CLI
    • Sign up for a free Heroku account at heroku.com.
    • Install the Heroku CLI by following the instructions for your operating system here.
  2. Log in to Heroku CLI
    • Once the Heroku CLI is installed, log in to your Heroku account:

heroku login

This will open a web page where you can authenticate.

  1. Create a New Heroku App
    • To create a new Heroku app, run the following command:

heroku create

This will generate a new app with a random name (e.g., frosty-basin-12345) and associate it with your Git repository.

  1. Deploy the App to Heroku
    • Once the app is created, deploy it using Git by running:

git push heroku master

Heroku will detect your Flask app, install dependencies, and run your application.

  1. Open the Flask App in Your Browser
    • After the app is deployed, you can open it in a web browser with the following command:

heroku open

  1. Monitor and Scale Your App
    • Heroku offers a simple dashboard and CLI commands to monitor and manage your app. You can view logs, scale your app, and update your configuration with ease.
    • To view the logs:

heroku logs –tail

    • To scale your app (e.g., increase dynos):

heroku ps:scale web=1

Best Practices for Deploying Flask on Heroku

  1. Use Environment Variables for Configuration:
    • Store sensitive data such as API keys, database credentials, or machine learning model paths in environment variables rather than hardcoding them. You can set environment variables in Heroku using the CLI:

heroku config:set API_KEY=your_api_key

  1. Database Integration:
    • If your Flask app requires a database, Heroku offers managed databases like PostgreSQL. You can provision a database directly on Heroku and connect it to your app.
  2. SSL for Secure Connections:
    • Heroku provides free SSL for custom domains. Ensure your app is served over HTTPS to protect user data and ensure secure connections.
  3. Log Monitoring:
    • Use Heroku logs to monitor your app for issues. Regularly check logs for error messages and performance bottlenecks.

Frequently Asked Questions

  1. What is a “dyno” on Heroku?
    • A dyno is a lightweight container used by Heroku to run your app. It provides the computing resources to execute your application. You can scale your app by increasing the number of dynos.
  2. Can I use a custom domain on Heroku?
    • Yes, you can configure a custom domain with Heroku. After setting up your domain, you can add it to your Heroku app using the dashboard or the CLI.
  3. How do I manage environment variables on Heroku?
    • You can manage environment variables using the Heroku CLI with the command heroku config:set VARIABLE_NAME=value. You can also access them in your Flask app using os.environ.get(‘VARIABLE_NAME’).
  4. Does Heroku provide database hosting?
    • Yes, Heroku provides managed databases like PostgreSQL. You can easily add a database to your app by running heroku addons:create heroku-postgresql:hobby-dev.
  5. Is there a cost for using Heroku?
    • Heroku offers a free tier, which includes 550-1000 dyno hours per month. However, for production-level applications or if you need more resources, you may need to switch to a paid plan.