0
votes

I am using terraform provisioner file & remote-exec to install google chrome after the VM is created. But it is giving error while running terraform apply. I have also enabled nsg rule to allow all port. I am not sure if the error is related to nsg rule or not.

Error: timeout - last error: unknown error Post "https://10.0.2.4:5986/wsman": dial tcp 10.0.2.4:5986: connectex: A connection attempt failed because the connected party did not properly respond after a 
period of time, or established connection failed because connected host has failed to respond.
resource "azurerm_virtual_machine" "myterraformvm" {

    name                  = "Test-01"
    location              = "East Us"
    resource_group_name = data.azurerm_resource_group.test.name
    network_interface_ids = ["${azurerm_network_interface.main.id}"]
    vm_size               = "Standard_DS1_v2"


    storage_os_disk {
    name              = "${azurerm_managed_disk.copy.name}"
    os_type           = "Windows"
    managed_disk_id   = "${azurerm_managed_disk.copy.id}"
    create_option     = "Attach"
    }

    provisioner "file" {
    source      = "./google_chrome_install.ps1"
    destination = "C:/"
     
     connection {
      host = "${azurerm_network_interface.main.private_ip_address}"
      type     = "winrm"
      https    = true
      port     = 5986
      use_ntlm = true
      insecure = true
      user     = "testadmin"
      password = "*******"
    }
  }



   provisioner "remote-exec" {

    connection {
      host = "${azurerm_network_interface.main.private_ip_address}"
      type     = "winrm"
      https    = true
      port     = 5986
      use_ntlm = true
      insecure = true
      user     = "testadmin"
      password = "***"
    }


inline = [
         "powershell -ExecutionPolicy Unrestricted -File C:/google_chrome_install.ps1 -Schedule"
        ]
      }
}
1
Do you have connectivity to the instance at 10.0.2.4 on 5986? How are you routing to that private IP address?ydaetskcoR
Also you are using https for ip. Guess it should be http.Marcin

1 Answers

0
votes

I had a similar problem with ssh connectivity from Terraform to aws ec2 instance. There are a few things I would like to mention. Please let me know if they dont work.

  1. This issue is probably because of firewall rules. My issue was with Security Groups (which is equivalent to firewall rules in aws) Please see if this article helps: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/nsg-quickstart-portal

  2. You should not be putting credentials in open text. You should always create private key (I created a pem key and used private_key parameter as mentioned in this article: https://github.com/DeekshithSN/Terraform/blob/master/Provisioner/file-Provisioner/main.tf

  3. You could probably use the same connection block if you create a module out of it. check this out for reference https://learn.hashicorp.com/tutorials/terraform/module-use