LW.

Glorified Notepad

Deploy VM from Azure Marketplace image using Terraform

2021-01-05 Azure Terraform

During code deployment via terraform to Azure, it’s useful to be able to reference marketplace-based images to support the deployment of 3rd party services.

This brief guide will cover how to find an image and then how to use that data to deploy, in this case, an AlertLogic Linux VM. Windows images can be found in the same manner.

For the purpose of this example, I will deploy Alert Logic Professional - BYOL. This can be found within the Azure marketplace.

AlertLogic Professional Marketplace Image

In order to deploy this image, we must first obtain the following IDs: Offer, Publisher, SKU, and Version. This can be done once connected to the Azure cloud shell using the following command:

az vm image list --output table --all --publisher AlertLogic

AlertLogic Professional Marketplace Cloud Shell Export

Once we have this detail we can now use these elements to build out the storage_image_reference and plan blocks:

storage_image_reference {
    publisher = "alertlogic"
    offer     = "alert-logic-tm"
    sku       = "20215000100-tmpbyol"
    version   = "latest"
  }
  plan {
    name      = "20215000100-tmpbyol"
    publisher = "alertlogic"
    product   = "alert-logic-tm"
  }

These blocks when used within azurerm_virtual_machine will fully deploy the template with more customization than the portal and even at scale. Below is a full example of this resource as a whole (note that this relies on existing variables and resources):

####
#resource group
####
resource "azurerm_resource_group" "hub_alertlogic" {
  name     = "${var.resource_prefix}-ALERTLOG-rg"
  location = var.location
  tags = var.tags
}

####
#virtual network adapter
####
resource "azurerm_network_interface" "hub_alertlogic_vm_nic" {
  count               = 1
  name                = "${var.spoke1_VM_resource_prefix}-ALERTLOG${count.index + 1}-nic"
  location            = azurerm_resource_group.hub_alertlogic.location
  resource_group_name = azurerm_resource_group.hub_alertlogic.name
  tags = var.tags
  ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = azurerm_subnet.hub_services_sn.id
    private_ip_address_allocation = "Dynamic"
  }
}

####
#virtual machine
####
resource "azurerm_virtual_machine" "hub_alertlogic_vm" {
  count                 = 1
  name                  = "${var.spoke1_VM_resource_prefix}-ALERTLOG${count.index + 1}"
  resource_group_name   = azurerm_resource_group.hub_alertlogic.name
  location              = azurerm_resource_group.hub_alertlogic.location
  vm_size               = "Standard_F4s_v2"
  network_interface_ids = [azurerm_network_interface.hub_alertlogic_vm_nic[count.index].id]

  storage_image_reference {
    publisher = "alertlogic"
    offer     = "alert-logic-tm"
    sku       = "20215000100-tmpbyol"
    version   = "latest"
  }
  plan {
    name      = "20215000100-tmpbyol"
    publisher = "alertlogic"
    product   = "alert-logic-tm"
  }
  storage_os_disk {
    name              = "${var.spoke1_VM_resource_prefix}-ALERTLOG${count.index + 1}-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }
  os_profile {
    computer_name  = "${var.spoke1_VM_resource_prefix}-ALERTLOG${count.index + 1}"
    admin_username = var.username
    admin_password = var.password
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = var.tags
}

Finally before deployment of a marketplace image with an associated plan can be completed the following powershell command needs to ran in order to accept the terms:

$Publisher = "alertlogic"
$Product = "alert-logic-tm"
$Name = "20215000100-tmpbyol"
Get-AzureRmMarketplaceTerms -Publisher $Publisher -Product $Product -Name $Name  | Set-AzureRmMarketplaceTerms -Accept