Set up the Development Environment

Lab Scenario

As one of the developers at Contoso, you know that setting up your development environment is an important step before starting to build your Azure IoT solution. You also know that Microsoft and other companies provide a number of tools that can be used to develop and support your IoT solutions, and that some decisions should be made about which tools your team will use.

You decide to prepare a development environment that the team can use to work on your IoT solution. The environment will need to support your work in Azure and on your local PC. After some discussion, your team has made the following high-level decisions about the dev environment:

  • Operating System: Windows 10 will be used as the OS. Windows is used by most of your team, so it was a logical choice. You make a note to the team that Azure services support other operating systems (such as Mac OS and Linux), and that Microsoft provides supporting documentation for the members of your team who choose one of these alternatives.
  • General Coding Tools: Visual Studio Code and Azure CLI will be used as the primary coding tools. Both of these tools support extensions for IoT that leverage the Azure IoT SDKs.
  • IoT Edge Tools: Docker Desktop Community and Python will be used to support custom IoT Edge module development (along with Visual Studio Code).

In support of these decisions, you will be setting up the following environment:

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later). Including
    • 4GB – 8GB system RAM (higher the better for Docker)
    • Hyper-V and Containers features of Windows must be enabled.
    • BIOS-level hardware virtualization support must be enabled in the BIOS settings.

    Note: When setting up the development environment on a virtual machine, the VM environment must support nested virtualization - nested virtualization

  • Azure CLI (current/latest)
  • .NET Core 3.1.200 (or later) SDK
  • VS Code (latest)
  • Python 3.9
  • Docker Desktop Community 2.1.0.5 (or later) set to Linux Containers
  • IoT Extensions for VS Code and Azure CLI
  • node.js (latest)

IMPORTANT: A virtual machine has been created for this course that provides a majority of the tools specified above. The instructions below support using the prepared VM or setting up the development environment locally using your PC.

In This Lab

In this lab, you will configure your development environment. The lab includes the following exercises:

  • For students using the LOD virtual machine environment:

    • Install Dev Tool Extensions in the Virtual Machine Environment
  • For students who need to configure their local PC as a development environment:

    • Install Developer Tools and Products on the Local PC
    • Install Dev Tool Extensions on the Local PC
    • Set Up Course Lab Files and Alternative Tools on the Local PC
    • Register Resource Providers on the Local PC

Lab Instructions

IMPORTANT: For students who are using the hosted virtual machine environment to complete the labs in this course, Exercise 1 is the only Exercise that you need to complete. All of the other Exercises in this lab are for students who are setting up a dev environment on their local PC. If you will be setting up a dev environment on your local PC, skip to ahead to Exercise 2 and then complete all of the remaining Exercises.

Exercise 1: Install Tool Extensions in the Virtual Machine Environment

The Visual Studio Code and Azure CLI tools (already installed on the virtual machine) both support an Azure IoT extension that helps developers to create their solutions more efficiently. These extensions leverage the Azure IoT SDKs and will often reduce development time while ensuring security provisions. You will also be adding a C# extension for Visual Studio Code.

Task 1: Install Visual Studio Code Extensions on the Virtual Machine

  1. Open Visual Studio Code.

  2. On the left side of the Visual Studio Code window, click Extensions.

    You can hover the mouse pointer over the buttons to display the button titles. The Extensions button is sixth from the top.

  3. In the Visual Studio Code Extension manager, search for and then install the following Extensions:

  4. Close Visual Studio Code.

Task 2: Install Azure CLI Extension - virtual machine environment

  1. Open a new command-line / terminal window, to install the Azure IoT CLI extensions.

    For example, you can use the Windows Command Prompt command-line application.

  2. At the command prompt, to install the Azure CLI extension for IoT, enter the following command:

     az extension add --name azure-iot
    
  3. At the command prompt, to install the Azure CLI extension for Time Series Insights, enter the following command:

     az extension add --name timeseriesinsights
    

Task 3: Install Azure CLI Extension - cloud environment

Many of the labs will require the use of the Azure CLI IoT extensions via the Azure Cloud Shell. The following steps ensure the extension is installed and running the latest version.

  1. Open Web browser window on your virtual machine, and then navigate to the Azure cloud shell at the following address:

    +++https://shell.azure.com/+++

  2. When prompted, login with the Azure subscription you are using for this course.

  3. If you are prompted about setting up storage for Cloud Shell, accept the defaults.

  4. Verify that the Cloud Shell is using Bash.

    The dropdown in the top-left corner of the Azure Cloud Shell page is used to select the environment. Verify that the selected dropdown value is Bash.

  5. At the command prompt, to install the Azure CLI extension for IoT, enter the following command:

     az extension add --name azure-iot
    

    Note: If the extension is already installed, you can ensure you are running the latest version by entering the following command:

     az extension update --name azure-iot
    
  6. At the command prompt, to install the Azure CLI extension for Time Series Insights, enter the following command:

     az extension add --name timeseriesinsights
    

