Decoding "admin_ssh_key.0.public_key" for public key data

Hi Everyone,

I am learning terraform and i want to create linuxvm on azure but i got one error.

Error: decoding "admin_ssh_key.0.public_key" for public key data
│
│   with azurerm_linux_virtual_machine.linux-vm,
│   on main.tf line 126, in resource "azurerm_linux_virtual_machine" "linux-vm":
│  126: resource "azurerm_linux_virtual_machine" "linux-vm" {
│

Below is the configuration


//*********************************************************************
#declaration of local variables
locals {
  resource_group_name="app-grp"
  location="Central India"
  virtual_network={
    name="appnetwork"
    address_space="10.0.0.0/16" # ip address space
  }
  subnets=[
  {
    name="subnetA"
    address_prefix="10.0.0.0/24"
  },
  {
    name="subnetB"
    address_prefix="10.0.1.0/24"
  }
  ]
}

#create resource group
resource "azurerm_resource_group" "appgrp" {
  name     = local.resource_group_name
  location = local.location
}

# create virtual network
resource "azurerm_virtual_network" "appnetwork" {
  name                = local.virtual_network.name
  location            = local.location
  resource_group_name = local.resource_group_name
  address_space       = [local.virtual_network.address_space]
 
 
depends_on = [ azurerm_resource_group.appgrp ]
 }
# subnet creation
 resource "azurerm_subnet" "subnetA" {
  name                 = local.subnets[0].name
  resource_group_name  = local.resource_group_name
  virtual_network_name = local.virtual_network.name
  address_prefixes     = [local.subnets[0].address_prefix]
  depends_on = [azurerm_virtual_network.appnetwork]
 
}
#subnet creation
 resource "azurerm_subnet" "subnetB" {
  name                 = local.subnets[1].name
  resource_group_name  = local.resource_group_name
  virtual_network_name = local.virtual_network.name
  address_prefixes     = [local.subnets[1].address_prefix]
  depends_on = [azurerm_virtual_network.appnetwork]
 
}
# network interface creation
resource "azurerm_network_interface" "appinterface" {
  name                = "appinterface"
  location            = local.location
  resource_group_name = local.resource_group_name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnetA.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id = azurerm_public_ip.app_ip.id
  }
  depends_on = [ azurerm_subnet.subnetA ]
  
}

/*
output "subnets_info" {
  value = tolist(azurerm_virtual_network.appnetwork.subnet)[0].id
}
*/
# assignment of public ip to network interface
resource "azurerm_public_ip" "app_ip" {
  name                = "app-ip"
  resource_group_name = local.resource_group_name
  location            = local.location
  allocation_method   = "Static"
  //sku = "Basic"
depends_on = [ azurerm_resource_group.appgrp ]
}


# network security group 
resource "azurerm_network_security_group" "appnsg" {
  name                = "app-nsg"
  location            = local.location
  resource_group_name = local.resource_group_name

  security_rule {
    name                       = "AllowSSH"
    priority                   = 300
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
depends_on = [ azurerm_resource_group.appgrp ]
}

# association of network security group with a subnets
resource "azurerm_subnet_network_security_group_association" "nsglink" {
  subnet_id                 = azurerm_subnet.subnetA.id
  network_security_group_id = azurerm_network_security_group.appnsg.id
}
#private key
resource "tls_private_key" "linux-key" {
  algorithm = "RSA"
  rsa_bits  = 4096
}
# creates file
resource "local_file" "linuxpemkey" {
  content  = tls_private_key.linux-key.private_key_pem
  filename = "linuxkey.pem"
  depends_on = [ tls_private_key.linux-key ]
}
# creates window virtual machine
resource "azurerm_linux_virtual_machine" "linux-vm" {
  name                = "linux-vm"
  resource_group_name = local.resource_group_name
  location            = local.location
  size                =  "Standard_DS1_v2" #"Standard_D2s_v3" 
  admin_username      = "linuxuser"
  network_interface_ids = [
    azurerm_network_interface.appinterface.id
  ]
 admin_ssh_key {
    username   = "linuxuser"
    public_key = tls_private_key.linux-key.private_key_openssh
  }
  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "RedHat"
    offer     = "RHEL"
    sku       = "87-gen2"
    version   = "latest"
  }
  depends_on = [ azurerm_network_interface.appinterface ,
                 azurerm_resource_group.appgrp,
                 tls_private_key.linux-key

  ]
}

Does anyone has face this problem

Hi @aniketpant1,

Based on the format of the attribute path in the error message, this message seems to be generated by the Azure provider rather than Terraform itself, and the provider seems to be reporting that you’ve not specified the public key using the syntax the provider expected.

I’m not familiar enough with the provider to suggest more than that, but I’m going to move this topic into the Azure provider’s category because participants there tend to be more familiar with Azure and the Azure provider.