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.
Agile Planning using spreadsheet and tickets.
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
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 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'
- Configure Azure devops with github.
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
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
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
-
Passing tests after running the
make all
command from theMakefile
-
Running Azure App Service from Azure Pipelines automatic deployment
-
Successful prediction from deployed flask app in Azure Cloud Shell.
After finish your job you can destroy your infrastructure running:
$ terraform destroy
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.