Skip to content

In this project, I implemented a Machine Learning Microservice API with CI/CD using Python, GitHub Actions, Azure Pipelines and Terraform for infrastructure provisioning.

Notifications You must be signed in to change notification settings

rrm86/Agile_ML_Engineer

Repository files navigation

Python CI with Github Actions Terraform Build Status

Overview

A data science team built a model using scikit learn and exported a joblib file. They wish to share the prediction functions with the world through an API.

In this project, I implemented a Microservice strategy using flask and Azure web app to deliver an MVP.

Designed to be the single source of truth this repo has the code of the application and the code to build the infrastructure.

For application, I implemented a full CI/CD cycle using Github Actions (CI) and Azure DevOps (CD).

For infrastructure provisioning, I use terraform. A full CI/CD cycle was implemented using Github Actions(CI) and Terraform Cloud(CD).

The application code is in the main branch and the terraform code in the terraform branch.

Diagram

Project Plan

Agile Planning using spreadsheet and tickets.

Instructions

Prerequisites & Setup

Appication Code

The Code is written in Python 3.7 . If you don't have Python installed you can find it here.

  • Create a virtual environment
$ python3 -m venv <project-name>

And inside the project folder run:

 $ source bin/activate

Once your virtual environment is ready, you can go to the project root folder and run:

$ pip install -r requirements.txt

Or run:

$ make install

Insfrastructure Code

The Code is written in Terraform 0.14 . If you don't have Terraform installed you can find it here.

This project runs terraform with terraform cloud as backend. To run locally go to the terraform folder(terraform branch) and comment the block terraform on main.tf file.

Azure

  • Azure account
  • Azure CLI To authentic into azure cloud run and follow the instructions:
$ az login
  • To check your account run:
$ az account list
  • In order to perform changes on the azure subscription you need to create an Active Directory service principal account.
$ az ad sp create-for-rbac --skip-assignment
{
  "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
  "displayName": "azure-cli-20**-**-**-**-**-**",
  "name": "http://azure-cli-20**-**-**-**-**-**",
  "password": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
  "tenant": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
}

Copy the output. You'll be prompt to input this values when run terraform init/plan if you run locally! For terraform cloud backend, configure key->value on UI. For connection configurations between github actions and Terraform cloud check this link.

  • Create a resouce group for the project:
$ az group create --name Agile-ML-Engineering-Project-rg --location 'Your Region'

Execution

Locally

To run the flask application locally, go to the root folder and run:

$ python app.py

You can go to 0.0.0.0:5000 and see the home application. To test prediction run

You can test the prediciont running:

$ chmod +x make_predictons.sh
$ ./make_predictions.sh

Cloud

Running Terraform Locally

  • Build Cloud Infrastructure
    • Switch to the terraform branch and go to the terraform folder
    • Run: $ terraform init
    • Run: $ terraform plan
    • Run: $ terraform apply

Running Terraform Cloud

  • Go to your workspace and queue a plan or send a commit to the branch terraform terraform plan

To deploy the application on the new infrastructure, you can trigger the azure pipeline or send a push for the main branch.

You can test the prediciont running on Azure:

$ chmod +x make_predict_azure_app.sh
$ ./make_predict_azure_app.sh

or going to https://your-project-name.azurewebsites.net

Screenshots:

  • Project up and running running-on-app-service

  • Project cloned into Azure Cloud Shell clone-azure-shell

  • Passing tests after running the make all command from the Makefile lint-test

  • Successful deploy of the project in Azure Pipelines. deployed-pipeline.png.

  • Running Azure App Service from Azure Pipelines automatic deployment deploy

  • Successful prediction from deployed flask app in Azure Cloud Shell. output-prediction

  • Output of streamed log files from deployed application app-log

  • Load test with locust (100 users) terraform-destroy

After finish your job you can destroy your infrastructure running:

$ terraform destroy

terraform-destroy

Enhancements

The project is running well but, it's limited to Azure. To avoid this, you can run the project in a container(Docker/ Kubernetes) and adopt a MultiCloud Strategy.

For this purpose, I create a multi-cloud branch with scripts to run the app on containers.

Once we have containers, we can use terraform to manage our multi-cloud infrastructure.

Check out this nice post from Hans Joerg Wieland to understand how to implement it.

Demo

Project Screencast on YouTube

About

In this project, I implemented a Machine Learning Microservice API with CI/CD using Python, GitHub Actions, Azure Pipelines and Terraform for infrastructure provisioning.

Resources

Stars

Watchers

Forks