Task 4: Verify the Virtual Machine environment

You should verify that the development environment has been set up successfully. Once this is complete, you will be ready to start building your IoT solutions.

  1. Open a new command-line / terminal window.

  2. Validate the Azure CLI installation by running the following command that will output the version information for the currently installed version of the Azure CLI.

     az --version
    

    The az --version command will output the version information for Azure CLI that you have installed (the azure-cli version number). This command also outputs the version number for all the Azure CLI modules installed, including the IoT extension. You should see output similar to the following:

     azure-cli                           2.21.0
    
     core                                2.21.0
     telemetry                            1.0.6
    
     Extensions:
     azure-iot                          0.10.10
     timeseriesinsights                   0.2.1
    
  3. Validate the .NET Core 3.x SDK installation by running the following command that will output the version number for the currently installed version of the .NET Core SDK.

     dotnet --version
    

    The dotnet --version command will output the version of the .NET Core SDK that is currently installed.

  4. Verify that .NET Core 3.1 or higher is installed.

NOTE: If you are using the LOD virtual machine environment to complete the labs in this course, your dev environment is configured and you are ready to move on to the next lab.

Exercise 2: Install Developer Tools and Products on the local PC

IMPORTANT: Exercise 2 and all of the remaining Exercises in this lab are for students who are NOT using the LOD virtual machine that was created for this course. Students who need to configure their local PC as their dev environment can use Exercises 2-5 to help them install tools and configure the dev environment on their local PC.

Task 1: Install .NET Core on your local PC

.NET Core is a cross-platform version of .NET for building websites, services, and console apps.

  1. To open the .NET Core download page, open a new browser tab, and then navigate to the following page:

     https://dotnet.microsoft.com/download
    
  2. On the .NET download page, under .NET Core, click Download .NET Core SDK.

    The .NET Core SDK is used to build .NET Core apps. You will be using it to build/edit code files during the labs in this course.

  3. On the popup menu, click Run, and then follow the on-screen instructions to complete the installation.

    The installation should take less than a minute to complete. The following components will be installed:

    • .NET Core SDK 3.1.100 or later
    • .NET Core Runtime 3.1.100 or later
    • ASP.NET Core Runtime 3.1.100 or later
    • .NET Core Windows Desktop Runtime 3.1.0 or later

    The following resources are available for further information:

Task 2: Install Visual Studio Code on your local PC

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and run times (such as .NET and Unity).

  1. To open the Visual Studio Code download page, open a new browser tab, and then navigate to the following page:

     https://code.visualstudio.com/Download
    

    Instructions for installing Visual Studio Code on Mac OS X and Linux can be found on the Visual Studio Code set up guide here. This page also includes more detailed instructions and tips for the Windows installation.

  2. On the Download Visual Studio Code page, click Windows.

    When you start the download, two things will happen: a popup dialog opens and some getting started guidance will be displayed.

  3. On the popup dialog, to begin the set up process, click Run and then follow the on-screen instructions.

    If you choose to Save the installer to your Downloads folder, you you can complete the installation by opening the folder and then double-clicking the VSCodeSetup executable.

    By default, Visual Studio Code is installed in the “C:\Program Files (x86)\Microsoft VS Code” folder location (for a 64-bit machine). The set up process should only take about a minute.

    Note: .NET Framework 4.5 is required for Visual Studio Code when installing on Windows. If you are using Windows 7, please ensure .NET Framework 4.5 is installed.

    For detailed instructions on installing Visual Studio Code, see the Microsoft Visual Studio Code Installation Instruction guide here: https://code.visualstudio.com/Docs/editor/setup

Task 3: Install Azure CLI on your local PC

Azure CLI is a command-line tool that is designed to make scripting Azure-related tasks easier. It also enables you to flexibly query data, and it supports long-running operations as non-blocking processes.

  1. To open the Azure CLI tools download page, open a new browser tab, and then navigate to the following page:

     https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest  
    

    You should be installing the latest version of the Azure CLI tools. The current version of Azure CLI (as of February 2022) is version 2.33, but be aware that new versions are released monthly, so the latest version is likely to have changed.

  2. On the Install Azure CLI page, select the install option for your OS (such as Install on Windows), and then follow the on-screen instructions to install the Azure CLI tool.

    You will be given detailed instructions for using the Azure CLI tools during the labs in this course, but if you want more information now, see Get started with Azure CLI

