LoginSignup
1
1

More than 5 years have passed since last update.

Azure Resource Manager(ARM)の小ネタ:仮想マシンの自動シャットダウン設定をJSONテンプレートから行う

Last updated at Posted at 2017-01-20

はじめに

2016年11月にAzureポータルから仮想マシンの自動シャットダウンが設定できるようになりました。
テスト環境や開発環境などを指定時間になったら強制的にシャットダウンさせることによって、不要なコストアップを避ける事ができます。

詳しくはAnnouncing auto-shutdown for VMs using Azure Resource Manager(英語)もしくは、MSさとうなおきさんの【Azureなう 12/2号】本番環境でも仮想マシンを自動シャットダウンあたりを参照ください。

実行例

ポータル画面上で以下の状態になっているものを。。。
image

ポータル画面からは変更せずにAzure PowershellからNew-AzureRmResourceGroupDeploymentコマンドを使って実行します。

PS C:\> New-AzureRmResourceGroupDeployment -ResourceGroupName KTKRLINUX-rg -TemplateFile .\autoshutdown-template.json -TemplateParameterFile .\autoshutdown-parameters.json


DeploymentName          : autoshutdown-template
ResourceGroupName       : KTKRLINUX-rg
ProvisioningState       : Succeeded
Timestamp               : 2017/01/20 6:36:11
Mode                    : Incremental
TemplateLink            : 
Parameters              : 
                          Name             Type                       Value     
                          ===============  =========================  ==========
                          vmName           String                     KTKRLINUXVM01
                          shutdownStatus   String                     Enabled   
                          shutdownTime     String                     1700      
                          shutdownTimezone  String                     Tokyo Standard Time
                          shutdownNotification  String                     Disabled  
                          shutdownNotificationTime  Int                        15        
                          shutdownNotificationWebHook  String                     http://example.com/

Outputs                 : 
DeploymentDebugLogLevel : 

ポータル画面をリロードして自動シャットダウンの設定が変更されたことを確認します。
image

JSONテンプレート

今回紹介したJSONテンプレート本体とサンプルパラメータを以下に示します。

テンプレート本体

autoshutdown-template.json
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string"
        },
        "shutdownStatus": {
            "type": "string",
            "defaultValue": "Enabled"
        },
        "shutdownTime": {
            "type": "string"
        },
        "shutdownTimezone": {
            "type": "string",
            "defaultValue": "Tokyo Standard Time"
        },
        "shutdownNotification": {
            "type": "string",
            "defaultValue": "Disabled"
        },
        "shutdownNotificationTime": {
            "type": "int",
            "defaultValue": 15
        },
        "shutdownNotificationWebHook": {
            "type": "string",
            "defaultValue": "http://example.com/"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.DevTestLab/schedules",
            "name": "[concat('shutdown-computevm-',parameters('vmName'))]",
            "apiVersion": "2016-05-15",
            "location": "[resourceGroup().location]",
            "dependsOn": [],
            "properties": {
                "status": "[parameters('shutdownStatus')]",
                "taskType": "ComputeVmShutdownTask",
                "dailyRecurrence": {
                    "time": "[parameters('shutdownTime')]"
                },
                "timeZoneId": "[parameters('shutdownTimezone')]",
                "notificationSettings": {
                    "status": "[parameters('shutdownNotification')]",
                    "timeInMinutes": "[parameters('shutdownNotificationTime')]",
                    "webhookUrl": "[parameters('shutdownNotificationWebHook')]"
                },
                "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines',parameters('vmName'))]"
            }
        }
    ]
}

サンプルパラメータ

autoshutdown-parameters.json
{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "shutdownStatus": {
      "value": "Enabled"
    },
    "vmName": {
      "value": "TESTVM01"
    },
    "shutdownTime": {
      "value": "1900"
    },
    "shutdownNotification": {
      "value": "Disabled"
    }
  }
}

おわりに

JSONテンプレートはAzure Powershellほどではありませんが、copyIndex()などの機能を使うことによって複数の仮想マシンに対して同じ操作を一括設定ができたりします。
今回紹介した、単独仮想マシンに対する設定変更レベルであれば、ポータル画面から実施したほうが早いですが、色々と拡張することにより効率的に利用できるのではと思います。

1
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
1
1