Deploying a Machine Learning Model Using Flask, Gunicorn and Nginx on AWS

Introduction

Deploying machine learning models into production is a critical step in the ML lifecycle that allows data science teams to deliver real business value from their models. However, model deployment can be complex, requiring integration with web frameworks, application servers, and other infrastructure components.

In this article, we‘ll walk through a step-by-step process for deploying a machine learning model using the popular Python web framework Flask, along with Gunicorn and Nginx, on Amazon Web Services (AWS). This stack provides a scalable, performant and secure architecture for exposing machine learning models as web services.

We‘ll cover the key concepts and components involved, provide a detailed tutorial with code examples, and share best practices to ensure your model is production-ready. Whether you‘re a data scientist or ML engineer, by the end of this guide you‘ll be equipped with the knowledge to deploy your own models using this powerful stack.

Why Flask for Machine Learning Models?

Flask is a lightweight and flexible Python web framework that is well-suited for developing and deploying machine learning models. Some key benefits of Flask for ML include:

  1. Easy to get started – Flask has a simple and intuitive API, so you can quickly build web services around your trained models without a steep learning curve.

  2. Extensible – Flask supports a wide ecosystem of extensions for common tasks like database integration, user authentication, caching, and more. You can easily add functionality to your model-serving API as needed.

  3. Integrate with data science tools – Flask plays nicely with popular data science libraries like numpy, pandas, scikit-learn, and tensorflow, making it straightforward to load trained models and make predictions.

  4. Lightweight – As a micro web framework, Flask has minimal overhead which helps keep your model inference fast and resource-efficient.

  5. Testing – Flask provides a built-in development server and testing utilities to validate your model API before deploying to production.

While Flask can be used for large-scale production applications, it‘s especially good for ML models where performance is critical and you want a simple, no-frills way to expose your models to other systems over a REST API. Let‘s take a look at how to deploy Flask-based ML models into production.

Architecture Overview

Here‘s a high-level architecture diagram showing how the components fit together when deploying a Flask-based machine learning service on AWS:

[Architecture Diagram: Flask App -> Gunicorn -> Nginx -> AWS EC2]

The key components are:

  • Flask application – This contains your trained machine learning model and a web service API (e.g. REST endpoints) for making predictions. Flask handles the incoming HTTP requests, runs them through the model, and returns the inference results.

  • Gunicorn – A Python WSGI HTTP server that runs your Flask application and handles serving multiple concurrent requests. Gunicorn acts as an application server, taking incoming web requests from Nginx and distributing them across multiple worker processes for better performance.

  • Nginx – An open-source web server and reverse proxy that sits in front of Gunicorn and handles tasks like load balancing, HTTP caching, serving static files, and SSL termination. Nginx efficiently routes web traffic to the Gunicorn server.

  • AWS EC2 – The cloud compute service where your Flask application will be deployed. EC2 provides scalable, pay-as-you-go servers for running web applications and services.

Now that we have an overview of the architecture, let‘s walk through the process of deploying a Flask machine learning application on AWS EC2 with Gunicorn and Nginx.

Step 1: Set Up AWS EC2 Instance

The first step is launching a new EC2 instance that will host your Flask application. To do this:

  1. Login to the AWS Management Console and navigate to the EC2 dashboard.
  2. Click "Launch Instance" and select an Amazon Machine Image (AMI), such as Amazon Linux 2 or Ubuntu Server.
  3. Choose an instance type with sufficient resources for your application‘s needs. For lighter models, a t2.micro may suffice.
  4. Configure the instance details like VPC, subnet, IAM role, etc. Add a Name tag to identify it.
  5. Configure the security group to allow inbound traffic on port 80 (HTTP) and port 22 (SSH). You can lock it down further later.
  6. Review and launch the instance. Create a new key pair or use an existing one so you can SSH into the instance.

Once the EC2 instance is provisioned, you can SSH into it using the key pair and public DNS name. For example:

ssh -i mykey.pem [email protected]

Step 2: Install Dependencies

With the EC2 instance up and running, the next step is to install the necessary packages and dependencies for running Flask with Gunicorn and Nginx.

Update the package manager and install Python 3, pip, Nginx, and other tools:

sudo yum update
sudo yum install python3 python3-devel python3-pip nginx git

Pip install Flask, Gunicorn, and any other dependencies required for your ML model:

sudo pip3 install flask gunicorn tensorflow pandas scikit-learn

Step 3: Transfer Flask Application Code

Next, transfer the code for your Flask machine learning application to the EC2 instance. There are a few ways to do this, such as pushing the code to a Git repository and cloning it on the server:

