title | description | author | ms.author | ms.service | services | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Create an Azure IoT Hub using a template (.NET) | Microsoft Docs |
How to use an Azure Resource Manager template to create an IoT Hub with a C# program. |
kgremban |
kgremban |
iot-hub |
iot-hub |
csharp |
conceptual |
08/08/2017 |
devx-track-csharp |
[!INCLUDE iot-hub-resource-manager-selector]
You can use Azure Resource Manager to create and manage Azure IoT hubs programmatically. This tutorial shows you how to use an Azure Resource Manager template to create an IoT hub from a C# program.
Note
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and classic. This article covers using the Azure Resource Manager deployment model.
[!INCLUDE updated-for-az]
To complete this tutorial, you need the following:
- Visual Studio.
- An active Azure account.
If you don't have an account, you can create a free account in just a couple of minutes. - An Azure Storage account where you can store your Azure Resource Manager template files.
- Azure PowerShell 1.0 or later.
[!INCLUDE iot-hub-prepare-resource-manager]
-
In Visual Studio, create a Visual C# Windows Classic Desktop project using the Console App (.NET Framework) project template. Name the project CreateIoTHub.
-
In Solution Explorer, right-click on your project and then click Manage NuGet Packages.
-
In NuGet Package Manager, check Include prerelease, and on the Browse page search for Microsoft.Azure.Management.ResourceManager. Select the package, click Install, in Review Changes click OK, then click I Accept to accept the licenses.
-
In NuGet Package Manager, search for Microsoft.IdentityModel.Clients.ActiveDirectory. Click Install, in Review Changes click OK, then click I Accept to accept the license.
[!IMPORTANT] The Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the migration guide for more details.
-
In Program.cs, replace the existing using statements with the following code:
using System; using Microsoft.Azure.Management.ResourceManager; using Microsoft.Azure.Management.ResourceManager.Models; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.Rest;
-
In Program.cs, add the following static variables replacing the placeholder values. You made a note of ApplicationId, SubscriptionId, TenantId, and Password earlier in this tutorial. Your Azure Storage account name is the name of the Azure Storage account where you store your Azure Resource Manager template files. Resource group name is the name of the resource group you use when you create the IoT hub. The name can be a pre-existing or new resource group. Deployment name is a name for the deployment, such as Deployment_01.
static string applicationId = "{Your ApplicationId}"; static string subscriptionId = "{Your SubscriptionId}"; static string tenantId = "{Your TenantId}"; static string password = "{Your application Password}"; static string storageAddress = "https://{Your storage account name}.blob.core.windows.net"; static string rgName = "{Resource group name}"; static string deploymentName = "{Deployment name}";
[!INCLUDE iot-hub-get-access-token]
Use a JSON template and parameter file to create an IoT hub in your resource group. You can also use an Azure Resource Manager template to make changes to an existing IoT hub.
-
In Solution Explorer, right-click on your project, click Add, and then click New Item. Add a JSON file called template.json to your project.
-
To add a standard IoT hub to the East US region, replace the contents of template.json with the following resource definition. For the current list of regions that support IoT Hub see Azure Status:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "hubName": { "type": "string" } }, "resources": [ { "apiVersion": "2016-02-03", "type": "Microsoft.Devices/IotHubs", "name": "[parameters('hubName')]", "location": "East US", "sku": { "name": "S1", "tier": "Standard", "capacity": 1 }, "properties": { "location": "East US" } } ], "outputs": { "hubKeys": { "value": "[listKeys(resourceId('Microsoft.Devices/IotHubs', parameters('hubName')), '2016-02-03')]", "type": "object" } } }
-
In Solution Explorer, right-click on your project, click Add, and then click New Item. Add a JSON file called parameters.json to your project.
-
Replace the contents of parameters.json with the following parameter information that sets a name for the new IoT hub such as {your initials}mynewiothub. The IoT hub name must be globally unique so it should include your name or initials:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "hubName": { "value": "mynewiothub" } } }
[!INCLUDE iot-hub-pii-note-naming-hub]
-
In Server Explorer, connect to your Azure subscription, and in your Azure Storage account create a container called templates. In the Properties panel, set the Public Read Access permissions for the templates container to Blob.
-
In Server Explorer, right-click on the templates container and then click View Blob Container. Click the Upload Blob button, select the two files, parameters.json and templates.json, and then click Open to upload the JSON files to the templates container. The URLs of the blobs containing the JSON data are:
https://{Your storage account name}.blob.core.windows.net/templates/parameters.json https://{Your storage account name}.blob.core.windows.net/templates/template.json
-
Add the following method to Program.cs:
static void CreateIoTHub(ResourceManagementClient client) { }
-
Add the following code to the CreateIoTHub method to submit the template and parameter files to the Azure Resource Manager:
var createResponse = client.Deployments.CreateOrUpdate( rgName, deploymentName, new Deployment() { Properties = new DeploymentProperties { Mode = DeploymentMode.Incremental, TemplateLink = new TemplateLink { Uri = storageAddress + "/templates/template.json" }, ParametersLink = new ParametersLink { Uri = storageAddress + "/templates/parameters.json" } } });
-
Add the following code to the CreateIoTHub method that displays the status and the keys for the new IoT hub:
string state = createResponse.Properties.ProvisioningState; Console.WriteLine("Deployment state: {0}", state); if (state != "Succeeded") { Console.WriteLine("Failed to create iothub"); } Console.WriteLine(createResponse.Properties.Outputs);
You can now complete the application by calling the CreateIoTHub method before you build and run it.
-
Add the following code to the end of the Main method:
CreateIoTHub(client); Console.ReadLine();
-
Click Build and then Build Solution. Correct any errors.
-
Click Debug and then Start Debugging to run the application. It may take several minutes for the deployment to run.
-
To verify your application added the new IoT hub, visit the Azure portal and view your list of resources. Alternatively, use the Get-AzResource PowerShell cmdlet.
Note
This example application adds an S1 Standard IoT Hub for which you are billed. You can delete the IoT hub through the Azure portal or by using the Remove-AzResource PowerShell cmdlet when you are finished.
Now you have deployed an IoT hub using an Azure Resource Manager template with a C# program, you may want to explore further:
- Read about the capabilities of the IoT Hub resource provider REST API.
- Read Azure Resource Manager overview to learn more about the capabilities of Azure Resource Manager.
- For the JSON syntax and properties to use in templates, see Microsoft.Devices resource types.
To learn more about developing for IoT Hub, see the following articles:
To further explore the capabilities of IoT Hub, see: