Create and delete routes and endpoints by using Azure PowerShell

This article shows you how to create a route and endpoint in your hub in Azure IoT Hub and then delete your route and endpoint. Learn how to use Azure PowerShell to create routes and endpoints for Azure Event Hubs, Azure Service Bus queues and topics, and Azure Storage.

To learn more about how routing works in IoT Hub, see Use IoT Hub message routing to send device-to-cloud messages to different endpoints. To walk through setting up a route that sends messages to storage and then testing on a simulated device, see Tutorial: Send device data to Azure Storage by using IoT Hub message routing.

Note

Currently, PowerShell doesn't support managed identity authentication types for creating endpoints. If you can't use SAS authentication in your scenario, use one of the other management tools to create endpoints.

Also, PowerShell currently doesn't support creating Cosmos DB endpoints.

Prerequisites

Review the prerequisites for this article based on the type of endpoint you want to route the messages to.

Create endpoints

In IoT Hub, you can create a route to send messages or capture events. Each route has a data source and an endpoint. The data source is where messages or event logs originate. The endpoint is where the messages or event logs end up. You choose locations for the data source and endpoint when you create a new route in your IoT hub. Then, you use routing queries to filter messages or events before they go to the endpoint.

The service that you use to create your endpoint must first exist in your Azure account.

Note

If you use a local version of Azure PowerShell, sign in to Azure PowerShell before you begin.

The commands in the following procedures use these references:

  1. Get the primary connection string from your event hub. Copy the connection string to use later.

    Get-AzEventHubKey -ResourceGroupName MyResourceGroup -NamespaceName MyNamespace -EventHubName MyEventHub -Name MyAuthRule
    
  2. Create a new IoT hub endpoint to Event Hubs. Use your primary connection string from the preceding step. The value for EndpointType must be EventHub. For all other parameters, use the values for your scenario.

    Add-AzIotHubRoutingEndpoint -ResourceGroupName MyResourceGroup -Name MyIotHub -EndpointName MyEndpoint -EndpointType EventHub -EndpointResourceGroup MyResourceGroup -EndpointSubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -ConnectionString "Endpoint=<my connection string>"
    

    To see all routing endpoint options, see Add-AzIotHubRoutingEndpoint.

Create an IoT Hub route

With your new endpoint in your IoT hub, you can create a new route.

The default fallback route in IoT Hub collects messages from DeviceMessages. Choose a different option for your custom route, such as DeviceConnectionStateEvents. For more information about source options, see Add-AzIotHubRoute. The Enabled parameter is a switch, so you don't need to use a value with the parameter.

Add-AzIotHubRoute -ResourceGroupName MyResourceGroup -Name MyIotHub -RouteName MyRoute -Source DeviceLifecycleEvents -EndpointName MyEndpoint -Enabled

PowerShell displays a confirmation that looks similar to this example:

RouteName     : MyIotHub 
DataSource    : DeviceLifecycleEvents
EndpointNames : MyEndpoint
Condition     : true
IsEnabled     : True

Update an IoT Hub route

To make changes to an existing route, use the following command. For example, try changing the name of your route by using the command.

Set-AzIotHubRoute -ResourceGroupName MyResourceGroup -Name MyIotHub -RouteName MyRoute

Use the Get-AzIotHubRoute command to confirm the change in your route:

Get-AzIotHubRoute -ResourceGroupName MyResourceGroup -Name MyIotHub

Delete an endpoint

To delete an endpoint:

Remove-AzIotHubRoutingEndpoint -ResourceGroupName MyResourceGroup -Name MyIotHub -EndpointName MyEndpoint -PassThru

Delete an IoT Hub route

To delete an IoT Hub route:

Remove-AzIotHubRoute -ResourceGroupName MyResourceGroup -Name MyIotHub -RouteName MyRoute -PassThru

Tip

Deleting a route doesn't delete any endpoints in your Azure account. You must delete an endpoint separately from deleting a route.

Next steps

In this how-to article, you learned how to create a route and endpoint for Event Hubs, Service Bus queues and topics, and Azure Storage.

To learn more about message routing, see Tutorial: Send device data to Azure Storage by using IoT Hub message routing. In the tutorial, you create a storage route and test it with a device in your IoT hub.