3

I'm trying to set up machines to be automatically start/stopped using the newish Azure Automation add-in (https://learn.microsoft.com/en-us/azure/automation/automation-solution-vm-management) with this being set up by Terraform.

I can create the automation account but I don't know how to create the start-stop functionality, can someone help fill in the blanks?

Stu
  • 2,426
  • 2
  • 26
  • 42

2 Answers2

2

the AzureRM provider can manage aspects of runbooks. If you have a look at the documentation here. Using azurerm_automation_runbook and azurerm_automation_schedule you can create and schedule runbooks. The Microsoft solution requires parameters on the runbooks, I don't see any attributes in the provider to add parameters so this may not be possible.

Tim Tharratt
  • 1,251
  • 1
  • 10
  • 18
1

You can pass the required parameter in this resource provider "azurerm_automation_job_schedule". Please note the Parameters attribute in the below code this is how we can pass the required parameter. You can refer this link for more details. https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/automation_job_schedule

resource "azurerm_automation_job_schedule" "startvm_sched" {
  resource_group_name     = "IndraTestRG"
  automation_account_name = "testautomation"
  schedule_name           = azurerm_automation_schedule.scheduledstartvm.name
  runbook_name            = azurerm_automation_runbook.startstopvmrunbook.name
   parameters = {
    action        = "Start"
  }
  depends_on = [azurerm_automation_schedule.scheduledstartvm]
}

Below is the complete code for VM Start/Stop job schedule resource provider "azurerm_automation_schedule" and "azurerm_automation_job_schedule"

resource "azurerm_automation_schedule" "scheduledstartvm" {
  name                    = "StartVM"
  resource_group_name     = "IndraTestRG"
  automation_account_name = "testautomation"
  frequency               = "Day"
  interval                = 1
  timezone                = "America/Chicago"
  start_time              = "2021-09-20T13:00:00Z"
  description             = "Run every day"
}

resource "azurerm_automation_job_schedule" "startvm_sched" {
  resource_group_name     = "IndraTestRG"
  automation_account_name = "testautomation"
  schedule_name           = azurerm_automation_schedule.scheduledstartvm.name
  runbook_name            = azurerm_automation_runbook.startstopvmrunbook.name
   parameters = {
    action        = "Start"
  }
  depends_on = [azurerm_automation_schedule.scheduledstartvm]
}

resource "azurerm_automation_schedule" "scheduledstopvm" {
  name                    = "StopVM"
  resource_group_name     = "IndraTestRG"
  automation_account_name = "testautomation"
  frequency               = "Day"
  interval                = 1
  timezone                = "America/Chicago"
  start_time              = "2021-09-20T10:30:00Z"
  description             = "Run every day"
}

resource "azurerm_automation_job_schedule" "stopvm_sched" {
  resource_group_name     = "IndraTestRG"
  automation_account_name = "testautomation"
  schedule_name           = azurerm_automation_schedule.scheduledstopvm.name
  runbook_name            = azurerm_automation_runbook.startstopvmrunbook.name
  parameters = {
    action        = "Stop"
  }
  depends_on = [azurerm_automation_schedule.scheduledstopvm]
}
Indra
  • 21
  • 3