Auto Shutdown using Terraform and azurerm_dev_test_global_vm_shutdown_schedule

azurerm_dev_test_global_vm_shutdown_schedule

As more organizations shift towards cloud-based infrastructures, optimizing resource usage and minimizing operational costs are essential. One effective way to achieve this is by scheduling Azure Virtual Machines (VMs) to automatically shut down during periods of inactivity. In this blog post, I will demonstrate how to configure Azure VMs for auto shutdown using Terraform and the azurerm_dev_test_global_vm_shutdown_schedule resource.

The Azure Virtual Machine auto-shutdown feature is a powerful tool that enables the automatic configuration of virtual machines to shut down every day at a specific time. This feature is especially useful when running test and development virtual machines, as it helps to reduce costs and optimize the utilization of resources.

The below code using Terraform azurerm_dev_test_global_vm_shutdown_schedule examples will configure auto-shutdown every day at 10 PM, AUS Eastern Standard Time. 

Prerequisites using azurerm_dev_test_global_vm_shutdown_schedule

Before I begin, make sure you have the following prerequisites:

  • An Azure subscription
  • Terraform installed (version 1.5.* and higher)
  • Azure CLI installed and authenticated with your Azure subscription
  • Basic knowledge of Terraform and Azure
  • Set up the Terraform configuration

azurerm_dev_test_global_vm_shutdown_schedule code

Main.tf file


# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "this" {
  name     = var.resource_group_name
  location = var.location
}

data "azurerm_virtual_network" "this" {
  name                = var.virtual_network_name
  resource_group_name = var.resource_group_name
}

data "azurerm_subnet" "this" {
  name                 = var.subnet_name
  resource_group_name  = var.resource_group_name
  virtual_network_name = var.virtual_network_name
}

resource "azurerm_network_interface" "this" {
  name                = var.network_interface_name
  location            = azurerm_resource_group.this.location
  resource_group_name = azurerm_resource_group.this.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.this.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "this" {
  name                = var.vm_name
  resource_group_name = azurerm_resource_group.this.name
  location            = azurerm_resource_group.this.location
  size                = var.vm_size
  admin_username      = var.admin_username
  admin_password      = var.admin_password
  network_interface_ids = [
    azurerm_network_interface.this.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "microsoftwindowsserver"
    offer     = "windowsserver"
    sku       = "2022-datacenter-azure-edition"
    version   = "latest"
  }

}

resource "azurerm_dev_test_global_vm_shutdown_schedule" "this" {
  virtual_machine_id = azurerm_windows_virtual_machine.this.id
  location           = azurerm_resource_group.this.location
  enabled            = true

  daily_recurrence_time = var.daily_recurrence_time
  timezone              = var.timezone
// optionally use notification_settings if required notification_settings { enabled = var.notification_status } }

The "main.tf" file contains the main configuration of your Terraform infrastructure, which includes provider configuration, resource creation, and data sources. In this case, it consists of the following sections:

  1. Terraform and Provider Configuration: It specifies the required providers (in this case, azurerm) and its version. The azurerm provider is configured with the features block, which is currently empty.

  2. Resource Group: A resource block creates an Azure Resource Group using the azurerm_resource_group resource type. It takes the name and location from the variables defined in "variables.tf".

  3. Data Sources: Two data source blocks, data "azurerm_virtual_network" and data "azurerm_subnet", fetch information about the existing virtual network and subnet based on the names provided in "variables.tf".

  4. Network Interface: A resource block creates an Azure Network Interface using the azurerm_network_interface resource type. It associates the interface with the fetched subnet data and sets the private IP address allocation method to "Dynamic".

  5. Windows Virtual Machine: A resource block creates an Azure Windows Virtual Machine using the azurerm_windows_virtual_machine resource type. The block configures the VM with the specified size, admin credentials, network interface, OS disk, and source image reference.

  6. VM Shutdown Schedule: A resource block creates a VM shutdown schedule using the azurerm_dev_test_global_vm_shutdown_schedule resource type. The block configures the shutdown schedule with the virtual machine ID, daily recurrence time, timezone, and notification settings.

variables.tf file


variable "resource_group_name" {
  description = "The name of the resource group where the VM will be created."
  type        = string
}

variable "location" {
  description = "The location where the VM and related resources will be created."
  type        = string
}

variable "vm_name" {
  description = "The name of the Azure Virtual Machine."
  type        = string
}

variable "vm_size" {
  description = "The size (SKU) of the Azure Virtual Machine."
  type        = string
  default     = "Standard_B1s"
}

The "variables.tf" file defines input variables used throughout the Terraform configuration. It makes the configuration more modular and easier to maintain. In this case, it consists of the following variables:

  1. resource_group_name: The name of the resource group where the VM will be created.
  2. location: The location where the VM and related resources will be created.
  3. vm_name: The name of the Azure Virtual Machine.
  4. vm_size: The size (SKU) of the Azure Virtual Machine.

These variables are referenced in "main.tf" to make the configuration more flexible, customizable, and easier to maintain.

Conclusion

In this blog post, I've demonstrated how to configure Azure Virtual Machines for automatic shutdown using Terraform and the azurerm_dev_test_global_vm_shutdown_schedule resource. By implementing this solution, you can optimize your cloud infrastructure, reduce costs, and ensure that your VMs only consume resources when needed.

Remember to tailor the auto shutdown settings to your organization's requirements, and consider incorporating additional features such as notifications and webhooks for better resource management.

Happy optimizing!