首页 > 解决方案 > Automate the start/stop VMs during off-hours in Azure using terraform

问题描述

I'm trying to automate the start/stop VMs during off-hours in Azure using Terraform. This is the way of automating it in Azure portal https://docs.microsoft.com/en-us/azure/automation/automation-solution-vm-management I have done it once in azure portal but I want to do the same using terraform. I've searched days to find out how to do this. I've found the same question asked by someone else before Create Azure Automation Start/Stop solution through Terraform but there was only one answer to that which is it's not possible since the Microsoft solution requires parameters on the runbooks, and there isn't any attributes in the provider to add parameters. But I'm not quite convinced with the answer.

I'm newish in Terraform and I know some resources like azurerm_automation_job_schedule and azurerm_automation_runbook must be used, but I couldn't figure out the whole module to do this. Has anyone done anything like this before?

标签: azureterraformazure-automationterraform-provider-azure

解决方案


我认为这篇文章现在有点老了,但如果这会帮助有人试图找出解决方案来为运行手册传递参数,我会做出回应。您可以在此资源提供程序“azurerm_automation_job_schedule”中传递所需的参数。请注意 Parameters 属性,这是我们可以传递所需参数的方式。您可以参考此链接了解更多详情。 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]
}

在此处输入图像描述

以下是 VM 启动/停止作业计划资源提供程序“azurerm_automation_schedule”和“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]
}

推荐阅读