Create an IoT hub using Azure Resource Manager template (PowerShell)

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 with PowerShell.

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.

To complete this tutorial, you need the following:

Tip

The article Using Azure PowerShell with Azure Resource Manager provides more information about how to use PowerShell and Azure Resource Manager templates to create Azure resources.

Connect to your Azure subscription

In a PowerShell command prompt, enter the following command to sign in to your Azure subscription:

Login-AzureRmAccount

If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure subscriptions associated with your credentials. Use the following command to list the Azure subscriptions available for you to use:

Get-AzureRMSubscription

Use the following command to select subscription that you want to use to run the commands to create your IoT hub. You can use either the subscription name or ID from the output of the previous command:

Select-AzureRMSubscription `
    -SubscriptionName "{your subscription name}"

You can use the following commands to discover where you can deploy an IoT hub and the currently supported API versions:

((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Devices).ResourceTypes | Where-Object ResourceTypeName -eq IoTHubs).Locations
((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Devices).ResourceTypes | Where-Object ResourceTypeName -eq IoTHubs).ApiVersions

Create a resource group to contain your IoT hub using the following command in one of the supported locations for IoT Hub. This example creates a resource group called MyIoTRG1:

New-AzureRmResourceGroup -Name MyIoTRG1 -Location "East US"

Submit a template to create an IoT hub

Use a JSON template 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.

  1. Use a text editor to create an Azure Resource Manager template called template.json with the following resource definition to create a new standard IoT hub. This example adds the IoT Hub in the East US region, creates two consumer groups (cg1 and cg2) on the Event Hub-compatible endpoint, and uses the 2016-02-03 API version. This template also expects you to pass in the IoT hub name as a parameter called hubName. For the current list of locations 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"
        }
      },
      {
        "apiVersion": "2016-02-03",
        "type": "Microsoft.Devices/IotHubs/eventhubEndpoints/ConsumerGroups",
        "name": "[concat(parameters('hubName'), '/events/cg1')]",
        "dependsOn": [
          "[concat('Microsoft.Devices/Iothubs/', parameters('hubName'))]"
        ]
      },
      {
        "apiVersion": "2016-02-03",
        "type": "Microsoft.Devices/IotHubs/eventhubEndpoints/ConsumerGroups",
        "name": "[concat(parameters('hubName'), '/events/cg2')]",
        "dependsOn": [
          "[concat('Microsoft.Devices/Iothubs/', parameters('hubName'))]"
        ]
      }
      ],
      "outputs": {
        "hubKeys": {
          "value": "[listKeys(resourceId('Microsoft.Devices/IotHubs', parameters('hubName')), '2016-02-03')]",
          "type": "object"
        }
      }
    }
    
  2. Save the Azure Resource Manager template file on your local machine. This example assumes you save it in a folder called c:\templates.

  3. Run the following command to deploy your new IoT hub, passing the name of your IoT hub as a parameter. In this example, the name of the IoT hub is abcmyiothub. The name of your IoT hub must be globally unique:

    New-AzureRmResourceGroupDeployment -ResourceGroupName MyIoTRG1 -TemplateFile C:\templates\template.json -hubName abcmyiothub
    

    Important

    The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

  4. The output displays the keys for the IoT hub you created.

  5. To verify your application added the new IoT hub, visit the Azure portal and view your list of resources. Alternatively, use the Get-AzureRmResource 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-AzureRmResource PowerShell cmdlet when you are finished.

Next steps

Now you have deployed an IoT hub using an Azure Resource Manager template with PowerShell, you may want to explore further:

To learn more about developing for IoT Hub, see the following articles:

To further explore the capabilities of IoT Hub, see: