hashicorp/terraform

azurerm_virtual_machine won't plan with ssh_keys variable

Closed this issue · 8 comments

I can create instances using ssh keys property of azurerm_virtual_machine. When trying to extract ssh_keys as a list parameter it errors when executing terraform plan
* azurerm_virtual_machine.test: os_profile_linux_config.0.ssh_keys.0: expected object, got string

The variable is a list containing a map.
An example failing configuration is given below.

Terraform Version

Terraform v0.7.0

Affected Resource(s)

azurerm_virtual_machine

Terraform Configuration Files

Example for azurerm_virtual_machine based on https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html with list variable for ssh_keys

resource "azurerm_resource_group" "test" {
  name     = "acctestrg"
  location = "West US"
}

resource "azurerm_virtual_network" "test" {
  name                = "acctvn"
  address_space       = ["10.0.0.0/16"]
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
  name                 = "acctsub"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_network_interface" "test" {
  name                = "acctni"
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.test.id}"
    private_ip_address_allocation = "dynamic"
  }
}

resource "azurerm_storage_account" "test" {
  name                = "accsa"
  resource_group_name = "${azurerm_resource_group.test.name}"
  location            = "westus"
  account_type        = "Standard_LRS"

  tags {
    environment = "staging"
  }
}

resource "azurerm_storage_container" "test" {
  name                  = "vhds"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  storage_account_name  = "${azurerm_storage_account.test.name}"
  container_access_type = "private"
}

resource "azurerm_virtual_machine" "test" {
  name                  = "acctvm"
  location              = "West US"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${azurerm_network_interface.test.id}"]
  vm_size               = "Standard_A0"

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "14.04.2-LTS"
    version   = "latest"
  }

  storage_os_disk {
    name          = "myosdisk1"
    vhd_uri       = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys                        = ["${var.ssh_keys}"]
  }

  tags {
    environment = "staging"
  }
}

variable "ssh_keys" {
  type = "list"
  default = [{
    path     = "/home/testadmin/.ssh/authorized_keys"
    key_data = "ssh-rsa SomeKeyDataStringHere"
  }]
}

Debug Output

Expected Behavior

Create the instance with the supplied ssh keys

Actual Behavior

* azurerm_virtual_machine.test: os_profile_linux_config.0.ssh_keys.0: expected object, got string

Steps to Reproduce

terraform plan the example above

Important Factoids

References

I think this is a duplicate bug of GH-7705

Any updates here? Thanks

@dobrerazvan This is the work-around I've been using for this, just in-lining the key data

  os_profile_linux_config {
    disable_password_authentication = false
    ssh_keys = [{
      path     = "/home/${var.ssh_user_username}/.ssh/authorized_keys"
      key_data = "${file("~/.ssh/some_key.pub")}"
    }]
  }

@karysto

Thanks, that is working.

Hi @carinadigital

Thanks for opening this issue :)

Using the configuration you've posted above (after pasting my public key into the variable key_data in ssh_keys) - I'm able to Plan and Apply as expected. As such I believe this issue has been resolved - would it be possible to you to confirm this for me? :)

Thanks! :)

@tombuildsstuff I can confirm it's working as expected now.

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.