Task 4: Install Python 3.9 on your local PC

You will be using Python 3.9 in support of IoT Edge and Docker.

  1. To open the Python.org downloads page, open a new browser tab, and then navigate to the following page:

     https://www.python.org/downloads/
    
  2. On the Python download page, select the installer file that is appropriate for your Operating System.

  3. When prompted, select the option to run the installer

  4. On the Install Python dialog, click Add Python 3.9 to PATH.

  5. Click Install Now.

  6. When the “Setup was successful” page appears, click Disable path length limit.

  7. To finish the installation process, click Close.

Task 5: Install Docker Desktop on your local PC

You will be using Docker Desktop Community (latest stable version) during a lab that covers creating and deploying custom IoT Edge modules.

  1. To open the Docker Desktop for Windows download page, open a new browser tab, and then navigate to the following page:

     https://docs.docker.com/docker-for-windows/install/ 
    

    The left-side navigation menu provides access to installations for additional operating systems.

  2. Verify that your PC meets the System Requirements.

    You can use Windows Settings to open the Windows Features dialog, and use that to verify that Hyper-V and Containers are enabled.

  3. Click Download from Docker Hub

  4. Under Docker Desktop for Windows, click Get Docker Desktop for Windows (stable).

  5. To start the installation, click Run.

    It can take a little while for the installation dialog for Docker Desktop to appear.

  6. When the Installation Succeeded message appears, click Close.

    Docker Desktop does not start automatically after installation. To start Docker Desktop, search for Docker, and select Docker Desktop in the search results. When the whale icon in the status bar stays steady, Docker Desktop is up-and-running, and is accessible from any terminal window.

Task 6 - Install node.js on your local PC

Some sample web applications are run locally using node.js. The following steps ensure node.js is installed and running the latest version:

  1. To open the Node.js download page, open a new browser tab, and then navigate to the following page:

     https://nodejs.org/en/#home-downloadhead
    
  2. Download the latest LTS (Long Term Support) version - 14.16.0 at the time of writing.

  3. When prompted, select the option to run the installer.

  4. Step through the installer:

    • End-User License Agreement - accept terms and click Next.
    • Destination Folder - accept the default (or change if required) and click Next.
    • Custom Setup - accept the defaults and click Next.
    • Tools for Native Modules - check the Automatically install and click Next.
    • Ready to install Node.js - click Install
      • On the UAC dialog, click Yes.
  5. Wait for the install to complete and click Finish.

  6. In the Install Additional Tools for Node.js command window, when prompted, press Enter to continue.

  7. On the UAC dialog, click Yes.

    Multiple packages will be downloaded and installed. This will take some time.

  8. Once the installation has completed, open a new command shell and enter the following command:

     node --version
    

    If node is installed successfully, the installed version will be displayed.

Exercise 3: Install Tool Extensions for the local PC environment

The Visual Studio Code and Azure CLI tools both support an Azure IoT extension that helps developers to create their solutions more efficiently. These extensions leverage the Azure IoT SDKs and will often reduce development time while ensuring security provisions. You will also be adding a C# extension for Visual Studio Code.

Task 1: Install Visual Studio Code Extensions on the local PC

  1. Open Visual Studio Code.

  2. On the left side of the Visual Studio Code window, click Extensions.

    You can hover the mouse pointer over the buttons to display the button titles. The Extensions button is sixth from the top.

  3. In the Visual Studio Code Extension manager, search for and then install the following Extensions:

  4. Close Visual Studio Code.

Task 2: Install Azure CLI Extension - local PC environment

  1. Open a new command-line / terminal window, to install the Azure IoT CLI extensions.

    For example, you can use the Windows Command Prompt command-line application.

  2. At the command prompt, to install the Azure CLI extension for IoT, enter the following command:

     az extension add --name azure-iot
    
  3. At the command prompt, to install the Azure CLI extension for Time Series Insights, enter the following command:

     az extension add --name timeseriesinsights
    

Task 3: Install Azure CLI Extension - cloud environment

