Error handling in cloud init scripts within azurerm_linux_virtual_machine

Hello everyone :raised_hand_with_fingers_splayed:,

I run a custom shell script when I deploy my virtual machine with terraform, which can throw errors.

My question is, how do you handle these errors, because regardless of the return code of the script, terraform always reports the deployment was successful, which leads to confusion when the VM does not what it’s supposed to do.

Here a snippet of the terraform file for context:

data "template_file" "setup_script" {
  count    = var.agent_count
  template = file("scripts/setup.sh")
  vars = {
    POOL_NAME        = var.pool_name
    AGENT            = "agent-${count.index}"
    ORGANIZATION_URL = var.organization_url
    TOKEN            = var.token
    TERRAFORM_VERSION = var.terraform_version
    VSTS_AGENT_VERSION = var.vsts_agent_version
  }
}

resource "azurerm_linux_virtual_machine" "vmachine" {
  count               = length(module.network.network_interfaces)
  name                = "agent-${count.index}"
  resource_group_name = azurerm_resource_group.deployment-agents.name
  location            = azurerm_resource_group.deployment-agents.location
  size                = "Standard_B1ms"
  admin_username      = "adminuser"

  network_interface_ids = [
    module.network.network_interfaces[count.index].id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = var.ssh_public_key
  }

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

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

  boot_diagnostics {
    storage_account_uri = azurerm_storage_account.boot.primary_blob_endpoint
  }
  custom_data = base64encode(data.template_file.setup_script.*.rendered[count.index])
}

And the setup.sh shell script:

# --- snip ----
apt-get install azure-cli
if [ $? -gt 0 ]; then
  echo "Cannot install azure cli!"
  exit 1
fi

# Test
exit 1

I appreciate the help.

Hello.

I have the same issue.
The only way I can install the custom_data is using another SKU of CentOS like 8_5 but I should use another image in version 7.

If you can solved this issue, could you reply to me, please.

Regards.