0
votes

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."
}
1
Could you share code of your module run_command?Andriy Bilous
done. I found the module on the repo. thank you, I'm pulling my hair out here!BarrySDCA

1 Answers

1
votes

In the Argument Reference of Terraform template azurerm_virtual_machine_extension, the argument virtual_machine_name is retired and has been replaced with virtual_machine_id. If you are using the latest Terraform

Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/azurerm v2.43.0

you can download the module locally then modify the related code manually and change the source as the local path when you run the terraform init/plan/apply.

Moreover, I found that the deprecated interpolation expression is still used in that module. Terraform 0.11 and earlier required all non-constant expressions to be provided via interpolation syntax, but this pattern is now deprecated for Terraform 0.12 and later.

For example, I assume you have copied the module code in the path ./modules/run_command. Then modify the related code as required.

In the child module run_command main.tf.

resource "azurerm_virtual_machine_extension" "windows" {
  count                      = lower(var.os_type) == "windows" ? 1 : 0
  name                       = "${var.virtual_machine_name}-run-command"
  
  virtual_machine_id         = data.azurerm_virtual_machine.main.id
  publisher                  = "Microsoft.CPlat.Core"
  type                       = "RunCommandWindows"
  type_handler_version       = "1.1"
  auto_upgrade_minor_version = true
  settings                   = jsonencode(local.settings_windows)
  tags                       = var.tags
}

In the parent module main.tf.

module "run_command" {
  source               = "./modules/run_command"
  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
}