Bylaw

Gallery › Compute › Azure Landing Zones Library

Deploy Virtual Machine Auto Shutdown Schedule

Converted Policy DeployIfNotExists Mode Indexed Version 1.0.0

Deploys an auto shutdown schedule to a virtual machine

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONDeploy-Vm-autoShutdown.alz_policy_definition.json · 194 lines
{
  "name": "Deploy-Vm-autoShutdown",
  "properties": {
    "description": "Deploys an auto shutdown schedule to a virtual machine",
    "displayName": "Deploy Virtual Machine Auto Shutdown Schedule",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Compute",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "EnableNotification": {
        "allowedValues": [
          "Disabled",
          "Enabled"
        ],
        "defaultValue": "Disabled",
        "metadata": {
          "description": "If notifications are enabled for this schedule (i.e. Enabled, Disabled).",
          "displayName": "Send Notification before auto-shutdown"
        },
        "type": "string"
      },
      "NotificationEmailRecipient": {
        "defaultValue": "",
        "metadata": {
          "description": "Email address to be used for notification",
          "displayName": "Email Address"
        },
        "type": "string"
      },
      "NotificationWebhookUrl": {
        "defaultValue": "",
        "metadata": {
          "description": "A notification will be posted to the specified webhook endpoint when the auto-shutdown is about to happen.",
          "displayName": "Webhook URL"
        },
        "type": "string"
      },
      "time": {
        "defaultValue": "0000",
        "metadata": {
          "description": "Daily Scheduled shutdown time. i.e. 2300 = 11:00 PM",
          "displayName": "Scheduled Shutdown Time"
        },
        "type": "String"
      },
      "timeZoneId": {
        "defaultValue": "UTC",
        "metadata": {
          "description": "The time zone ID (e.g. Pacific Standard time).",
          "displayName": "Time zone"
        },
        "type": "string"
      }
    },
    "policyRule": {
      "if": {
        "equals": "Microsoft.Compute/virtualMachines",
        "field": "type"
      },
      "then": {
        "details": {
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "EnableNotification": {
                  "value": "[parameters('EnableNotification')]"
                },
                "NotificationEmailRecipient": {
                  "value": "[parameters('NotificationEmailRecipient')]"
                },
                "NotificationWebhookUrl": {
                  "value": "[parameters('NotificationWebhookUrl')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "time": {
                  "value": "[parameters('time')]"
                },
                "timeZoneId": {
                  "value": "[parameters('timeZoneId')]"
                },
                "vmName": {
                  "value": "[field('name')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "outputs": {},
                "parameters": {
                  "EnableNotification": {
                    "defaultValue": "",
                    "metadata": {
                      "description": "If notifications are enabled for this schedule (i.e. Enabled, Disabled)."
                    },
                    "type": "string"
                  },
                  "NotificationEmailRecipient": {
                    "defaultValue": "",
                    "metadata": {
                      "description": "Email address to be used for notification"
                    },
                    "type": "string"
                  },
                  "NotificationWebhookUrl": {
                    "defaultValue": "",
                    "metadata": {
                      "description": "A notification will be posted to the specified webhook endpoint when the auto-shutdown is about to happen."
                    },
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "time": {
                    "defaultValue": "",
                    "metadata": {
                      "description": "Daily Scheduled shutdown time. i.e. 2300 = 11:00 PM"
                    },
                    "type": "string"
                  },
                  "timeZoneId": {
                    "defaultValue": "",
                    "metadata": {
                      "description": "The time zone ID (e.g. Pacific Standard time)."
                    },
                    "type": "string"
                  },
                  "vmName": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2018-09-15",
                    "location": "[parameters('location')]",
                    "name": "[concat('shutdown-computevm-',parameters('vmName'))]",
                    "properties": {
                      "dailyRecurrence": {
                        "time": "[parameters('time')]"
                      },
                      "notificationSettings": {
                        "emailRecipient": "[parameters('NotificationEmailRecipient')]",
                        "notificationLocale": "en",
                        "status": "[parameters('EnableNotification')]",
                        "timeInMinutes": 30,
                        "webhookUrl": "[parameters('NotificationWebhookUrl')]"
                      },
                      "status": "Enabled",
                      "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]",
                      "taskType": "ComputeVmShutdownTask",
                      "timeZoneId": "[parameters('timeZoneId')]"
                    },
                    "type": "Microsoft.DevTestLab/schedules"
                  }
                ],
                "variables": {}
              }
            }
          },
          "existenceCondition": {
            "allOf": [
              {
                "equals": "ComputeVmShutdownTask",
                "field": "Microsoft.DevTestLab/schedules/taskType"
              },
              {
                "equals": "[concat(resourceGroup().id,'/providers/Microsoft.Compute/virtualMachines/',field('name'))]",
                "field": "Microsoft.DevTestLab/schedules/targetResourceId"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "type": "Microsoft.DevTestLab/schedules"
        },
        "effect": "deployIfNotExists"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeploy-Vm-autoShutdown.bylaw.ps1 · 64 lines
# Converted from Deploy-Vm-autoShutdown.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deploy-Vm-autoShutdown' {

    Properties @{
        DisplayName = 'Deploy Virtual Machine Auto Shutdown Schedule'
        Description = 'Deploys an auto shutdown schedule to a virtual machine'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter EnableNotification @{
        Type          = 'string'
        AllowedValues = @('Disabled', 'Enabled')
        Default       = 'Disabled'
        DisplayName   = 'Send Notification before auto-shutdown'
        Description   = 'If notifications are enabled for this schedule (i.e. Enabled, Disabled).'
        JsonName      = 'EnableNotification'
    }
    Parameter NotificationEmailRecipient @{
        Type        = 'string'
        Default     = ''
        DisplayName = 'Email Address'
        Description = 'Email address to be used for notification'
        JsonName    = 'NotificationEmailRecipient'
    }
    Parameter NotificationWebhookUrl @{
        Type        = 'string'
        Default     = ''
        DisplayName = 'Webhook URL'
        Description = 'A notification will be posted to the specified webhook endpoint when the auto-shutdown is about to happen.'
        JsonName    = 'NotificationWebhookUrl'
    }
    Parameter Time @{
        Default     = '0000'
        DisplayName = 'Scheduled Shutdown Time'
        Description = 'Daily Scheduled shutdown time. i.e. 2300 = 11:00 PM'
    }
    Parameter TimeZoneId @{
        Type        = 'string'
        Default     = 'UTC'
        DisplayName = 'Time zone'
        Description = 'The time zone ID (e.g. Pacific Standard time).'
    }

    Resource 'Microsoft.Compute/virtualMachines'

    DeployIfNotExists 'Microsoft.DevTestLab/schedules' -Role 'Virtual Machine Contributor' {
        Require 'taskType, targetResourceId and name are as required' {
            Field taskType | Must -Be 'ComputeVmShutdownTask'
            Field targetResourceId | Must -Be (Arm '[concat(resourceGroup().id,''/providers/Microsoft.Compute/virtualMachines/'',field(''name''))]')
        }
        Deployment -TemplateFile './templates/Deploy-Vm-autoShutdown.json' -Parameters ([ordered]@{
                EnableNotification         = (Param EnableNotification)
                NotificationEmailRecipient = (Param NotificationEmailRecipient)
                NotificationWebhookUrl     = (Param NotificationWebhookUrl)
                location                   = (Arm '[field(''location'')]')
                time                       = (Param Time)
                timeZoneId                 = (Param TimeZoneId)
                vmName                     = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policy_definitions/Deploy-Vm-autoShutdown.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.