Many of the labs will require the use of the Azure CLI IoT extensions via the Azure Cloud Shell. The following steps ensure the extension is installed and running the latest version.

  1. In your Web browser, navigate to the Azure cloud shell at the following address:

    Local PC: Azure Cloud Shell

  2. When prompted, login with the Azure subscription you are using for this course.

  3. If you are prompted about setting up storage for Cloud Shell, accept the defaults.

  4. Verify that the Cloud Shell is using Bash.

    The dropdown in the top-left corner of the Azure Cloud Shell page is used to select the environment. Verify that the selected dropdown value is Bash.

  5. At the command prompt, to install the Azure CLI extension for IoT, enter the following command:

     az extension add --name azure-iot
    

    Note: If the extension is already installed, you can ensure you are running the latest version by entering the following command:

     az extension update --name azure-iot
    
  6. At the command prompt, to install the Azure CLI extension for Time Series Insights, enter the following command:

     az extension add --name timeseriesinsights
    

Task 4: Verify Development Environment Setup

You should verify that the development environment has been set up successfully.

  1. Open a new command-line / terminal window.

  2. Validate the Azure CLI installation by running the following command that will output the version information for the currently installed version of the Azure CLI.

     az --version
    

    The az --version command will output the version information for Azure CLI that you have installed (the azure-cli version number). This command also outputs the version number for all the Azure CLI modules installed, including the IoT extension. You should see output similar to the following:

     azure-cli                           2.21.0
    
     core                                2.21.0
     telemetry                            1.0.6
    
     Extensions:
     azure-iot                          0.10.10
     timeseriesinsights                   0.2.1
    
  3. Validate the .NET Core 3.x SDK installation by running the following command that will output the version number for the currently installed version of the .NET Core SDK.

     dotnet --version
    

    The dotnet --version command will output the version of the .NET Core SDK that is currently installed.

  4. Verify that .NET Core 3.1 or higher is installed.

Exercise 4: Set Up Course Lab Files and Alternative Tools on the Local PC

A number of the labs in this course rely on pre-built resources, such as a code project that can be used as a starting point for the lab activity. These lab resources are provided in a GitHub project that you should download to your local PC dev environment.

In addition to the resources that directly support the course labs (the resources contained in the GitHub project), there are some optional tools that you may choose to install because they support learning opportunities outside of this course. One example is PowerShell, which you may see referenced within Microsoft tutorials and other resources.

The instructions below lead you through the configuration of both these resource types.

Task 1: Download Course Lab Files to the Local PC

Microsoft has created a GitHub repo to provide access to lab resource files. Having these files local to the dev environment is required in some cases and convenient in many others. In this task you will be downloading and extracting the contents of the repo within your development environment.

  1. In your Web browser, navigate to the following location: https://github.com/MicrosoftLearning/AZ-220-Microsoft-Azure-IoT-Developer

  2. Open the Code dropdown, and then click Download ZIP.

  3. To save the ZIP file to your dev environment, click Save.

  4. Once the file has been saved, click Open folder.

  5. Right-click the saved ZIP file, and then click Extract all

  6. Click Browse, and then navigate to folder location that is convenient to access.

    Important: By default, Windows has a maximum file path length of 260. As the file paths within the ZIP are already long, avoid extracting the archive within nested folders with a large file path. For example, the default path prompted to extract the zip could be similar to c:\users\[username]\downloads\AZ-220-Microsoft-Azure-IoT-Developer-master - it is recommended that you shorten this as much as possible, to perhaps c:\az220

  7. To extract the files, click Extract.

    Be sure to make note of where you located the files.

Task 2: Install Azure PowerShell Module on the Local PC

Note: The lab activities in this course do NOT include using PowerShell, however, you may see sample code in reference documents that use PowerShell. If you want to run PowerShell code, you can use the following instructions to complete the installation steps.

Azure PowerShell is a set of cmdlets for managing Azure resources directly from the PowerShell command line. Azure PowerShell is designed to make it easy to learn and get started with, but provides powerful features for automation. Written in .NET Standard, Azure PowerShell works with PowerShell 5.1 on Windows, and PowerShell 6.x and higher on all platforms.

Warning: You can’t have both the AzureRM and Az modules installed for PowerShell 5.1 for Windows at the same time. If you need to keep AzureRM available on your system, install the Az module for PowerShell Core 6.x or later. To do this, install PowerShell Core 6.x or later and then follow these instructions in a PowerShell Core terminal.

  1. Decide if you wish to install the Azure PowerShell module for just the current user (recommended approach) or for all users.

  2. Launch the PowerShell terminal of your choice - if you are installing for all users you must launch an elevated PowerShell session either by either selecting run as administrator or with the sudo command on macOS or Linux.

  3. To only install for the current user, enter the following command:

     Install-Module -Name Az -AllowClobber -Scope CurrentUser
    

    or to install for all users on a system, enter the following command:

     Install-Module -Name Az -AllowClobber -Scope AllUsers
    
  4. By default, the PowerShell gallery isn’t configured as a trusted repository for PowerShellGet. The first time you use the PSGallery you see the following prompt:

     Untrusted repository
    
     You are installing the modules from an untrusted repository. If you trust this repository, change
     its InstallationPolicy value by running the Set-PSRepository cmdlet.
    
     Are you sure you want to install the modules from 'PSGallery'?
     [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):
    
  5. Answer Yes or Yes to All to continue with the installation.

    The Az module is a rollup module for the Azure PowerShell cmdlets. Installing it downloads all of the available Azure Resource Manager modules, and makes their cmdlets available for use.

