This is an example for deploying an R-based inferencing web service to an Azure Machine Learning Managed Online Endpoint. It has been inspired by an example from the Azure Machine Learning team here. You can use this repo to directly deploy to Azure from RStudio.
In case you are interested, I have written a companion article on Medium here.
To run the code, you need
- this repository cloned or downloaded to your local machine
- optional: RStudio for editing/running R scripts
- PowerShell Core, runs on Windows, Linux and Mac
- Docker running locally (not required when using a Compute Instance or an Ubuntu Data Science Virtual Machine (DSVM) where Linux Docker is pre-installed already)
- an Azure Machine Learning workspace incl. sufficient permissions
- an Azure CLI
- version 2 of AML's CLI extension installed, for details see here.
-
Install all required prerequisites (see above).
-
Adjust the code/files in folder
model-training
to your needs using the editor of your choice. You can do everything you want/need to in that folder. Only requirement is that you save your final model(s)/files in thewebservice
folder after training completion.Tip: If your local machine is too slow for training the model, try the RStudio pre-installed in an Azure Machine Learning Compute Instance or a Data Science Virtual Machine (DSVM). With the hardware power provided there, you can train your models much faster. For an overview of available machine sizes in Azure, see here.
-
Edit the
webservice/plumber.R
file to fit to your needs. If you use RStudio, you can directly test your plumber script from there. -
Edit the
sample_request.json
file and give it an example JSON document that is passed to your web service for testing. -
Modify the
Dockerfile.template
file as needed. For example, you may need to edit this file for installing additional R packages. -
Edit the
config.psd1
file and update its settings as needed. -
Open a PowerShell Core terminal and navigate to this folder. If you are using RStudio, you can use a terminal directly in RStudio.
-
Run the
deploy.ps1
script (from the directory where it is located). The script will then:- Ask you to confirm your configuration settings.
- Build and start a local Docker image which contains your web service.
- Ask you if you want to deploy the image to an Azure Machine Learning Managed Online Endpoint.
- If yes, deploy the model and the container.
- Show web service URI (and password if comment in code is removed).
- Run another test. This time against the web service in Azure.
- By passing a
-AcceptConfiguration
and/or-DeployToAzure
switch, you can bypass the user confirmations.
Note: you can configure RStudio to use PowerShell in its terminal. That way, you can deploy your web service entirely from RStudio.
The code provided here does not yet support AML MOE's autoscaling features. To get autoscaling, you need to extend the code or configure the autoscaling feature in AML manually.
By default, PowerShell runs only signed scripts. To run the provided script, you may need to re-configure PowerShell first. This is a one-time action.
Quick fix: Run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
in PowerShell (requires admin privileges).
More details about execution policies can be found here
For details on Azure Machine Learning, see the documentation here.
As always, everything provided here is provided "as is". Feel free to use but don't blame me if things go wrong.
I am open for pull requests. Please submit a pull request before starting your own public fork so we can keep things together.