Lab 2 - Create the Function
Deploy your ONNX model in an Azure Function
1. Create the Azure Function
Run the func init
command back in the terminal window you have open, as follows, to create a functions project in a folder named SimpsonsFunctions with the Python runtime and navigate into the project folder.
This folder contains various files for the project, including configurations files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
Add a function to your project by using the following command, where the --name
argument is the unique name of your function (Classify) and the --template
argument specifies the function's trigger (HTTP).
func new
creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named function.json.
Open the file that contains the code for your function.
Replace the code in the function (Classify/__init__.py) with:
Copy the model to the function.
Open the requirements.txt. This files contains a list of packages that need to be installed in the Azure Function.
Add the following packages to the requirements.txt
2. Deploy the Azure Functions
Create the Azure Resources
Create a resource group for the Azure Function
Create a Storage Account. Replace <UNIQUE_NAME> with a unique name like simpsonfunc112
In the previous example, replace <STORAGE_NAME>
with a name that is appropriate to you and unique in Azure Storage. Names must contain three to 24 characters numbers and lowercase letters only. Standard_LRS
specifies a general-purpose account, which is supported by Functions.
Create the function app in Azure:
In the previous example, replace <APP_NAME>
with a globally unique name appropriate to you. The <APP_NAME>
is also the default DNS domain for the function app.
This command creates a function app running in your specified language runtime under the Azure Functions Consumption Plan, which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see Monitor Azure Functions. The instance incurs no costs until you activate it.
After you've successfully created your function app in Azure, you're now ready to deploy your local functions project by using the func azure functionapp publish command.
Publish the Azure Function
In the following example, replace <APP_NAME>
with the name of your app.
Test the published function
Copy the Invoke URL, paste it in your browser and add the query string below to the URL.
As a result, you should see:
Done! Well done, you have now successfully deployed a classification model, that you have created using the Azure Custom Vision Service, running in a Python Azure Function.
Last updated