azure - 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?
解决方案
我认为这篇文章现在有点老了,但如果这会帮助有人试图找出解决方案来为运行手册传递参数,我会做出回应。您可以在此资源提供程序“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]
}
推荐阅读
- typescript - 如何在打字稿中指定以下类型?
- elasticsearch - filebeat:创建模板时出错:字段包含键
- javascript - 在不知道密钥的情况下从 Firebase 中移除一个孩子
- azure-active-directory - 代表用户调用 MS Graph API 时未找到租户“xxx”
- powershell - 使用正则表达式匹配 Powershell 中的路径
- applescript - 进程未在后台运行
- webserver - 500 lighttpd 中的内部服务器,同时调用用“C”编写的 CGI
- function - 使用 geofirestore 在云功能中通知附近的用户
- asp.net-core - .NETCore PasswordHasher.HashPassword 不使用 TUser 用户,但为什么包含在方法签名中?
- c# - C# 我想将文本框的值与组合框中的所有项目进行比较。单击按钮时出现异常