I want to copy file and run some shell commands after VM is created in Azure. I use provisioner 'file'
and provisioner 'remote-exec'
and created VM using ssh keys. Everything works fine till provisoner file and I get following error:
Error: timeout - last error: dial tcp :22: connect: connection refused
When I do ssh -i id_rsa <username>@<ip_address>
it works fine. I get this IP address from Azure Portal.
Here is my tf file:
resource "azurerm_resource_group" "myterraformgroup" {
name = "terrafromresources"
location = "eastus"
}
resource "azurerm_virtual_network" "myterraformnetwork" {
name = "terraformvnet"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
}
resource "azurerm_network_security_group" "myterraformnsg" {
name = "terraformNetworkSecurityGroup"
location = "eastus"
resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
security_rule {
name = "SSH"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_public_ip" "myterraformpublicip" {
name = "myPublicIP"
location = "eastus"
resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
allocation_method = "Dynamic"
}
resource "azurerm_linux_virtual_machine" "myterraformvm" {
name = "terraformVM"
location = "eastus"
resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
network_interface_ids = ["${azurerm_network_interface.myterraformnic.id}"]
size = "Standard_DS1_v2"
computer_name = "terrafromvm"
admin_username = "azureuser"
disable_password_authentication = true
admin_ssh_key {
username = "azureuser"
public_key = "${file("id_rsa.pub")}"
}
connection {
type = "ssh"
user = "azureuser"
host = "${azurerm_public_ip.myterraformpublicip.fqdn}"
private_key = "${file("id_rsa")}"
timeout = "5m"
}
provisioner "file" {
source = "example_file.txt"
destination = "/tmp/example_file.yml"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
]
}
}
id_rsa and id_rsa.pub are in same folder are .tf file.
Also tried higher timeouts with 10m and 15m.
Thanks
This github issue addresses the same problem as yours and proper explanation is provided for this issue.
The fix for this problem is updating the allocation_method to "Static".
Hope this helps!