{"id":94740,"date":"2022-07-25T17:00:41","date_gmt":"2022-07-25T17:00:41","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=94740"},"modified":"2022-07-19T22:01:41","modified_gmt":"2022-07-19T22:01:41","slug":"creating-azure-policy-to-setup-virtual-machine-auto-shutdown","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/creating-azure-policy-to-setup-virtual-machine-auto-shutdown\/","title":{"rendered":"Creating Azure Policy to setup Virtual Machine Auto-Shutdown"},"content":{"rendered":"<p>\u00a0The Auto-Shutdown policy is another important policy to ensure our virtual machines don&#8217;t expend more than what we planned for them. If we have a time window to use the virtual machines, the auto-shutdown policy can deactivate them at the right time.<\/p>\n<p>We need to discover the deep internal details about the auto-shutdown configuration before creating the policy. The method we can use is to set this configuration and export the virtual machine as a template. We change the configuration to on and off, export and check the difference.<\/p>\n<p>After testing the export template you will discover that Azure creates an object of type <em>Microsoft.DevTestLab\/schedules<\/em> when the auto-shutdown configuration is defined. Azure creates this object the first time we enable the auto-shutdown configuration. However, when we disable it, Azure doesn&#8217;t drop the object, it only disables it. Azure enables the existing object again when the auto-shutdown configuration.<\/p>\n<h2>Schedules and Properties<\/h2>\n<p>We need to check the <em>Microsoft.DevTestLab\/schedules. <\/em>We will test the property <em>targetResourceId<\/em> to ensure the schedule belongs to the correct machine machine and the <em>status<\/em> property to check if the schedule object is enabled or not.<\/p>\n<p>The policy require us to use the full name of the fields. The documentation about the full name of the fields is not always available. It&#8217;s a challenge to find them. After a lot of research, I discovered the full name of the fields:<\/p>\n<p><strong>Status:<\/strong> Microsoft.DevTestLab\/schedules\/status<\/p>\n<p><strong>TargetRersourceId:<\/strong> Microsoft.DevTestLab\/schedules\/targetResourceId<\/p>\n<p>A few weeks ago I wrote about <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/parameterizing-azure-policy-and-ensuring-azure-sql-ad-authentication\/\">parameterizing Azure policies<\/a>. We can apply the same concepts to the auto-shutdown policy. The local IT teams will choose to enable the auto-shutdown configuration automatically or only audit when the configuration is enabled or not. The policy will allow them to choose between <strong>AuditIfNotExists<\/strong> or <strong>DeployIfNotExists<\/strong><\/p>\n<p>The policy will be like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\" class=\"crayon:false\">{\r\n     <span style=\"color: #007700\">\"parameters\"<\/span>: {\r\n          <span style=\"color: #007700\">\"effect\"<\/span>: {\r\n            <span style=\"color: #007700\">\"type\"<\/span>: <span style=\"background-color: #fff0f0\">\"String\"<\/span>,\r\n            <span style=\"color: #007700\">\"metadata\"<\/span>: {\r\n              <span style=\"color: #007700\">\"displayName\"<\/span>: <span style=\"background-color: #fff0f0\">\"Effect\"<\/span>,\r\n              <span style=\"color: #007700\">\"description\"<\/span>: <span style=\"background-color: #fff0f0\">\"Enable or disable the execution of the policy\"<\/span>\r\n            },\r\n            <span style=\"color: #007700\">\"allowedValues\"<\/span>: [\r\n              <span style=\"background-color: #fff0f0\">\"DeployIfNotExists\"<\/span>,\r\n              <span style=\"background-color: #fff0f0\">\"auditIfNotExists\"<\/span>,\r\n              <span style=\"background-color: #fff0f0\">\"Disabled\"<\/span>\r\n            ],\r\n            <span style=\"color: #007700\">\"defaultValue\"<\/span>: <span style=\"background-color: #fff0f0\">\"DeployIfNotExists\"<\/span>\r\n          }\r\n     },\r\n     <span style=\"color: #007700\">\"policyRule\"<\/span>: {\r\n       <span style=\"color: #007700\">\"if\"<\/span>: {\r\n         <span style=\"color: #007700\">\"allOf\"<\/span>: [\r\n           {\r\n             <span style=\"color: #007700\">\"field\"<\/span>: <span style=\"background-color: #fff0f0\">\"type\"<\/span>,\r\n             <span style=\"color: #007700\">\"equals\"<\/span>: <span style=\"background-color: #fff0f0\">\"Microsoft.Compute\/virtualMachines\"<\/span>\r\n           }\r\n         ]\r\n       },\r\n       <span style=\"color: #007700\">\"then\"<\/span>: {\r\n         <span style=\"color: #007700\">\"effect\"<\/span>: <span style=\"background-color: #fff0f0\">\"[parameters('effect')]\"<\/span>,\r\n         <span style=\"color: #007700\">\"details\"<\/span>: {\r\n           <span style=\"color: #007700\">\"type\"<\/span>: <span style=\"background-color: #fff0f0\">\"microsoft.devtestlab\/schedules\"<\/span>,\r\n           <span style=\"color: #007700\">\"roleDefinitionIds\"<\/span>: [\r\n             <span style=\"background-color: #fff0f0\">\"\/providers\/Microsoft.Authorization\/roleDefinitions\/9980e02c-c2be-4d73-94e8-173b1dc7cf3c\"<\/span>\r\n           ],\r\n           <span style=\"color: #007700\">\"existenceCondition\"<\/span>: {\r\n             <span style=\"color: #007700\">\"allOf\"<\/span>: [\r\n               {\r\n                 <span style=\"color: #007700\">\"field\"<\/span>: <span style=\"background-color: #fff0f0\">\"Microsoft.DevTestLab\/schedules\/targetResourceId\"<\/span>,\r\n                 <span style=\"color: #007700\">\"equals\"<\/span>: <span style=\"background-color: #fff0f0\">\"[field('id')]\"<\/span>\r\n               },\r\n               {\r\n                 <span style=\"color: #007700\">\"field\"<\/span>: <span style=\"background-color: #fff0f0\">\"Microsoft.DevTestLab\/schedules\/status\"<\/span>,\r\n                 <span style=\"color: #007700\">\"equals\"<\/span>: <span style=\"background-color: #fff0f0\">\"Enabled\"<\/span>\r\n               }\r\n             ]\r\n           },\r\n           <span style=\"color: #007700\">\"deployment\"<\/span>: {\r\n             <span style=\"color: #007700\">\"properties\"<\/span>: {\r\n               <span style=\"color: #007700\">\"mode\"<\/span>: <span style=\"background-color: #fff0f0\">\"incremental\"<\/span>,\r\n               <span style=\"color: #007700\">\"name\"<\/span>: <span style=\"background-color: #fff0f0\">\"Default\"<\/span>,\r\n               <span style=\"color: #007700\">\"template\"<\/span>: {\r\n                 <span style=\"color: #007700\">\"$schema\"<\/span>: <span style=\"background-color: #fff0f0\">\"https:\/\/schema.management.azure.com\/schemas\/2019-04-01\/deploymentTemplate.json#\"<\/span>,\r\n                 <span style=\"color: #007700\">\"contentVersion\"<\/span>: <span style=\"background-color: #fff0f0\">\"1.0.0.0\"<\/span>,\r\n                 <span style=\"color: #007700\">\"parameters\"<\/span>: {\r\n                   <span style=\"color: #007700\">\"vmName\"<\/span>: {\r\n                     <span style=\"color: #007700\">\"defaultValue\"<\/span>: <span style=\"background-color: #fff0f0\">\"devMaltaStation\"<\/span>,\r\n                     <span style=\"color: #007700\">\"type\"<\/span>: <span style=\"background-color: #fff0f0\">\"String\"<\/span>\r\n                   }\r\n                 },\r\n                 <span style=\"color: #007700\">\"variables\"<\/span>: {\r\n                   <span style=\"color: #007700\">\"rId\"<\/span>: <span style=\"background-color: #fff0f0\">\"[resourceId('Microsoft.Compute\/virtualMachines', parameters('vmName'))]\"<\/span>,\r\n                   <span style=\"color: #007700\">\"schName\"<\/span>: <span style=\"background-color: #fff0f0\">\"[concat('shutdown-computevm-',parameters('vmName'))]\"<\/span>\r\n                 },\r\n                 <span style=\"color: #007700\">\"resources\"<\/span>: [\r\n                   {\r\n                     <span style=\"color: #007700\">\"type\"<\/span>: <span style=\"background-color: #fff0f0\">\"Microsoft.DevTestLab\/schedules\"<\/span>,\r\n                     <span style=\"color: #007700\">\"apiVersion\"<\/span>: <span style=\"background-color: #fff0f0\">\"2018-09-15\"<\/span>,\r\n                     <span style=\"color: #007700\">\"name\"<\/span>: <span style=\"background-color: #fff0f0\">\"[variables('schName')]\"<\/span>,\r\n                     <span style=\"color: #007700\">\"location\"<\/span>: <span style=\"background-color: #fff0f0\">\"northeurope\"<\/span>,\r\n                     <span style=\"color: #007700\">\"properties\"<\/span>: {\r\n                       <span style=\"color: #007700\">\"status\"<\/span>: <span style=\"background-color: #fff0f0\">\"Enabled\"<\/span>,\r\n                       <span style=\"color: #007700\">\"taskType\"<\/span>: <span style=\"background-color: #fff0f0\">\"ComputeVmShutdownTask\"<\/span>,\r\n                       <span style=\"color: #007700\">\"dailyRecurrence\"<\/span>: {\r\n                         <span style=\"color: #007700\">\"time\"<\/span>: <span style=\"background-color: #fff0f0\">\"0000\"<\/span>\r\n                       },\r\n                       <span style=\"color: #007700\">\"timeZoneId\"<\/span>: <span style=\"background-color: #fff0f0\">\"Central European Standard Time\"<\/span>,\r\n                       <span style=\"color: #007700\">\"notificationSettings\"<\/span>: {\r\n                         <span style=\"color: #007700\">\"status\"<\/span>: <span style=\"background-color: #fff0f0\">\"Disabled\"<\/span>,\r\n                         <span style=\"color: #007700\">\"timeInMinutes\"<\/span>: <span style=\"color: #0000dd;font-weight: bold\">30<\/span>,\r\n                         <span style=\"color: #007700\">\"notificationLocale\"<\/span>: <span style=\"background-color: #fff0f0\">\"en\"<\/span>\r\n                       },\r\n                       <span style=\"color: #007700\">\"targetResourceId\"<\/span>: <span style=\"background-color: #fff0f0\">\"[variables('rId')]\"<\/span>\r\n                     }\r\n                   }\r\n                 ]\r\n               },\r\n               <span style=\"color: #007700\">\"parameters\"<\/span>: {\r\n                 <span style=\"color: #007700\">\"vmName\"<\/span>: {\r\n                   <span style=\"color: #007700\">\"value\"<\/span>: <span style=\"background-color: #fff0f0\">\"[field('name')]\"<\/span>\r\n                 }\r\n               }\r\n             }\r\n           }\r\n         }\r\n       }\r\n     }\r\n}\r\n<\/pre>\n<\/div>\n<p>\nOnce created and assigned, you can check the policy to verify the result or apply remediation. I wrote a bit about this on the blog about evaluating <a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/evaluation-policies-in-a-tenant-wide-level-and-more-azure-tricks\/\">Azure Policy tenant-wide<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Summary<\/h2>\n<p>The auto-shutdown policy is one more important policy we should use in our Azure environment. Making it as a parameterized policy is one additional benefit for our environment.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0The Auto-Shutdown policy is another important policy to ensure our virtual machines don&#8217;t expend more than what we planned for them. If we have a time window to use the virtual machines, the auto-shutdown policy can deactivate them at the right time. We need to discover the deep internal details about the auto-shutdown configuration before&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5364,145786,5383],"coauthors":[6810],"class_list":["post-94740","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-azure","tag-azure-policy","tag-virtual-machines"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=94740"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94740\/revisions"}],"predecessor-version":[{"id":94746,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/94740\/revisions\/94746"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=94740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=94740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=94740"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=94740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}