# Deploy Model

## Step to deploy the model

#### Clone the repository <a href="#clone-the-sample-repository" id="clone-the-sample-repository"></a>

#### Set an endpoint name <a href="#set-an-endpoint-name" id="set-an-endpoint-name"></a>

```
export ENDPOINT_NAME="<YOUR_ENDPOINT_NAME>"
```

#### Create deployment configuration yml file&#x20;

```
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json
name: blue
endpoint_name: my-endpoint
model:
  path: ../../model-1/model/
code_configuration:
  code: ../../model-1/onlinescoring/
  scoring_script: score.py
environment: 
  conda_file: ../../model-1/environment/conda.yml
  image: mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20210727.v1
instance_type: Standard_DS2_v2
instance_count: 1
```

#### Register the model

* Extract the YAML definitions of `model` and `environment` into separate YAML files and use the commands `az ml model create` and `az ml environment create`. To learn more about these commands, run `az ml model create -h` and `az ml environment create -h`.

#### Deploy the model with online endpoint to Azure

* To create the endpoint in the cloud, run the following code

```
az ml online-endpoint create --name $ENDPOINT_NAME -f endpoints/online/managed/sample/endpoint.yml
```

* To create the example deployment named `blue` under the endpoint, run the following code:

```
az ml online-deployment create --name blue --endpoint $ENDPOINT_NAME -f endpoints/online/managed/sample/blue-deployment.yml --all-traffic
```

#### View the endpoint by navigating to Endpoints in Azure Machine Learning studio

#### Invoke the endpoint to score data with the model

* invoke command

```
az ml online-endpoint invoke --name $ENDPOINT_NAME --request-file endpoints/online/model-1/sample-request.json
```

* use curl to request REST API to score data&#x20;

```
SCORING_URI=$(az ml online-endpoint show -n $ENDPOINT_NAME -o tsv --query scoring_uri)

curl --request POST "$SCORING_URI" --header "Authorization: Bearer $ENDPOINT_KEY" --header 'Content-Type: application/json' --data @endpoints/online/model-1/sample-request.json
```

Reference about deployment :&#x20;

<https://learn.microsoft.com/en-us/azure/machine-learning/how-to-deploy-with-rest>

<https://learn.microsoft.com/en-us/azure/machine-learning/how-to-deploy-managed-online-endpoints?tabs=azure-cli>
