Search code examples
terraformterraform-provider-azureazure-vm-scale-set

Run script extension on Linux VM using Terraform


I'm running trying to run a bash script on an Azure Linux VM scaleset using custom script extensions, I have the script uploaded into an Azure Storage account already. The bash script is meant to install ngix, on the VM Scaleset. The script runs without any errors, however if I log into any of the VMScaleset instances to validate I don't see NGIX running. Bash script here

#!/bin/bash
apt-get update 
apt-get install -y nginx

Terraform file here

data "azurerm_subnet" "refdata" {
  name                 = var.subnetName1
  virtual_network_name = var.vnetName
  resource_group_name  = var.resourceGroupName
}
resource "azurerm_windows_virtual_machine_scale_set" "res-vmscaleset" {
  name                = var.vmScaleSetName
  resource_group_name = azurerm_resource_group.DevRG.name
  location            = azurerm_resource_group.DevRG.location
  sku                 = "Standard_F2"
  instances           = 1
  admin_password      = "xxxxxx"
  admin_username      = "adminuser"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-Server-Core"
    version   = "latest"
  }

  os_disk {
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }

  network_interface {
    name    = "vmscaleset-nic"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
       subnet_id=data.azurerm_subnet.test.id
    }
  }
}

resource "azurerm_linux_virtual_machine_scale_set" "res-linuxscale" {
  name                = "linuxvmss"
  resource_group_name = azurerm_resource_group.DevRG.name
  location            = azurerm_resource_group.DevRG.location
  sku                 = "Standard_F2"
  instances           = 2
  admin_password = "Password1234!"
  disable_password_authentication = false
  admin_username      = "adminuser"

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

  os_disk {
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }

  network_interface {
    name    = "lvmscaleset-nic"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id=data.azurerm_subnet.test.id
      
    }
  }
}

resource "azurerm_virtual_machine_scale_set_extension" "res-extension" {
  name                         = "example"
  virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.res-linuxscale.id
  publisher                    = "Microsoft.OSTCExtensions"
  type                         = "CustomScriptForLinux"
  type_handler_version         = "1.0"
  settings = <<SETTINGS
{
    
    "fileUris": ["https://xxxxxxxxxxx.blob.core.windows.net/shellscript11/post-deploy.sh"],
    "commandToExecute": "sh post-deploy.sh"
    }
 SETTINGS

}

Solution

  • Reference to this document, you can use the publisher and type for your custom script like this.

    resource "azurerm_virtual_machine_scale_set_extension" "res-extension" {
      name                         = "nnn-extension"
      virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.example.id
      publisher                    = "Microsoft.Azure.Extensions"
      type                         = "CustomScript"
      type_handler_version         = "2.0"
      settings = jsonencode({ 
        "fileUris" = ["https://xxxx.blob.core.windows.net/shscripts/aptupdate.sh"],
        "commandToExecute" = "sh aptupdate.sh"
        }
      )
    }
    

    After applying the above configurations, you could upgrade each vmss instance, then the Nginx will be running.

    enter image description here

    Result

    enter image description here

    enter image description here