How to use in Terraform an image from Azure Marketplace

2.9k Views Asked by At

I'm trying to create a VM in Azure using Terraform. I can create the VM directly in Azure Marketplace. However, I'm interested in using Terraform for that.

It's a Linux VM machine, and I'm aware of the block source_image_reference belonging to the resource azurerm_linux_virtual_machine.

This block looks like this:

source_image_reference {
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "16.04-LTS"
  version   = "latest"
}

How am I supposed to fulfill such block using an image from Azure Marketplace?

Cheers

EDIT 1

The source_image_reference shown above is just an example. Actually, the very example available on Terraform's site. The image I want to use is SQL Server 2019 on Ubuntu Server 20.04 LTS

However, I'd say that the question is more general. I mean, once I find an image in Azure Marketplace, how could I use it in Terraform plans?

1

There are 1 best solutions below

1
On

I tried in my environment and successfully created Linux virtual machine with image SQL Server 2019 on Ubuntu Server 20.04 LTS

Terraform code:

provider "azurerm" {
  features {}

}
resource "azurerm_resource_group" "rg" {
  name     = "example-resources"
  location = "eastus"
}

resource "azurerm_virtual_network" "my_terraform_network" {
  name                = "myVnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet
resource "azurerm_subnet" "my_terraform_subnet" {
  name                 = "mySubnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "my_terraform_public_ip" {
  name                = "myPublicIP"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "my_terraform_nsg" {
  name                = "myNetworkSecurityGroup"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create network interface
resource "azurerm_network_interface" "my_terraform_nic" {
  name                = "myNIC"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "my_nic_configuration"
    subnet_id                     = azurerm_subnet.my_terraform_subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.my_terraform_public_ip.id
  }
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
  network_interface_id      = azurerm_network_interface.my_terraform_nic.id
  network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id
}

# Generate random text for a unique storage account name
resource "random_id" "random_id" {
  keepers = {
    # Generate a new ID only when a new resource group is defined
    resource_group = azurerm_resource_group.rg.name
  }

  byte_length = 8
}

# Create storage account for boot diagnostics
resource "azurerm_storage_account" "my_storage_account" {
  name                     = "diag${random_id.random_id.hex}"
  location                 = azurerm_resource_group.rg.location
  resource_group_name      = azurerm_resource_group.rg.name
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

# Create (and display) an SSH key
resource "tls_private_key" "example_ssh" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

# Create virtual machine
resource "azurerm_linux_virtual_machine" "my_terraform_vm" {
  name                  = "myVM"
  location              = azurerm_resource_group.rg.location
  resource_group_name   = azurerm_resource_group.rg.name
  network_interface_ids = [azurerm_network_interface.my_terraform_nic.id]
  size                  = "Standard_DS1_v2"

  os_disk {
    name                 = "myOsDisk"
    caching              = "ReadWrite"
    storage_account_type = "Premium_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftSQLServer"
    offer     = "sql2019-ubuntu2004"
    sku       = "web"
    version   = "15.0.221108"
  }

  computer_name                   = "myvm"
  admin_username                  = "azureuser"
  disable_password_authentication = true

  admin_ssh_key {
    username   = "azureuser"
    public_key = tls_private_key.example_ssh.public_key_openssh
  }

  boot_diagnostics {
    storage_account_uri = azurerm_storage_account.my_storage_account.primary_blob_endpoint
  }
}

The source image reference for SQL Server 2019 on Ubuntu Server 20.04 LTS

source_image_reference {
    publisher = "MicrosoftSQLServer"
    offer     = "sql2019-ubuntu2004"
    sku       = "web"
    version   = "15.0.221108"
  }

Console: enter image description here

Portal:

enter image description here

You can get the version and sku by executing below azure cli commands:

az vm image list --all --publisher="MicrosoftSQLServer" --offer="sql2019-ubuntu2004"

Reference: Creating an Azure Linux VM with Ubuntu 20.04 with Terraform - Stack Overflow by Teodorico Maziviala