azureazure-virtual-machineazure-vm-scale-set

Azure CustomScript not being executed on start


I am running a VMScaleSet with Ubuntu instances.

Also I have a customScript extension set up in terraform like this:

resource "azurerm_virtual_machine_scale_set_extension" "customScriptExtension" {
  name                         = "my-cs-ext"
  virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.vmss.id
  publisher                    = "Microsoft.Azure.Extensions"
  type                         = "CustomScript"
  type_handler_version         = "2.0"
  settings = jsonencode({
    "commandToExecute" = "sh init.sh"
    "fileUris" = ["storageurlToDownloadfrom/foo.txt?sas_token"]
  })
}

This kinda works, as everythin gets provisioned, and the VMSS is running fine. But the customScript is not being executed automatically after starting the VMSS.

I always have to manually press the "upgrade" button for the selected VMSS-instances, then it works (means the commandToExecute is being executed).

I need to have this script run automatically when a new instance starts, as this is especially relevant for scaling out the vmss.

I expected the customScript to be used for exactly this and in Videos I watched, the customScript was executed automatically. At least I have seen this working for VMs, not yet for VMSS, but there shouldn't be a difference, right?

Hope anyone can help me :)))


Solution

  • After you make any changes in VMSS by default you need to upgrade the VMSS nodes with the most recent configuration MANUALLY for Flexible orchestration. Refer below:-

    When I selected the Orchestration mode as Flexible by default manual upgrade was selected like below:-

    enter image description here

    enter image description here

    If you want your VMSS to update automatically, You can create a VMSS instance in an orchestrated mode selected as Uniform by referring the code in this official terraform document below:- As the document itself states :- NOTE: As of the v2.86.0 (November 19, 2021) release of the provider this resource will only create Virtual Machine Scale Sets with the Uniform Orchestration Mode and enable automatic update with the block below:-

    {
    
    name  =  "example-vmss"
    
    resource_group_name  =  azurerm_resource_group.example.name
    
    location  =  azurerm_resource_group.example.location
    
    sku  =  "Standard_F2"
    
    instances  =  1
    
    admin_username  =  "adminuser"
    
    upgrade_mode  =  "Automatic"
    
      
    
    automatic_os_upgrade_policy {
    
    enable_automatic_os_upgrade  =  true
    
    
    disable_automatic_rollback  =  true
    
    }
    
    health_probe_id  =  azurerm_lb_probe.example.id 
    

    On Portal:-

    I created one Uniform orchestrated VMSS and enabled automatic update like below:-

    enter image description here

    enter image description here

    Added the custom script extension to install Azure CLI like below:-

    enter image description here

    Now, When I logged into my VMSS instance the az module was automatically reflected without a need to do it manually upgrade refer below:-

    enter image description here

    Reference:-

    https://medium.com/microsoftazure/vm-scale-set-with-a-local-azure-custom-script-pushed-via-terraform-c4196329657a By James Dumont le Douarec