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!
2 Answers
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.
3 Comments
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
}
}