【问题标题】:Automate the start/stop VMs during off-hours in Azure using terraform使用 terraform 在 Azure 的非工作时间自动启动/停止 VM
【发布时间】:2021-09-21 14:38:01
【问题描述】:

我正在尝试使用 TerraformAzure 的非工作时间自动启动/停止 VM。这是在 Azure 门户https://docs.microsoft.com/en-us/azure/automation/automation-solution-vm-management 中实现自动化的方式我在 azure 门户中做过一次,但我想使用 terraform 来做同样的事情。 我已经搜索了几天以找出如何做到这一点。我在Create Azure Automation Start/Stop solution through Terraform 之前发现了其他人提出的相同问题,但只有一个答案是不可能的,因为 Microsoft 解决方案需要运行手册上的参数,并且提供程序中没有任何要添加的属性参数。但我不太相信这个答案。

我是 Terraform 的新手,我知道必须使用像 azurerm_automation_job_scheduleazurerm_automation_runbook 这样的资源,但我无法弄清楚整个模块可以做到这一点。以前有人做过这样的事吗?

【问题讨论】:

  • 当简单的 azure cli 命令可以解决您的问题时,为什么要使用 terraform?保持简单
  • 我不能。我们在 Azure 上为不同的项目运行了许多 VM,我这样做是为了降低它们的成本。整个过程应该使用 Terraform 自动化。
  • 嗨,我也想做同样的事情,你找到什么了吗?
  • 看来上一个问题的答案是对的;这是不可能的。

标签: azure terraform azure-automation terraform-provider-azure


【解决方案1】:

我认为这篇文章现在有点老了,但如果这将帮助有人试图找出解决方案来为运行手册传递参数,我会做出回应。您可以在此资源提供程序“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]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多