使用 Azure 资源管理器模板创建 IoT 中心 (PowerShell)Create an IoT hub using Azure Resource Manager template (PowerShell)

了解如何使用 Azure 资源管理器模板创建 IoT 中心和使用者组。Learn how to use an Azure Resource Manager template to create an IoT Hub and a consumer group. Resource Manager 模板为 JSON 文件,用于定义针对解决方案进行部署时所需的资源。Resource Manager templates are JSON files that define the resources you need to deploy for your solution. 有关开发资源管理器模板的详细信息,请参阅 Azure 资源管理器文档For more information about developing Resource Manager templates, see Azure Resource Manager documentation.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

创建 IoT 中心Create an IoT hub

本快速入门中使用的资源管理器模板来自 Azure 快速入门模板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/2015-01-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'))]"
      ]
    }
  ]
}

该模板创建一个具有三个终结点(eventhub、cloud-to-device 和 messaging)的 Azure Iot 中心和一个使用者组。The template creates an Azure Iot hub with three endpoints (eventhub, cloud-to-device, and messaging), and a consumer group. 有关更多模板示例,请参阅 Azure 快速入门模板For more template samples, see Azure Quickstart templates. 可在此处找到 Iot 中心模板架构。The Iot Hub template schema can be found here.

可通过多种方法来部署模板。There are several methods for deploying a template. 在本教程中,将使用 Azure PowerShell。You use Azure PowerShell in this tutorial.

$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

正如你在 PowerShell 脚本中所见,使用的模板来自 Azure 快速入门模板。As you can see from the PowerShell script, the template used is from Azure Quickstart templates. 若要使用你自己的模板,你需要先上传模板文件,然后使用 -TemplateFile 开关来指定文件名。To use your own, you need to first upload the template file, and then use the -TemplateFile switch to specify the file name. 有关示例,请参阅部署模板For an example, see Deploy the template.

后续步骤Next steps

现在,你已使用 Azure 资源管理器模板部署了一个 IoT 中心,你可能希望进一步进行探索:Now you have deployed an IoT hub by using an Azure Resource Manager template, you may want to explore further:

若要详细了解如何开发 IoT 中心,请参阅以下文章:To learn more about developing for IoT Hub, see the following articles:

若要进一步探索 IoT 中心的功能,请参阅:To further explore the capabilities of IoT Hub, see: