Azure IoT Solutions. Stopping/starting Azure IoT related services to cut costs.

After installing Azure IoT Solutions (free trial) in a couple days you’ll realize that IoT related services permanently consume your money and there is no easy way (the one button) how to manage this. Money just dries up. But, it’s not so hard to code short PowerShell script to stopping and starting all services related with IoT. So, the solution is below.

Stop Azure IoT services

PowerShell script to stop all Azure IoT services to minimize expenses when IoT is not used.

Test Resource Group name is

$ResourceGroupName = "RemoteMonitoringTest1018"

Stop Streaming Jobs

You can stop Streaming Jobs manually. See Azure dashboard screenshot:

Start or stop Azure Streaming Job

Streaming job name is “streamingjobs-lbcnd”. It’s easy to get the streaming job name executing command Get-AzureRmStreamAnalyticsJob. If you’ll create runbook, don’t forget to add module ‘AzureRM.StreamAnalytics‘.

$StreamingJobName = "streamingjobs-lbcnd"
Stop-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName

If the operation result is wrong – the result of the command executing will be “False”. Such a result will be if the specified in the command names are wrong.

Stop Time Series Insights (TSI) environment

There is no way to disable/enable Time Series Insights (TSI) environment. 🙁 You can check this claim going to Dashboard -> TSI-… Only “Delete” TSI is available.

You can script template deployment for creating/deleting TSI environment. With this approach, however, you are going to constantly lose your data. On the link below there are guidelines, provided by Microsoft, on how to implement template deployment: https://docs.microsoft.com/en-us/azure/time-series-insights/time-series-insights-manage-resources-using-azure-resource-manager-template

Stop App Services

To get the name of the app service: Get-AzureRmWebApp. App service name is “RemoteMonitoringTest1018”. Runbook module to add: ‘AzureRM.Websites’.

$AppServiceName = "RemoteMonitoringTest1018"
Stop-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName

Stop Azure Virtual Machines (VMs)

It’s easy to get names of the available VMs: Get-AzureRmVM. The VM name is “vmlbcnd”. Runbook module to add: ‘AzureRM.Compute‘.

PowerShell script to stop IoT Virtual Machine:

$VMname = "vm-lbcnd"
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname -Force

The final PowerShell script to stop IoT services to minimize costs

$ResourceGroupName = "RemoteMonitoringTest1018"
$AppServiceName = "RemoteMonitoringTest1018" 
$StreamingJobName = "streamingjobs-lbcnd" 
$VMname = "vm-lbcnd"
Stop-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName
Stop-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname -Force

Start Azure IoT services

Start Streaming Jobs

PowerShell script to start all Azure IoT services to minimize expenses when IoT is not used.

$StreamingJobName = "streamingjobs-lbcnd"
Start-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName -OutputStartMode "JobStartTime"

Start App Service

$AppServiceName = "RemoteMonitoringTest1018"
Start-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName

Start Azure IoT Virtual Machine

Start-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname

The final PowerShell script to stop IoT services

$ResourceGroupName = "RemoteMonitoringTest1018"
$AppServiceName = "RemoteMonitoringTest1018" 
$StreamingJobName = "streamingjobs-lbcnd" 
$VMname = "vm-lbcnd"
Start-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName -OutputStartMode "JobStartTime"
Start-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName
Start-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname

Runbook StopIoTServices

<#
    .DESCRIPTION
        An runbook which stop IoT services.

    .NOTES
        AUTHOR: Andrey Fedorov
        E-Mail: fedorov@bizkit.ru
        LASTEDIT: 22 oct 2018
#>

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

    $ResourceGroupName = "RemoteMonitoringTest1018"
    $AppServiceName = "RemoteMonitoringTest1018" 
    $StreamingJobName = "streamingjobs-lbcnd" 
    $VMname = "vm-lbcnd"
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Stop-AzureRmStreamAnalyticsJob..."
    Stop-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Stop-AzureRmWebApp..."
    Stop-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Stop-AzureRmVM..."
    Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname -Force
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Done"
    Write-Output "---------------------------------------------------------------------------"
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

StopAzureIoTServices

Runbook StartIoTServices

<#
    .DESCRIPTION
        An runbook which start stopped IoT services.

    .NOTES
        AUTHOR: Andrey Fedorov
        E-Mail: fedorov@bizkit.ru
        LASTEDIT: 22 oct 2018
#>

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

    $ResourceGroupName = "RemoteMonitoringTest1018"
    $AppServiceName = "RemoteMonitoringTest1018" 
    $StreamingJobName = "streamingjobs-lbcnd" 
    $VMname = "vm-lbcnd"
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Start-AzureRmStreamAnalyticsJob..."
    Start-AzureRmStreamAnalyticsJob -ResourceGroupName $ResourceGroupName -Name $StreamingJobName -OutputStartMode "JobStartTime"
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Start-AzureRmWebApp..."
    Start-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $AppServiceName
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Execute Start-AzureRmVM..."
    Start-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname
    Write-Output "---------------------------------------------------------------------------"
    Write-Output "Done."
    Write-Output "---------------------------------------------------------------------------"
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

StartAzureIoTServices

Spread the love
This entry was posted in IT Recipies and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *