azurenoops / terraform-azurerm-overlays-virtual-machine

Terraform module overlay to deploy single or multiple SCCA compliant Virtual Machines of Linux or Windows with optional features to be used with Azure NoOps.
MIT License
1 stars 1 forks source link

Issue using custom images #16

Closed TomBeckett closed 1 year ago

TomBeckett commented 1 year ago

Is there an existing issue for this?

Greenfield/Brownfield provisioning

greenfield

Terraform Version

1.5.3

Module Version

1.1.0

AzureRM Provider Version

3.60.0

Affected Resource(s)/Data Source(s)

mod_virtual_machine

Terraform Configuration Files

module "mod_virtual_machine" {
  source  = "azurenoops/overlays-virtual-machine/azurerm"
  version = "1.1.0"

  # Resource Group, location, VNet and Subnet details
  existing_resource_group_name = data.azurerm_resource_group.shared_services_rg.name
  location                     = data.azurerm_resource_group.shared_services_rg.location
  deploy_environment           = var.env
  org_name                     = "pc"
  workload_name                = "${var.shared_services_prefix}-${var.env}-kibana"
  virtual_network_name         = local.shared_services_virtual_network_name
  subnet_name                  = "${var.shared_services_prefix}-${var.env}-default-snet"

  os_type                   = "windows"
  windows_distribution_name = "windows2019dc"
  virtual_machine_size      = "Standard_B2s"
  instances_count           = 1

  existing_network_security_group_name = data.azurerm_network_security_group.shared_services_nsg.name
  admin_username                       = var.kibana_rdp_username
  admin_password                       = var.kibana_rdp_password

  nsg_inbound_rules = [
    {
      name                   = "rdp"
      destination_port_range = "3389"
      source_address_prefix  = "*"
    }
  ]

  # az vm image list --offer windows-11 --location uksouth --all
  custom_image = {
    windows_11_pro = {
      publisher = "MicrosoftWindowsDesktop"
      offer     = "windows-11"
      sku       = "win11-22h2-pro"
      version   = "latest"
    }
  }
}

tfvars variables values

N/A

Debug Output/Panic Output

Error: Invalid index
│ 
│   on .terraform/modules/mod_virtual_machine/resources.virtual.machine.tf line 182, in resource "azurerm_windows_virtual_machine" "win_vm":
│  182:       publisher = var.custom_image != null ? var.custom_image["publisher"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["publisher"]
│     ├────────────────
│     │ var.custom_image is map of object with 1 element
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on .terraform/modules/mod_virtual_machine/resources.virtual.machine.tf line 183, in resource "azurerm_windows_virtual_machine" "win_vm":
│  183:       offer     = var.custom_image != null ? var.custom_image["offer"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["offer"]
│     ├────────────────
│     │ var.custom_image is map of object with 1 element
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on .terraform/modules/mod_virtual_machine/resources.virtual.machine.tf line 184, in resource "azurerm_windows_virtual_machine" "win_vm":
│  184:       sku       = var.custom_image != null ? var.custom_image["sku"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["sku"]
│     ├────────────────
│     │ var.custom_image is map of object with 1 element
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on .terraform/modules/mod_virtual_machine/resources.virtual.machine.tf line 185, in resource "azurerm_windows_virtual_machine" "win_vm":
│  185:       version   = var.custom_image != null ? var.custom_image["version"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["version"]
│     ├────────────────
│     │ var.custom_image is map of object with 1 element
│ 
│ The given key does not identify an element in this collection value.

Expected Behaviour

I've seen two different versions of this:

From variables.tf

  custom_image = {
    windows_11_pro = {
      publisher = "MicrosoftWindowsDesktop"
      offer     = "windows-11"
      sku       = "win11-22h2-pro"
      version   = "latest"
    }
  }

OR

From README.md

  custom_image = {
      publisher = "MicrosoftWindowsDesktop"
      offer     = "windows-11"
      sku       = "win11-22h2-pro"
      version   = "latest"
    }

Either shape is fine and I would expect a VM that is Windows 11.

Actual Behaviour

Slightly different errors depending on terraform which is used.

Steps to Reproduce

terraform apply with the terraform above.

Important Factoids

No response

References

No response

jrspinella commented 1 year ago

Hi,

custom_image = { windows_11_pro = { publisher = "MicrosoftWindowsDesktop" offer = "windows-11" sku = "win11-22h2-pro" version = "latest" } }

This custom image is not in the module's image index, so it throws an error. Please refer to the README for adding custom images.

Section: Custom Virtual Machine images

custom_image = { publisher = "myPublisher" offer = "myOffer" sku = "mySKU" version = "latest" }