Don’t run out of credits on Azure! How to shutdown Azure Virtual Machines automatically with Terraform
If you are distracted and forgetful like me, you probably already forgot an Azure Virtual Machine running without need and just figured it out when your Azure credits were gone. To avoid this, check a simple tip on how to solve this problem with Terraform.
Photo by Tristan Gassert on Unsplash
In my previous post, I have already shown how to create an Azure Virtual Machine using Terraform, now I’m adding this feature, which, for me, is very important and helps me to not spend all my Azure Credits in a forgotten VM 🙃.
What is Azure VM Auto-Shutdown?
Azure VM Auto-Shutdown is like setting a timer for your Azure VM. Auto-shutdown allows you to set a specific time so your Virtual Machine will turn off. This is a handful when you create VMs for tests, or even in development/QA environments, where you don’t need your VMs running at night when your team is not working.
Terraform resource for auto-shutdown of an Azure VM
To make it work, we will use the Terraform resource azurerm_dev_test_global_vm_shutdown_schedule. This is part of the Azure Terraform provider. The usage is quite simple, as we can see below:
The most important information required are the VM id, daily_recurrence_time, and timezone. Based on this information your VM will be shut down at the time you have defined.
Complete Terraform to create an Azure VM with auto-shutdown
To facilitate the understanding of how to integrate it in a real-life scenario, I have implemented the shutdown process in the following complete Terraform file:
Conclusion
Below it is the link to my GitHub repository where I am creating a library to have all references related to Terraform. I hope that this post despite being simple might be helpful! wesleycamargo/terraform (github.com)
