きっかけ
以前、Azure Application Gatewayの課金を止めたい!にて、コマンドでApplication Gatewayを停止する記事を書きました。
ところが、金曜日の退勤時に停止するのを忘れ、土日そのまま無駄に稼働させてしまいました。
約15円/h × 48h として約720円の無駄遣い・・・。
やっぱり自動停止したい
手動停止は忘れちゃいますよね。だって人間だもの。
というわけで、自動停止するようにしてみました。
Azure Automationを使う
- Azure Automation の概要
プロセスの自動化
このサービスでは、Runbook の作成をグラフィカルに行うか、PowerShell または Python を使用して実行できます。
含まれている無料ユニット (1 か月あたり) 料金 ジョブ実行時間 500 分 ¥0.224/分 ウォッチャー 744 時間 ¥0.224/時間
AutomationでPowerShell動かせますね。
たぶん無料分で収まるかな?仮に1か月分かかっても200円いかないくらい?
1日止め忘れを防止できれば元取れそう。
さっそくやってみる
以下の手順で、Automationを利用して自動停止することができます。
Automationアカウントの作成
参考記事のAutomation アカウントを作成するを参考に、Automationアカウントを作成します。
※記事のスクリーンショットが古いので、記事執筆時点1の画面で手順を記載しました。
-
Azureホーム から Automationアカウント を開く。
-
**[+追加]**ボタンを押す。
【参考】スクリーンショット
- アカウント名等を入力して、**[作成]**を押す。
【参考】スクリーンショット
- しばらく待つとアカウントが作成されます。
【参考】スクリーンショット
AzureRM.Networkモジュールの追加
前回の記事で書いたコマンドを使うのですが、Automationでそのコマンドを実行するとCommandNotFoundException
が出てしまいました。
Get-AzureRmApplicationGateway : The term 'Get-AzureRmApplicationGateway' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:21 char:10
+ $AppGw = Get-AzureRmApplicationGateway -Name $appGatewayName -Resourc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-AzureRmApplicationGateway:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Automationの初期状態では、このコマンドのモジュール(AzureRM.Network
)が入っていないため、自分でインポートする必要があるとのこと。
この記事を参考に、AzureRM.Network
をインポートします。
※やはりスクリーンショットが古いので、記事執筆時点1の画面で手順を記載しました。
モジュールのバージョン確認
-
作成したAutomationアカウントを開く。
-
**[共有リソース]の[モジュール]**を開く。
-
AzureRM.Profileのバージョンを確認する。
本記事執筆時点1では、AzureRM.Networkをインストールする際に、AzureRM.Profileのバージョンが5.8.2以上が必須というエラーが出ました。
それより古い場合はAzureRM.profileの更新から、それ以上であればAzureRM.Networkのインポートの手順から実施して下さい。
【参考】スクリーンショット
AzureRM.profileの更新
-
**[共有リソース]の[モジュール ギャラリー]**を開く。
-
AzureRM.profileを選択する。
【参考】スクリーンショット
- バージョンを確認し**[インポート]**を押す。
インポートの確認ダイアログが出るので、同意をチェックしてOKを押す。
【参考】スクリーンショット
- **[共有リソース]の[モジュール]**を開き、バージョンが変わって使用可能になるまで待つ。
AzureRM.Networkのインポート
-
**[共有リソース]の[モジュール ギャラリー]**を開く。
-
AzureRM.Network を選択する。
【参考】スクリーンショット
- **[インポート]**を押す。
インポートの確認ダイアログが出るので、OKを押す。
【参考】スクリーンショット
- **[共有リソース]の[モジュール]**を開き、使用可能になるまで待つ。
【参考】スクリーンショット
Runbookの作成
この記事を参考に、新しいRunbookを作り、自動停止シェルを書いていきます。
※ここも記事執筆時点1の画面で手順を記載しました。
-
作成したAutomationアカウントを開く。
-
**[プロセス オートメーション]の[Runbook]**を開く。
-
**[+Runbook]**の作成を押す。
-
適当な名前を付け、[Runbookの種類]は「PowerShell」を選択し、**[作成]**を押す。
【参考】スクリーンショット
- 作成したRunbookの画面から、**[+編集]**を押す。
【参考】スクリーンショット
- 自動停止シェルを書いて、**[保存]**を押す。
【参考】スクリーンショット
- **[公開]を押す。
なお、[テストウィンドウ]**を押すとテスト実行することができます。
自動停止シェル
AutomationからAzureRMのコマンドを実行する場合、認証処理が必要です。
Microsoftの記事に認証処理のサンプルがあったので、そのままパクりました。
# 初期設定
$appGatewayName = "Application Gatewayの名前を入力する"
$resourceGroupName = "Application Gatewayのリソースグループを入力する"
# AzureRM認証処理
Disable-AzureRmContextAutosave -Scope Process
$connection = Get-AutomationConnection -Name AzureRunAsConnection
while (!($connectionResult) -And ($logonAttempt -le 10)) {
Write-Output ("* Trying AzureRM authentication...")
$logonAttempt++
$connectionResult = Connect-AzureRmAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationID $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint
Start-Sleep -Seconds 10
}
# ApplicationGatewayの情報を取得
$AppGw = Get-AzureRmApplicationGateway -Name $appGatewayName -ResourceGroupName $resourceGroupName
Write-Output ("* " + $appGatewayName + " is " + $AppGw.OperationalState + ".")
# 稼働中なら停止する
if ($AppGw.OperationalState -eq "Running") {
Write-Output ("* Stopping " + $appGatewayName + "...")
Stop-AzureRmApplicationGateway -ApplicationGateway $AppGw
}
Write-Output ("* Done.")
スケジューリングの設定
-
作成したRunbookを開く。
-
**[リソース]の[スケジュール]**を開く。
-
[+スケジュールの追加]を押す。
【参考】スクリーンショット
- スケジュールを作成します。
【参考】スクリーンショット
- **[スケジュール]**から、次の実行タイミングを確認します。
【参考】スクリーンショット
おわりに
前回記事のStop-AzureRmApplicationGateway
書くだけでいけるっしょ!と軽い気持ちで作り始めたら、意外とハマってしまいました。
何はともあれ、これで止め忘れは無くなったので良かったです。
Automationも使えたので、いろいろな運用を自動化させてみたいと思います。
参考記事
- Automation アカウントを作成する
- 初めての PowerShell Runbook
- Azure Automation に AzureRM.Network モジュールがない件