Xophmeister
Xophmeister

Reputation: 9211

Creating an Azure Linux VM with Ubuntu 20.04 with Terraform

I am trying to create a Linux VM, with Terraform, in the West Europe Azure region, with a Ubuntu Server 20.04 LTS image. I can do this just fine from within the Azure Portal, but Terraform complains that the image doesn't exist:

The platform image 'Canonical:UbuntuServer:20.04-LTS:latest' is not available.

Indeed, az vm image list --location westeurope confirms this; 18.04 LTS exists, but no 20.04 LTS.

I am using the azurerm_linux_virtual_machine resource, with the following source_image_reference:

source_image_reference {
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "20.04-LTS"  # FIXME SKU doesn't exist in westeurope
  version   = "latest"
}

I'm utterly confused by this! How does one access the images in the Azure Marketplace in Terraform? I've seen suggestions that the plan block is needed, but have no idea (nor have I found any documentation) on how to configure this.

Upvotes: 20

Views: 20324

Answers (5)

saeed ahmad
saeed ahmad

Reputation: 1

Create a virtual machine resource "azurerm_virtual_machine" "JabVM" { name = "JabriVM" location = azurerm_resource_group.jb-gr.location resource_group_name = azurerm_resource_group.jb-gr.name network_interface_ids = [azurerm_network_interface.jb-interface.id] vm_size = "Standard_F2"

Upvotes: -1

Sri
Sri

Reputation: 61

Just in case if anyone else stumble upon this in 2024, here is the lates one that works for me:

source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts"
    version   = "latest"
}

Ref: https://github.com/hashicorp/terraform-provider-azurerm/issues/22078

Upvotes: 1

Shivanand
Shivanand

Reputation: 19

Code="BadRequest" Message="The selected VM size 'Standard_D2_v2' cannot boot Hypervisor Generation '2'.

When you use ubuntu "20_04-lts-gen2" please select the proper VM size that suits gen2, I'm answering to avoid some confusion. In my case, I used the VM "Standard B2s"

Upvotes: 1

Teodorico Maziviala
Teodorico Maziviala

Reputation: 426

For anyone else having this issue and having tried the above but still finding it unhelpful. Here is an addition to the above answer:

Login to azure cli and run the following command to list all your existing VMs based on your needs.

az vm image list --all --publisher="Canonical" --sku="20_04-lts-gen2"

You should see an output like this:

{
    "architecture": "x64",
    "offer": "0001-com-ubuntu-server-focal",
    "publisher": "Canonical",
    "sku": "20_04-lts-gen2",
    "urn": "Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:20.04.202209050",
    "version": "20.04.202209050"
},
{
    "architecture": "x64",
    "offer": "0001-com-ubuntu-server-focal",
    "publisher": "Canonical",
    "sku": "20_04-lts-gen2",
    "urn": "Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:20.04.202209200",
    "version": "20.04.202209200"
}

In my case, I was having issues with my version. In this case, had to change my code from this ...

source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts-gen2"
    version   = "latest"
 }

... to this:

source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts-gen2"
    version   = "20.04.202209200"
 }

As you can see, I used the version based on the output from the az command.

enjoy terraform

Upvotes: 18

Johnny9
Johnny9

Reputation: 496

I too was confused at first when I found out that it is available but under a different name, it is indeed kind of hidden.

offer                 = "0001-com-ubuntu-server-focal"
publisher             = "Canonical"
sku                   = "20_04-lts-gen2"

I used this inside packer so I am guessing it is the same in terraform, but you can let me know.

Upvotes: 34

Related Questions