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

Learn how to use an Azure Resource Manager template to create an IoT Hub and a consumer group. Resource Manager templates are JSON files that define the resources you need to deploy for your solution. For more information about developing Resource Manager templates, see Azure Resource Manager documentation.

If you don't have an Azure subscription, create a free account before you begin.

Create an IoT hub

The Resource Manager template used in this quickstart is from Azure Quickstart templates. Here is a copy of the template:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "iotHubName": {
      "type": "string",
      "minLength": 3,
      "metadata": {
        "description": "Specifies the name of the IoT Hub."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
              "description": "Location for all resources."
      }
    },
    "skuName": {
      "type": "string",
      "defaultValue": "F1",
      "metadata": {
        "description": "Specifies the IotHub SKU."
      }
    },
    "capacityUnits": {
      "type": "int",
      "minValue": 1,
      "maxValue": 1,
      "defaultValue": 1,
      "metadata": {
        "description": "Specifies the number of provisioned IoT Hub units. Restricted to 1 unit for the F1 SKU. Can be set up to maximum number allowed for subscription."
      }
    }
  },
  "variables": {
    "consumerGroupName": "[concat(parameters('iotHubName'), '/events/cg1')]"
  },
  "resources": [
    {
      "type": "Microsoft.Devices/IotHubs",
      "apiVersion": "2018-04-01",
      "name": "[parameters('iotHubName')]",
      "location": "[parameters('location')]",
      "properties": {
        "eventHubEndpoints": {
          "events": {
            "retentionTimeInDays": 1,
            "partitionCount": 2
          }
        },
        "cloudToDevice": {
          "defaultTtlAsIso8601": "PT1H",
          "maxDeliveryCount": 10,
          "feedback": {
            "ttlAsIso8601": "PT1H",
            "lockDurationAsIso8601": "PT60S",
            "maxDeliveryCount": 10
          }
        },
        "messagingEndpoints": {
          "fileNotifications": {
            "ttlAsIso8601": "PT1H",
            "lockDurationAsIso8601": "PT1M",
            "maxDeliveryCount": 10
          }
        }
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "capacity": "[parameters('capacityUnits')]"
      }
    },
    {
      "type": "Microsoft.Devices/iotHubs/eventhubEndpoints/ConsumerGroups",
      "apiVersion": "2018-04-01",
      "name": "[variables('consumerGroupName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Devices/IotHubs', parameters('iotHubName'))]"
      ]
    }
  ]
}

The template creates an Azure Iot hub with three endpoints (eventhub, cloud-to-device, and messaging), and a consumer group. For more template samples, see Azure Quickstart templates. The Iot Hub template schema can be found here.

There are several methods for deploying a template. You use Azure PowerShell in this tutorial.

To run the PowerShell script, Select Try it to open the Azure Cloud shell. To paste the script, right-click the shell, and then select Paste:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$iotHubName = Read-Host -Prompt "Enter the IoT Hub name"

New-AzResourceGroup -Name $resourceGroupName -Location "$location"
New-AzResourceGroupDeployment `
    -ResourceGroupName $resourceGroupName `
    -TemplateUri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-iothub-with-consumergroup-create/azuredeploy.json" `
    -iotHubName $iotHubName

As you can see from the PowerShell script, the template used is from Azure Quickstart templates. To use your own, you need to first upload the template file to the Cloud shell, and then use the -TemplateFile switch to specify the file name. For an example, see Deploy the template.

Next steps

Now you have deployed an IoT hub by using an Azure Resource Manager template, 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: