This is an error I encountered while trying to set the admin ssh key for a linux instance – and attempting to use an existing pub key in my .ssh folder (puttygen can be used to create ssh key pairs)

resource "azurerm_linux_virtual_machine" "myvm" {

  name                = local.vm_name

  resource_group_name = azurerm_resource_group.rg_des.name

  location            = azurerm_resource_group.rg_des.location

  size                = var.vm_size

  admin_username      = "adminuser"

  network_interface_ids = [

    azurerm_network_interface.myvm.id,

  ]

  admin_ssh_key {

    username   = "adminuser"

    #public_key = file("~/.ssh/id_rsa.pub")

     public_key = tls_private_key.linux_test_ssh.public_key_openssh

  }

Instead of doing it through an existing key pair, I used terraform to create the ssh key pair. This worked – no error

# Create (and display) an SSH key

resource "tls_private_key" "linux_test_ssh" {

  algorithm = "RSA"

  rsa_bits  = 4096

}
Summary
If you encounter this error on trying to create linux VMs on azure – Error decoding “admin_ssh_key.0.public_key” for public key data, try creating the key pair directly as a terraform resource.