If I understand your preoblem correctly I think the key is to create declare VM with the following sections with placeholder replaced;
plan {
publisher = "${publisher}" // e.g. bitnami
product = "${offer}" // e.g. elk
name = "${sku}" // e.g. 46
}
storage_image_reference {
publisher = "${publisher}" // e.g. bitnami
offer = "${offer}" // e.g. elk
sku = "${sku}" // e.g. 46
version = "${version}" // e.g. latest
}
So a complete VM resource definition would lok something like this.
resource "azurerm_virtual_machine" "virtual_machine" {
count = "${var.vm_count}"
name = "${element(module.template.vm_names, count.index)}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
network_interface_ids = ["${element(azurerm_network_interface.network_interface.*.id, count.index)}"]
vm_size = "${var.vm_size}"
delete_data_disks_on_termination = true
delete_os_disk_on_termination = true
plan {
publisher = "${var.publisher}"
product = "${var.offer}"
name = "${var.sku}"
}
boot_diagnostics {
enabled = true
storage_uri = "${var.boot_diagnostics_storage_url}"
}
storage_image_reference {
publisher = "${var.publisher}"
offer = "${var.offer}"
sku = "${var.sku}"
version = "${var.version}"
}
storage_os_disk {
name = "primarydisk"
vhd_uri = "${join("", list(var.disks_container_url, "/" , element(module.template.vm_names, count.index), ".vhd"))}"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "${element(module.template.vm_names, count.index)}"
admin_username = "${element(module.template.user_names, count.index)}"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys = [{
path = "/home/${element(module.template.user_names, count.index)}/.ssh/authorized_keys"
key_data = "${replace(file("../vars/keys/vm.pub"),"\n","")}"
}]
}
tags {
environment = "${var.resource_group_name}"
}
}