Terraform-provider-azurerm: azurerm_virtual_machine_extension

Created on 5 Dec 2017  ·  7Comments  ·  Source: terraform-providers/terraform-provider-azurerm

Terraform v0.10.8

variable "app_count" {default = "2"}

resource "azurerm_virtual_machine" "test" {

  count = "${var.app_count}"

  name                  = "test-${count.index+1}"
  location              = "${var.app_location}"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${element(azurerm_network_interface.test.*.id, count.index+1)}"]
  vm_size               = "${var.app_instance_type}"

  # TODO: set based on environment
  delete_os_disk_on_termination = true

  storage_image_reference {
    id = "/subscriptions/xxxx/resourceGroups/test/providers/Microsoft.Compute/images/images-2017-11-15-113313"
  }

  storage_os_disk {
    name              = "test-${count.index+1}"
    managed_disk_type = "Standard_LRS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
  }

  os_profile {
    computer_name  = "test-${count.index+1}"
    admin_username = "centos"
  }

  os_profile_linux_config {
    ssh_keys {
      path     = "/home/centos/.ssh/authorized_keys"
      key_data = "${var.centos_key}"
    }
    disable_password_authentication = true
  }
}

resource "azurerm_virtual_machine_extension" "test" {
  name                 = "centos"
  location             = "${var.app_location}"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"
  settings = <<SETTINGS
    {
        "fileUris": ["https://test.blob.core.windows.net/scripts/test.sh"],
        "commandToExecute": "./test.sh"
    }
   SETTINGS
  protected_settings = <<PROTECTED_SETTINGS
    {
        "storageAccountName": "test123",
        "storageAccountKey": "xxxx"
    }
   PROTECTED_SETTINGS
}
Error: Error running plan: 1 error(s) occurred:

* azurerm_virtual_machine_extension.test: 1 error(s) occurred:

* azurerm_virtual_machine_extension.test: Resource 'azurerm_virtual_machine.test' not found for variable 'azurerm_virtual_machine.test.name'

This occurs when count of instances are 2 or more. I tried to apply the same thing for single VM and it works. Any workaround? Appreciate for help!

good first issue question

Most helpful comment

I wanted to update that solution provided worked. Thanks once again!

All 7 comments

Same here. Any ways of making this work?

@shahbhavya85 please try it

resource "azurerm_virtual_machine_extension" "vmextension_puppetmaster_cleanup01" {
name = "${var.rethesh_prefix}${var.rethesh_server_env_suffix["${var.rethesh_environment}"]}${var.rethesh_puppetmaster_suffix}${format(count.index + 1)}-puppetcleanup-extension"
location = "${azurerm_resource_group.resourcegroup01.location}"
resource_group_name = "${azurerm_resource_group.resourcegroup01.name}"
virtual_machine_name = "${element(azurerm_virtual_machine.puppetmaster_vms.*.name, count.index )}"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
settings = < {
"fileUris" : ["${var.rethesh_automation_script_url}/${var.rethesh_puppetmaster_script}"]
}
SETTINGS
protected_settings = < {
"commandToExecute" : "bash ${var.rethesh_puppetmaster_script}",
"storageAccountName" : "${var.rethesh_storageaccount_automation_script}",
"storageAccountKey" : "${var.rethesh_storageaccount_automation_primary_key}"
}
PROTECTED_SETTINGS
depends_on = ["azurerm_virtual_machine_extension.vmextension_dc01"]
}

virtual_machine_name = "${element(azurerm_virtual_machine.puppetmaster_vms.*.name, count.index )}" is the most important part

Hi @shahbhavya85

Thanks for opening this issue.

For questions regarding Terraform Configurations we'd generally recommend asking this via one of the sources listed on the Community Page rather than here (which is meant for issues/bugs with the Terraform AzureRM Provider).

Taking a look at the configuration you've posted above - it appears there's multiple VM's being created with a single VM Extension - whereas in Azure each VM needs to have it's own VM Extension. This can be solved by creating one VM Extension per VM using the count parameter on the azurerm_virtual_machine_extension resource, and referencing the relevant VM Name as mentioned in @retheshnair's second comment above. More information on the count parameter is available in the documentation.

As mentioned above - given this is a Configuration issue, rather than a bug with the AzureRM Provider - I'm going to close this issue for the moment.

Thanks!

Thanks you so much @retheshnair and @tombuildsstuff. I will try out the given solution.

I wanted to update that solution provided worked. Thanks once again!

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

Was this page helpful?
0 / 5 - 0 ratings