Note: If the Az module is already installed, you can update to the latest version using:

Update-Module -Name Az

Exercise 5 - Register Resource Providers on the Local PC

Many different types of resources will be created during this course, some of which may not have been registered for use in the current subscription. While some resources are registered automatically during the first use, others must be registered before they can be used, otherwise errors will be reported.

Task 1 - Register resource providers using the Azure CLI

The Azure CLI provides a number of commands to help manage resource providers. In this task, you will ensure that the resource providers required for this course are registered.

  1. Using a browser, open the Azure Cloud Shell and login with the Azure subscription you are using for this course.

  2. To view a list of the current state of the resource providers, enter the following command:

     az provider list -o table
    

    This will display a long list of resources, similar to:

     Namespace                                RegistrationPolicy    RegistrationState
     ---------------------------------------  --------------------  -------------------
     Microsoft.OperationalInsights            RegistrationRequired  Registered
     microsoft.insights                       RegistrationRequired  NotRegistered
     Microsoft.DataLakeStore                  RegistrationRequired  Registered
     Microsoft.DataLakeAnalytics              RegistrationRequired  Registered
     Microsoft.Web                            RegistrationRequired  Registered
     Microsoft.ContainerRegistry              RegistrationRequired  Registered
     Microsoft.ResourceHealth                 RegistrationRequired  Registered
     Microsoft.BotService                     RegistrationRequired  Registered
     Microsoft.Search                         RegistrationRequired  Registered
     Microsoft.EventGrid                      RegistrationRequired  Registered
     Microsoft.SignalRService                 RegistrationRequired  Registered
     Microsoft.VSOnline                       RegistrationRequired  Registered
     Microsoft.Sql                            RegistrationRequired  Registered
     Microsoft.ContainerService               RegistrationRequired  Registered
     Microsoft.ManagedIdentity                RegistrationRequired  Registered
     ...
    
  3. To see return a list of the namespaces that contains the string Event, run the following command:

     az provider list -o table --query "[?contains(namespace, 'Event')]"
    

    The results will be similar to:

     Namespace            RegistrationState    RegistrationPolicy
     -------------------  -------------------  --------------------
     Microsoft.EventGrid  NotRegistered        RegistrationRequired
     Microsoft.EventHub   Registered           RegistrationRequired
    
  4. To register the resources required for this course, execute the following commands:

     az provider register --namespace "Microsoft.EventGrid" --accept-terms
     az provider register --namespace "Microsoft.EventHub" --accept-terms
     az provider register --namespace "Microsoft.Insights" --accept-terms
     az provider register --namespace "Microsoft.TimeSeriesInsights" --accept-terms
    

    NOTE: You may see a warning that -accept-terms is in preview - you can ignore this.

    NOTE: The microsoft.insights is listed in lowercase - however the register/unregister commands are case-insensitive.

  5. To view the updated status of the resources, execute the following commands:

     az provider list -o table --query "[?(contains(namespace, 'insight') || contains(namespace, 'Event') || contains(namespace, 'TimeSeriesInsights'))]"
    

    NOTE: Although the register/unregister commands are case-insensitive, the query language is not, so insight must be lowercase.

    The resources should now be registered.

Task 2 - Register resource providers using the Azure Portal

You can see the registration status and register a resource provider namespace through the portal. In this task, you will familiarize yourself with the UI.

  1. If necessary, log in to portal.azure.com using your Azure account credentials.

  2. From the portal, select All services.

    Select all services

  3. Select Subscriptions.

    Select subscriptions

  4. From the list of subscriptions, select the subscription you want to use for registering the resource provider.

    Select subscription to register resource provider

  5. For your subscription, select Resource providers.

    Select resource providers

  6. Look at the list of resource providers, resources can be be registered or unregistered by clicking the appropriate action.

    List resource providers

  7. To filter the listed resources, in the search textbox, enter insights.

    Notice that the list is filtered as search criteria is entered. The search is also case-insensitive.