Search code examples
azureterraform

How to effectively use azure_virtual_machine_extension in Azure Terraform


I'd like to run multiple linux commands using azurerm_virtual_machine_extension. Is there a sophisticated way to use multiple commands side by side instead of listing them in a single line? I've previously used it as below.

resource "azurerm_virtual_machine_extension" "run_command" {
  for_each             = var.vm_template
  name                 = "${each.value.vm_name}-run_command"
  virtual_machine_id   = azurerm_linux_virtual_machine.vm[each.key].id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"
  timeouts {
    create = "1h" # VM 개수가 많으면 그만큼 더 오래걸림
  }
  settings = <<SETTINGS
 {
  "commandToExecute": "bash -c 'hostnamectl set-hostname --static ${each.value.hostname}'&& sleep 60 && sh /tmp/for_image.sh > /tmp/installed.log 2>&1"
 }
SETTINGS
}

Is it possible to write a code by aligning it line by line without executing all the commands line like above? I need your help.


Solution

  • How to effectively use azure_virtual_machine_extension in Azure Terraform

    Here is the Terraform code to create multiple vms and multilple cmd's with VM extensions for executing Bash code inside the VMs.

    variables.tf

    variable "vms" {
      default = [
        {
          name   = "vm1"
          size   = "Standard_DS1_v2"
          admin_username = "Venkat"
          admin_password = "Welcome@123$"
          computer_name   = "Linux_VM1"
          new_computername = "Venkat_LinuxVM1"
        },
        {
          name   = "vm2"
          size   = "Standard_DS1_v2"
          admin_username = "Venkat"
          admin_password = "Welcome@123$"
          computer_name   = "Linux_VM2"
          new_computername = "Venkat_LinuxVM2"
        }
      ]
    }
    

    main.tf

    provider "azurerm" {
      features {}
    }
    
    resource "azurerm_resource_group" "example" {
      name     = "VM-resources"
      location = "West US"
    }
    
    resource "azurerm_virtual_network" "example" {
      name                = "example-network"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "sample"
      resource_group_name  = azurerm_resource_group.example.name
      virtual_network_name = azurerm_virtual_network.example.name
      address_prefixes     = ["10.0.2.0/24"]
    }
    
    resource "azurerm_public_ip" "example" {
      count               = 2
      name                = "demo-pip-${count.index}"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      allocation_method   = "Static"
    }
    
    resource "azurerm_network_interface" "example" {
      count               = 2
      name                = "demo-nic-${count.index}"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    
      ip_configuration {
        name                          = "internal"
        subnet_id                     = azurerm_subnet.example.id
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = element(azurerm_public_ip.example.*.id, count.index)
      }
    }
    
    resource "azurerm_linux_virtual_machine" "example" {
      count              = length(var.vms)
      name               = var.vms[count.index].name
      location           = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      size               = var.vms[count.index].size
      admin_username     = var.vms[count.index].admin_username
      network_interface_ids = [
        azurerm_network_interface.example[count.index].id,
      ]
    
      computer_name      = var.vms[count.index].computer_name
      admin_password     = var.vms[count.index].admin_password
      disable_password_authentication = false
    
      os_disk {
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
      }
    
      source_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "18.04-LTS"
        version   = "latest"
      }
    }
    
      resource "azurerm_virtual_machine_extension" "hostname" {
      count               = length(var.vms)
      name                = "hostname"
      virtual_machine_id  = azurerm_linux_virtual_machine.example[count.index].id
      publisher           = "Microsoft.Azure.Extensions"
      type                = "CustomScript"
      type_handler_version = "2.0"
      
          settings = <<SETTINGS
            fileUris = [
              "https://samplestoragevm.blob.core.windows.net/vmstorage/script.sh",
            ]
            commandToExecute = "sh script.sh"
    }
    
    

    Terraform apply

    enter image description here

    After executing the VM Extension, the hostname was changed successfully.

    VM 1 Hostname:

    enter image description here

    Update:

    To run multiple commands using azurerm_virtual_machine_extension, create a bash file and upload the same file to a storage account. Then, use the blob URL in fileUris in terraform.

       resource "azurerm_virtual_machine_extension" "hostname" {
      count               = length(var.vms)
      name                = "hostname"
      virtual_machine_id  = azurerm_linux_virtual_machine.example[count.index].id
      publisher           = "Microsoft.Azure.Extensions"
      type                = "CustomScript"
      type_handler_version = "2.0"
      
          settings = <<SETTINGS
            fileUris = [
              "https://samplestoragevm.blob.core.windows.net/vmstorage/script.sh",
            ]
            commandToExecute = "sh script.sh"
    }
    
    

    Reference: Stack link1 by Nancy and link2 answered by me for using multiple commands in azurerm_virtual_machine_extension.

    Custom Script Extension