368 строки
10 KiB
Plaintext
368 строки
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Installation and configuration¶\n",
|
|
"\n",
|
|
"This notebook create Azure resources and configures the notebooks in this tutorial to connect to an Azure Machine Learning (AzureML) Workspace. You can use an existing workspace or create a new one."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import azureml.core\n",
|
|
"from azureml.core import Workspace\n",
|
|
"from dotenv import set_key, get_key, find_dotenv\n",
|
|
"from pathlib import Path\n",
|
|
"from testing_utilities import get_auth\n",
|
|
"import json\n",
|
|
"import time"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The AzureML Python SDK is already installed. Let's check the AzureML SDK version."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"SDK Version: 1.0.21\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(\"SDK Version:\", azureml.core.VERSION)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Replace the values in the following cell with your information. If you would like to use service principal authentication as described [here](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/manage-azureml-service/authentication-in-azureml/authentication-in-azure-ml.ipynb) make sure you provide the optional values as well."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"tags": [
|
|
"parameters"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Azure resources\n",
|
|
"subscription_id = \"<subscription_id>\"\n",
|
|
"resource_group = \"<resource_group>\" \n",
|
|
"workspace_name = \"<workspace_name>\"\n",
|
|
"workspace_region = \"resource_region\" # e.g. resource_region = \"eastus\"\n",
|
|
"\n",
|
|
"# IoT hub name - a globally UNIQUE name is required, e.g. iot_hub_name = \"myiothubplusrandomnumber\".\n",
|
|
"iot_hub_name = \"<iot_hub_name>\" \n",
|
|
"device_id = \"<device_id>\" # the name you give to the edge device. e.g. device_id = \"mygpudevice\"\n",
|
|
"\n",
|
|
"image_name = \"<image_name>\" # avoid underscore in names"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Create and initialize a dotenv file for storing parameters used in multiple notebooks."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"env_path = find_dotenv()\n",
|
|
"if env_path == \"\":\n",
|
|
" Path(\".env\").touch()\n",
|
|
" env_path = find_dotenv()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set_key(env_path, \"subscription_id\", subscription_id)\n",
|
|
"set_key(env_path, \"resource_group\", resource_group)\n",
|
|
"set_key(env_path, \"workspace_name\", workspace_name)\n",
|
|
"set_key(env_path, \"workspace_region\", workspace_region)\n",
|
|
"\n",
|
|
"set_key(env_path, \"image_name\", image_name)\n",
|
|
"\n",
|
|
"set_key(env_path, \"iot_hub_name\", iot_hub_name)\n",
|
|
"set_key(env_path, \"device_id\", device_id)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Create Azure Resources"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# login in your account\n",
|
|
"accounts = !az account list --all -o tsv\n",
|
|
"if \"Please run \\\"az login\\\" to access your accounts.\" in accounts[0]:\n",
|
|
" !az login -o table\n",
|
|
"else:\n",
|
|
" print(\"Already logged in\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Below we will reload it just to make sure that everything is working."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!az account set --subscription $subscription_id "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Create AzureML workspace\n",
|
|
"\n",
|
|
"To create or access an Azure ML Workspace, you will need the following information:\n",
|
|
"\n",
|
|
"* An Azure subscription id\n",
|
|
"* A resource group name\n",
|
|
"* A name for your workspace\n",
|
|
"* A region for your workspace\n",
|
|
"\n",
|
|
"The next cell will create an AzureML workspace for you in a subscription, provided you have the correct permissions.\n",
|
|
"This will fail when:\n",
|
|
"\n",
|
|
"1. You do not have permission to create a workspace in the resource group\n",
|
|
"2. You do not have permission to create a resource group if it's non-existing.\n",
|
|
"3. You are not a subscription owner or contributor and no Azure ML workspaces have ever been created in this subscription\n",
|
|
"\n",
|
|
"If workspace creation fails, please work with your IT admin to provide you with the appropriate permissions or to provision the required resources. If this cell succeeds, you're done configuring AzureML!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import the Workspace class and check the azureml SDK version\n",
|
|
"# from azureml.core import Workspace\n",
|
|
"\n",
|
|
"ws = Workspace.create(\n",
|
|
" name=workspace_name,\n",
|
|
" subscription_id=subscription_id,\n",
|
|
" resource_group=resource_group,\n",
|
|
" location=workspace_region,\n",
|
|
" create_resource_group=True,\n",
|
|
" auth=get_auth(),\n",
|
|
" exist_ok=True,\n",
|
|
")\n",
|
|
"# persist the subscription id, resource group name, and workspace name in aml_config/config.json.\n",
|
|
"ws.write_config()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Below we will reload it just to make sure that everything is working."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# load workspace configuratio from ./aml_config/config.json file.\n",
|
|
"ws = Workspace.from_config(auth=get_auth())\n",
|
|
"ws.get_details()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Create IoT Hub"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# install az-cli iot extension - I had to use \"sudo -i\" to make it work\n",
|
|
"!sudo -i az extension add --name azure-cli-iot-ext"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!az iot hub list --resource-group $resource_group -o table"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Command to create a Standard tier S1 hub with name `iot_hub_name` in the resource group `resource_group`.\n",
|
|
"!az iot hub create --resource-group $resource_group --name $iot_hub_name --sku S1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Command to create a free tier F1 hub. You may encounter error \"Max number of Iot Hubs exceeded for sku = Free\" if quota is reached.\n",
|
|
"# !az iot hub create --resource-group $resource_group --name $iot_hub_name --sku F1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Register an IoT Edge device\n",
|
|
"We create a device with name `device_id` under previously created iot hub."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"time.sleep(30) # Wait 30 seconds to let IoT hub stable before creating a device\n",
|
|
"print(\"az iot hub device-identity create --hub-name {} --device-id {} --edge-enabled -g {}\".format(iot_hub_name,device_id,resource_group))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!az iot hub device-identity create --hub-name $iot_hub_name --device-id $device_id --edge-enabled -g $resource_group"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Obtain device_connection_string. It will be used in the next step."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"az iot hub device-identity show-connection-string --device-id {} --hub-name {} -g {}\".format(device_id, iot_hub_name,resource_group))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"json_data = !az iot hub device-identity show-connection-string --device-id $device_id --hub-name $iot_hub_name -g $resource_group\n",
|
|
"print(json_data)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"device_connection_string = json.loads(''.join([i for i in json_data if 'WARNING' not in i]))['connectionString']\n",
|
|
"print(device_connection_string)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set_key(env_path, \"device_connection_string\", device_connection_string)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In this notebook, we created a \".env\" file to save and reuse the variables needed cross all the notebooks. We also created a new Azure resource group with name <resource\\_group>, where all Azure resources are created. We can now move on to the next notebook [02_IoTEdgeConfig.ipynb](02_IoTEdgeConfig.ipynb)."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"formats": "ipynb"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.6.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|