I have made a very simple script to run a command in a windows VM on Azure. But I am getting these errors:
Error: Missing required argument
on .terraform\modules\run_command\main.tf line 23, in resource "azurerm_virtual_machine_extension" "linux": 23: resource "azurerm_virtual_machine_extension" "linux" {
The argument "virtual_machine_id" is required, but no definition was found.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 26, in resource "azurerm_virtual_machine_extension" "linux": 26: location
= "${data.azurerm_resource_group.main.location}"An argument named "location" is not expected here.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 27, in resource "azurerm_virtual_machine_extension" "linux": 27:
resource_group_name = "${data.azurerm_resource_group.main.name}"An argument named "resource_group_name" is not expected here.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 28, in resource "azurerm_virtual_machine_extension" "linux": 28:
virtual_machine_name = "${data.azurerm_virtual_machine.main.name}"An argument named "virtual_machine_name" is not expected here.
Error: Missing required argument
on .terraform\modules\run_command\main.tf line 37, in resource "azurerm_virtual_machine_extension" "windows": 37: resource "azurerm_virtual_machine_extension" "windows" {
The argument "virtual_machine_id" is required, but no definition was found.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 40, in resource "azurerm_virtual_machine_extension" "windows": 40: location
= "${data.azurerm_resource_group.main.location}"An argument named "location" is not expected here.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 41, in resource "azurerm_virtual_machine_extension" "windows": 41:
resource_group_name = "${data.azurerm_resource_group.main.name}"An argument named "resource_group_name" is not expected here.
Error: Unsupported argument
on .terraform\modules\run_command\main.tf line 42, in resource "azurerm_virtual_machine_extension" "windows": 42:
virtual_machine_name = "${data.azurerm_virtual_machine.main.name}"
However, if I remove the "unexpected" argument, I get another error that it's missing!
any advice? thank you much
provider "azurerm" {
tenant_id = "..."
subscription_id = "..."
features {}
}
# <https://registry.terraform.io/modules/innovationnorway/vm-run-command/azurerm/latest>
module "run_command" {
source = "innovationnorway/vm-run-command/azurerm"
resource_group_name = "dc-rg"
virtual_machine_name = "AD1"
os_type = "windows"
script = <<EOF
Install-Module -Name PSWindowsUpdate -Force -AllowClobber
Get-WUInstall -WindowsUpdate -AcceptAll -UpdateType Software -IgnoreReboot
Get-WUInstall -MicrosoftUpdate -AcceptAll -IgnoreUserInput -IgnoreReboot
EOF
}
thank you in advance. Much appreciated.
edited to include code for run_command, which I found on the repository:
run-command main.tf:
locals {
settings_windows = {
script = "${compact(concat(list(var.command), split("\n", var.script)))}"
fileUris = "${var.file_uris}"
}
settings_linux = {
commandToExecute = "${var.command}"
fileUris = "${var.file_uris}"
script = "${base64encode(var.script)}"
}
}
data "azurerm_resource_group" "main" {
name = "${var.resource_group_name}"
}
data "azurerm_virtual_machine" "main" {
name = "${var.virtual_machine_name}"
resource_group_name = "${data.azurerm_resource_group.main.name}"
}
resource "azurerm_virtual_machine_extension" "linux" {
count = "${lower(var.os_type) == "linux" ? 1 : 0}"
name = "${var.virtual_machine_name}-run-command"
location = "${data.azurerm_resource_group.main.location}"
resource_group_name = "${data.azurerm_resource_group.main.name}"
virtual_machine_name = "${data.azurerm_virtual_machine.main.name}"
publisher = "Microsoft.CPlat.Core"
type = "RunCommandLinux"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
protected_settings = "${jsonencode(local.settings_linux)}"
tags = "${var.tags}"
}
resource "azurerm_virtual_machine_extension" "windows" {
count = "${lower(var.os_type) == "windows" ? 1 : 0}"
name = "${var.virtual_machine_name}-run-command"
location = "${data.azurerm_resource_group.main.location}"
resource_group_name = "${data.azurerm_resource_group.main.name}"
virtual_machine_name = "${data.azurerm_virtual_machine.main.name}"
publisher = "Microsoft.CPlat.Core"
type = "RunCommandWindows"
type_handler_version = "1.1"
auto_upgrade_minor_version = true
settings = "${jsonencode(local.settings_windows)}"
tags = "${var.tags}"
}
run-command variables.tf:
variable "resource_group_name" {
description = "The name of the resource group."
}
variable "virtual_machine_name" {
description = "The name of the virtual machine."
}
variable "os_type" {
description = "Specifies the operating system type."
}
variable "command" {
default = ""
description = "Command to be executed."
}
variable "script" {
default = ""
description = "Script to be executed."
}
variable "file_uris" {
type = "list"
default = []
description = "List of files to be downloaded."
}
variable "timestamp" {
default = ""
description = "An integer, intended to trigger re-execution of the script when changed."
}
variable "tags" {
default = {}
description = "A mapping of tags to assign to the extension."
}