Azure / terraform-azurerm-avm-res-compute-virtualmachinescaleset

MIT License
4 stars 10 forks source link

[AVM Module Issue]: Bug in dynamic "admin_ssh_key" block of "linux_configuration" block #97

Closed chianw closed 4 weeks ago

chianw commented 4 weeks ago

Check for previous/existing GitHub issues

Issue Type?

Bug

(Optional) Module Version

No response

(Optional) Correlation Id

No response

Description

The code to produce this is similar to the examples in the module. Partial snippet below.


resource "tls_private_key" "this" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

module "terraform_azurerm_avm_res_compute_virtualmachinescaleset" {
  source  = "Azure/avm-res-compute-virtualmachinescaleset/azurerm"
  version = "0.3.0"
  # source = "github.com/chianw/terraform-azurerm-avm-res-compute-virtualmachinescaleset.git"
  # source             = "Azure/avm-res-compute-virtualmachinescaleset/azurerm"
  name                        = module.naming.virtual_machine_scale_set.name_unique
  resource_group_name         = azurerm_resource_group.this.name
  enable_telemetry            = var.enable_telemetry
  location                    = azurerm_resource_group.this.location
  admin_password              = "P@ssw0rd1234!"
  instances                   = 2
  sku_name                    = "Standard_B2s"
  extension_protected_setting = {}
  user_data_base64            = null
  boot_diagnostics = {
    storage_account_uri = "" # Enable boot diagnostics
  }
  admin_ssh_keys = [(
    {
      id         = tls_private_key.this.id
      public_key = tls_private_key.this.public_key_openssh
      username   = "azureuser"
    }
  )]
  network_interface = [{
    name                      = "VMSS-NIC"
    network_security_group_id = azurerm_network_security_group.nic.id
    ip_configuration = [{
      name      = "VMSS-IPConfig"
      subnet_id = azurerm_subnet.subnet.id
      public_ip_address = [{
        name = "VMSS-PIP"
      }]
    }]
  }]
  os_profile = {
    linux_configuration = {
      disable_password_authentication = true
      admin_username                  = "azureuser"
      admin_ssh_key_id                = toset([tls_private_key.this.id])
    }
  }
  source_image_reference = {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-jammy"
    sku       = "22_04-LTS-gen2" # Auto guest patching is enabled on this sku.  https://learn.microsoft.com/en-us/azure/virtual-machines/automatic-vm-guest-patching
    version   = "latest"
  }
  extension = [{
    name                        = "HealthExtension"
    publisher                   = "Microsoft.ManagedServices"
    type                        = "ApplicationHealthLinux"
    type_handler_version        = "1.0"
    auto_upgrade_minor_version  = true
    failure_suppression_enabled = false
    settings                    = "{\"port\":80,\"protocol\":\"http\",\"requestPath\":\"/index.html\"}"
  }]
  tags = local.tags
  # Uncomment the code below to implement a VMSS Lock
  #lock = {
  #  name = "VMSSNoDelete"
  #  kind = "CanNotDelete"
  #}
  # depends_on = [azurerm_subnet_nat_gateway_association.this]
}

The following error is observed in terraform plan.

╷
│ Error: Reference to "each" in context without for_each
│ 
│   on .terraform/modules/terraform_azurerm_avm_res_compute_virtualmachinescaleset/main.tf line 183, in resource "azurerm_orchestrated_virtual_machine_scale_set" "virtual_machine_scale_set":
│  183:               public_key = var.admin_ssh_keys[each.key].public_key
│ 
│ The "each" object can be used only in "module" or "resource" blocks, and only when the "for_each" argument is set.
╵
╷
│ Error: Reference to "each" in context without for_each
│ 
│   on .terraform/modules/terraform_azurerm_avm_res_compute_virtualmachinescaleset/main.tf line 184, in resource "azurerm_orchestrated_virtual_machine_scale_set" "virtual_machine_scale_set":
│  184:               username   = var.admin_ssh_keys[each.key].username
│ 
│ The "each" object can be used only in "module" or "resource" blocks, and only when the "for_each" argument is set.

Please see if https://github.com/Azure/terraform-azurerm-avm-res-compute-virtualmachinescaleset/compare/main...chianw:terraform-azurerm-avm-res-compute-virtualmachinescaleset:main addresses this or if this is a bug at all?

chianw commented 4 weeks ago

Closed because it is duplicate of https://github.com/Azure/terraform-azurerm-avm-res-compute-virtualmachinescaleset/pull/92