Azure CLI Cheatsheet#

Azure CLI Cheatsheet#

Here is a list of commands we’ll commonly use with Azure CLI.

Requirements#

You can check that both requirements are met with the command az --version.

Create a Device#

To register a device on IoTHub in Azure CLI, use the az iot hub device-identity create command:

Recall that Bash environment variables allow you to store strings in a shell session. You can save environment variables in a project .env file that you can source later.

# It's convenient to store these names in environment variables for later re-use
$ export IOT_DEVICE_NAME=simDevice
$ export IOTHUB_NAME=YourIoTHubName

# Create a device with the name "simDevice"
$ az iot hub device-identity create -d ${IOT_DEVICE_NAME} -n ${IOTHUB_NAME}

Simulate a Device#

You can “simulate” an IoT Device (that is, create a temporary stream that sends IoT messages similar to real IoT Devices) using the commands below:

# Begin device simulation for an existing device
$ az iot device simulate -d ${IOT_DEVICE_NAME} -n ${IOTHUB_NAME}

The az iot device simulate command runs for a few minutes unless interrupted by the user. Use Bash Process Control keyboard shortcuts to suspend, resume, scroll through the output, etc.!

By default, this command will:

  • Send D2C messages with a payload of Ping from Az CLI IoT Extension

  • Automatically receive and acknowledge C2D messages.

See az iot device simulate documentation for details.

Monitor Messages#

You can monitor all actions in an Azure IOT Hub using the az iot hub monitor-events command:

Bash Process Control keyboard shortcuts and Bash redirection and pipes are useful for pausing/continuing/scrolling/parsing the az iot hub monitor-events command.

# List all message details for all devices.
$ az iot hub monitor-events --output table -p all -n ${IOTHUB_NAME}

# List message details for a specific device:
$ az iot hub monitor-events --output table --device-id ${IOT_DEVICE_NAME} --hub-name ${IOTHUB_NAME}

Warning

If you are having uamqp errors when using this command, try installing it in pip first.

(.venv) $ pip install uamqp

If the above is giving you issues, see the course notes on installing azure cli for more troubleshooting info.

See az iot hub monitor-events documentation for details.

Device Commands#

Send C2D Message to Simulated Device#

The az iot device c2d-message send command sends a cloud-to-device message from your IoT hub to an IoT device. The message can include a data string, representing a payload for the command, and a set of key-value pairs, representing command properties.

az iot device c2d-message send -d ${IOT_DEVICE_NAME} --data "Hello World" --props "key0=value0;key1=value1" -n ${IOTHUB_NAME}

If simDevice is the default simulated device as seen in the Simulate a Device section, then you should see the following log printed when a c2d-message is sent to it:

C2D Message Handler [Received C2D message]:
{ 'Message Properties': { 'content_encoding': 'utf-8',
                          'key0': 'value0',
                          'key1': 'value1',
                          'message_id': '1bbb2dd3-7b24-4e62-ab3a-79a8b13cb1fe'},
  'Payload': 'Hello World',
  'Topic': '/devices/simDevice/messages/devicebound'}

Note that the keys, values, and payload are set by the --props and --data arguments of the c2d-message command.

See az iot device c2d-message send documentation for more details.

Invoke Direct Method on Device#

The az iot hub invoke-device-method command calls a method (specified by name) on a chosen device, and returns a payload.

az iot hub invoke-device-method --mn ${METHOD_NAME} -d ${IOT_DEVICE_NAME} -n ${IOTHUB_NAME}

If simDevice is the default simulated device as seen in the Simulate a Device section, then you should see the following log printed when the SetTelemetryInterval direct method is invoked:

Method Request Handler [Received direct method invocation request]:
{ 'Device Id': 'simDevice',
  'Method Request Id': '1',
  'Method Request Name': 'SetTelemetryInterval',
  'Method Request Payload': {}}
{
  "payload": {
    "methodName": "SetTelemetryInterval",
    "methodRequestId": "1",
    "methodRequestPayload": {}
  },
  "status": 200
}

Update Device Twin Properties#

az iot hub device-twin update -d ${IOT_DEVICE_NAME} --desired '{"conditions":{"temperature":{"warning":98, "critical":107}}}' -n ${IOTHUB_NAME}

Get Device Twin Properties#

az iot hub device-twin show -d ${IOT_DEVICE_NAME} --query properties.reported -n ${IOTHUB_NAME}

Connection Strings#

Connection Strings provide authentication for most Azure SDK commands, both in Python and C#.

The sections below give example commands for Azure CLI connection strings retrieval.

Bash redirection and pipes are useful tools for saving the output of these connection string commands as environment variables. You can save environment variables in a project .env file that you can source later.

Device connection string#

Recommended string for a single device

az iot hub device-identity connection-string show \
  --device-id ${IOT_DEVICE_NAME} \
  --hub-name ${IOTHUB_NAME}

Service connection string#

Recommended string for external applications.

az iot hub connection-string show \
  --policy-name service \
  --hub-name ${IOTHUB_NAME}

IoT Hub EventHub-compatible connection string#

Needed for certain applications.

az iot hub connection-string show \
  -n ${IOTHUB_NAME} \
  --default-eventhub

EventHub Connection string#

Prerequisites:

  • you have an eventhub namespace

  • you have an eventhub

  • you have a shared access policy set for the eventhub

az eventhubs eventhub authorization-rule keys list \
  --resource-group ${AZURE_RESOURCE_GROUP} \
  --namespace-name ${AZURE_EVENTHUB_NAMESPACE} \
  --eventhub-name ${AZURE_EVENTHUB_NAME} \
  --name ${AZURE_SAS_POLICY}

See https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-get-connection-string#azure-cli for more details.

Owner connection string#

NOT recommended, provides access to entire resource group.

az iot hub connection-string show --hub-name ${IOTHUB_NAME}

See detailed documentation at learn.microsoft.com: