-2

After terraform apply it creates an azurerm_virtual_machine_scale_set resource with N instances, but then I need to execute some script with different params for each VM instance. Could you please help me with this issue? Thanks a lot!

1
  • You should post what you've tried so far (with example Terraform code) and why that doesn't work for you and what you need precisely. Commented Jan 14, 2019 at 17:43

2 Answers 2

0

For your issue, I think you should be better to take a knowledge of the Azure VM Scale Set. Generally, scale sets are useful for deploying the highly available infrastructure where a set of machines has the similar configuration.

There are some features are only available in scale sets while other features are only available in VMs. See When to use scale sets instead of virtual machines? So you should think about which service that you really want.

Also, take a look at the Argument of VM scale set that you can set in Terraform. You just can set the extension to execute the script in the scale set, but it seems the extension would apply to the whole scale set not the individual instance with different parameters. Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply! In fact, I use the scale sets, because I did not find any instructions for creating several VMs with terraform and azure. Please write me if you have any info on how I can create multiple VMs. This will help me a lot!
You can take a look at this case. It will help you create multi VMs through Terraform. You can also use the Azure Template to create multi VMs, see create multi-instances.
@JohnDoe Any more question? Or if it's helpful you can accept it as the answer.
0

you can just use a loop to create multiple copies of anything in terraform:

resource "azurerm_virtual_machine" "vm" {
  name                             = "${var.reference["name"]}-${var.vmName}-vm-${count.index}"
  location                         = "${var.reference["location"]}"
  resource_group_name              = "${var.reference["name"]}"
  network_interface_ids            = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
  vm_size                          = "Standard_B1ms"
  availability_set_id              = "${azurerm_availability_set.av.id}"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true
  count                            = "${var.reference["${var.vmName}Count"]}"

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

  storage_os_disk {
    name              = "${var.reference["name"]}-${var.vmName}-vm-${count.index}-osDisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "${var.reference["name"]}-${var.vmName}-vm-${count.index}"
    admin_username = "${var.reference["name"]}"
    admin_password = "!Q2w3e4r5t6y"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.