15

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.

Xophmeister
  • 8,884
  • 4
  • 44
  • 87
  • 1
    One way is to begin manual deployment of the VM (searching the marketplace for the image you'd like), then export the ARM template to see what the parameters for the image are. – cody.codes Feb 24 '22 at 15:20

3 Answers3

28

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.

Johnny9
  • 436
  • 5
  • 8
  • 1
    That worked -- thank you :) You also need to provide the `version`, the choices for which can be found with `az vm image list`. – Xophmeister Feb 24 '22 at 16:21
  • I apologize for not including it, but as I said I used this inside packer and there I did not need a version. I should have mentioned that in my original post, but you seemed to figured out so that is nice to hear. – Johnny9 Feb 24 '22 at 17:13
  • No worries -- thanks for the help, it's most appreciated. I don't know why Microsoft make it so obscure! – Xophmeister Feb 24 '22 at 17:14
  • @Johnny9 Your code works properly in terraform block as well. No need to add packer if not necessary – Hussain K Mar 15 '22 at 05:12
  • Thanks for highlighting the `CLI usage` coming form `AWS` world, things are quite different in `az` – Teodorico Maziviala Oct 02 '22 at 11:45
  • Can you tell us why do you use 0001-com-ubuntu-server-focal and no for example 0001-com-ubuntu-pro-jammy. Is it connected any way with offer UbuntuServer which ends at ubuntu 19? – zolty13 Nov 18 '22 at 11:20
  • The question stated that @Xophmeister needs ubuntu 20.04. Jammy is code name for Ubuntu 22.04 which I assume is code which you mentioned in the comment ( have not checked what is the code for ubuntu 22.04 ). Ubuntu 22.04 had not yet came out when I commented this in February. – Johnny9 Nov 18 '22 at 11:25
14

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

whymatter
  • 755
  • 8
  • 18
1

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"

Shivanand
  • 11
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '23 at 03:29