git clone https://github.com/yourusername/flask-ml-model.git
cd flask-ml-model

Alternatively, you can use scp to securely copy the files from your local machine to the server:

scp -i mykey.pem -r flask-ml-model [email protected]:~/

The application code should include the necessary files for your Flask app, such as:

  • app.py – The main Flask application file
  • model.pkl (or similar) – Your serialized trained model
  • requirements.txt – A list of Python dependencies
  • Any templates or static assets used in the Flask app

Step 4: Configure Python Environment

It‘s a good practice to run Python applications in isolated virtual environments. Create a new virtual environment for your Flask app and activate it:

python3 -m venv venv
source venv/bin/activate

Install the Python dependencies in the virtual environment:

pip install -r requirements.txt

This ensures your app has all the packages it needs without conflicting with the system Python installation.

Step 5: Configure and Run Gunicorn

Gunicorn is a Python WSGI HTTP server that will run your Flask application and handle serving requests. To configure it, create a new file called gunicorn.conf.py in your app directory:

# gunicorn.conf.py
command = ‘/home/ec2-user/flask-ml-model/venv/bin/gunicorn‘
pythonpath = ‘/home/ec2-user/flask-ml-model‘
bind = ‘127.0.0.1:8000‘
workers = 3

This tells Gunicorn where to find the Gunicorn executable, your Flask app code, to listen on localhost port 8000, and to spawn 3 worker processes.

Next, create a systemd service file to allow Gunicorn to run as a service. Create a file called flask-ml-model.service in the /etc/systemd/system directory:

[Unit]
Description=Gunicorn instance to serve Flask ML model
After=network.target

[Service]
User=ec2-user
Group=www-data
WorkingDirectory=/home/ec2-user/flask-ml-model
Environment="PATH=/home/ec2-user/flask-ml-model/venv/bin"
ExecStart=/home/ec2-user/flask-ml-model/venv/bin/gunicorn --config gunicorn.conf.py app:app

[Install]
WantedBy=multi-user.target

This service file tells systemd to run Gunicorn with the config file we created and serve the Flask app object found in app.py.

Finally, start the Gunicorn service:

sudo systemctl start flask-ml-model
sudo systemctl enable flask-ml-model

Your Flask application is now running under Gunicorn! However, we still need to set up Nginx to act as a reverse proxy and handle web traffic.

Step 6: Configure Nginx

Nginx will sit in front of Gunicorn to efficiently handle incoming HTTP requests and route them to the application server. It also takes care of tasks like serving static files and SSL termination.

Create a new Nginx configuration file for your Flask app:

sudo nano /etc/nginx/conf.d/flask-ml-model.conf

Add the following configuration:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /static {
        alias /home/ec2-user/flask-ml-model/static; 
    }
}

This configuration tells Nginx to listen on port 80 and forward requests to the Gunicorn server running on localhost port 8000. It also sets some proxy headers and defines a location block for serving static files if your app has any.

Test the Nginx configuration and restart the service:

sudo nginx -t
sudo systemctl restart nginx

At this point, your Flask machine learning application is deployed and accessible over the public internet!

Securing the Application with SSL

To encrypt traffic and secure communication with your ML web service, it‘s strongly recommended to configure SSL/HTTPS using a tool like Let‘s Encrypt and Certbot.

First, install Certbot and the Nginx plugin:

sudo amazon-linux-extras install epel 
sudo yum install certbot-nginx

Next, run Certbot and follow the prompts to obtain an SSL certificate for your domain:

sudo certbot --nginx -d your-domain.com

Certbot will automatically update your Nginx configuration to use the new SSL certificate. Verify that your application is accessible over HTTPS.

Conclusion and Next Steps

Congratulations! You now have a production-ready deployment of a Flask machine learning model using Gunicorn and Nginx on AWS. This architecture provides a scalable and performant foundation for serving predictions from your trained models.

Some additional considerations and next steps:

  • Set up AWS auto scaling and load balancing to handle high traffic loads
  • Monitor your application using tools like AWS CloudWatch or Datadog
  • Implement user authentication and a rate limiting to secure your model API
  • Use infrastructure-as-code tools like AWS CloudFormation or Terraform to automate provisioning
  • Package your model service using containers (e.g. Docker) for easier deployment and portability
  • Continuously retrain and update your model on new data

I hope this guide was helpful for learning how to deploy machine learning models into production with Flask, Gunicorn, Nginx, and AWS! Let me know if you have any other questions.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts