3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Azure] Azure サービスを定期的にスケールダウンしてコスト削減する ~Cosmos DB RU値の定期変更を例に~

Last updated at Posted at 2019-12-02

はじめに

クラウドの利用料を削減するために、夜間や休日などクラウドサービスの使用率が小さい時間はあらかじめスケールダウンやスケールインするよう設定したいですよね。
Azureには自動スケールという仕組みが用意されていますが、対応サービス数が限られていたり、スケールアウト/インのみの対応だったりします。

そんな中、スケール変更の別アプローチとして、Azure Automationというサービスを使う方法を紹介します。
Azure AutomationではPowerShellのコマンドを定期的に実行させることが可能です。

そして、Azureサービスの中には、PowerShellコマンドでスケール設定を変更できるものがあります。
例えば、App Service プランはこちらに提示されているPowerShellコマンドで-Tierオプションを使用するとスケール変更ができます。
また、本記事で具体例として紹介するCosmos DBのスケール変更(ここではRU値の変更を指しています)についてはこちらのドキュメントに記載されています。

よって、Azure Automationでスケール設定を変更するPowerShellコマンドを定期実行させることで、サービス使用率が低い時間帯はスケールを下げることを実現できます。

※Cosmos DBのRU値についてはこちら

1. Automation アカウントの作成

Azure Automationを初めて使う場合は、Automationアカウントを作成する必要があります。
Azureポータルの[すべてのサービス]で"Automation"などと検索し、[Automation アカウント]を選択します。
2019-11-08_18h33_21.png
[+追加]でAutomation アカウント作成画面に移動したら、任意の名前、サブスクリプション、リソースグループ、場所を設定し、[Azure 実行アカウントの作成]は"はい"のまま[作成]を押下します。
2019-11-08_18h52_43.png

これでAzure Automationを使用する準備ができました。
※Azure実行アカウントが作成できない場合はこちらのドキュメントを参考に権限をご確認ください。

2. Runbookの作成

続いて、Runbookなるものを作成します。
作成したAutomation アカウントに入ったら、[Runbook]を選択して、[+Runbookの作成]を押下します。
2019-11-08_19h06_15.png
作成画面に移動したら、任意の名前を入力し、[Runbookの種類]は[PowerShell]を選択します。説明は自由に記述してください。
2019-11-08_19h10_35.png
[作成]を押下するとコマンド編集画面に移動します。
ここでスケールを変更するためのPowerShellコマンドを入力して[保存]を押下します。

今回は例としてCosmos DBのRU値を変更するコマンドを示します。
設定変更したいAzureサービスに合わせて、以下のパラメータや変数、設定コマンド部分は変更してください。
また、設定コマンドによっては変更確認をスルーするために-Forceオプションなどが必要な場合もあるのでエラーが出た場合は気にしてみてください。

# Set input parameter
# Please change the following depending on the Azure Service
Param(
 [Parameter (Mandatory= $true)]
 [string]$resourceGroupName,
 [Parameter (Mandatory= $true)]
 [string]$accountName,
 [Parameter (Mandatory= $true)]
 [string]$databaseName,
 [Parameter (Mandatory= $true)]
 [string]$containerName,
 [Parameter (Mandatory= $true)]
 [Int32]$throughput
)

# Set variable
# Please change the following depending on the Azure Service
$resourceType = "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings"
$apiVersion = "2015-04-08"
$containerResourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName + "/throughput"
$properties = @{
    "resource"=@{"throughput"=$throughput}
}

# Stop runbook when an error occurs
$ErrorActionPreference = "Stop"

# Turn off autosaving Azure credentials
Disable-AzContextAutosave -Scope Process

# Connect to Azure
$connection = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -ServicePrincipal -Tenant $connection.TenantID `
    -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint

# Set Cosmos DB throughput
# Please change the following depending on the Azure Service
Set-AzResource -Force -ResourceType $resourceType `
    -ApiVersion $apiVersion -ResourceGroupName $resourceGroupName `
    -Name $containerResourceName -PropertyObject $properties 

3. モジュールのインポート、テスト、Runbookの発行

コマンドが完成したら、[テストウィンドウ]からコマンドの動作テストができます。
※テストといっても、実際にサービスのスケールは変更されるので注意しましょう。
ただし、上記をそのまま実行しても以下のようなエラーメッセージが表示されてしまうと思います。

失敗
The term 'Disable-AzContextAutosave' 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. (The term 'Disable-AzContextAutosave' 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.)

PowerShellからAzureのリソースを触るコマンド(上記でいうとDisable-AzContextAutosaveSet-AzResource)を使用するには、適切なAzモジュールのインポートが必要です。
必要なAzモジュールは、使用したいPowershellコマンドの公式ドキュメントに書いてあります。
2019-11-22_12h45_15.png

必要なAzモジュールが分かったら、[モジュールギャラリー]からインポートします。
今回の場合はAz.AccountsAz.Resourcesという2つのモジュールが必要になるので、検索窓から検索してインポートします。
2019-11-08_20h09_21.png

モジュールのインポートもできたら、改めてRunbookが動くかテストします。
[モジュールギャラリー]にいる場合は、[Runbook] - [ (作成中のRunbook) ]と選択して概要画面の[編集]を押下すれば、先ほどの編集画面に戻ります。
[テストウィンドウ]を押下し、テスト画面に移動します。
上記コマンド内の# Set input parameterで設定したパラメータを入力できるので、対象のCosmos DBとスケール設定の情報を入力して、[開始]を押下します。
2019-11-22_13h00_28.png
完了の文字が出たら、実際にCosmos DBを見に行きましょう。RU値が変更されているはずです。
RU値の変更が確認できたら、再度編集画面に戻り[公開]ボタンを押して作成したRunbookを発行します。
2019-11-08_20h48_13.png

4. スケジュールの設定

ここまで来たら、後はスケジュールの設定だけです。
スケジュールを設定するには該当のRunbook画面から[スケジュール] - [+ スケジュールの追加]と進みます。
スケジュールで、[+新しいスケジュールを作成します]でスケジュール設定を入力します。
[繰り返し]で"定期的"を選択すると、1日おきや1週おきなどRunbookの実行間隔を設定できます。
Runbookの有効期限も設定可能です。
2019-11-08_20h54_24.png

スケールダウンの設定だけでなく、もとに戻すスケジュール設定もお忘れなく。
2019-11-08_20h57_16.png

あとは時が来るのを待つのみです。
実行されると、"最近のジョブ"に実行履歴が表示されます。
2019-11-22_17h33_36.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?