Azure VM stuck in creating state when custom_data is passed

271 views
Skip to first unread message

Suraj Muthreja

unread,
May 18, 2018, 8:36:27 AM5/18/18
to Terraform
Hi Team,

I am deploying one VM on Azure using terraform template but when I use custom_data field then it always stuck in creating state and finally failed after around 40 mins.

If I dont use this custom_data field then VM is deployed successfully within 5-7 mins.

Here is the sample of Terraform template for VM creation field only along with custom_data file:

# Create virtual machine
resource "azurerm_virtual_machine" "myterraformvm" {
    name                  = "myVMTerraform"
    location              = "westus"
    resource_group_name   = "${azurerm_resource_group.myterraformgroup.name}"
primary_network_interface_id = "${azurerm_network_interface.myterraformnic1.id}"
    vm_size               = "Standard_D12"

    storage_os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
    }

    storage_image_reference {
        id="Resource ID of Image"
    }

    os_profile {
        computer_name  = "myVMTerraform"
        admin_username = "testadmin"
        custom_data = "${data.template_file.user_data.rendered}"
    }

    os_profile_linux_config {
        disable_password_authentication = true
        ssh_keys {
            path     = "/home/versa/.ssh/authorized_keys"
            key_data = "ssh key pasted here"
        }
    }

    boot_diagnostics {
        enabled = "true"
        storage_uri = "${azurerm_storage_account.mystorageaccount.primary_blob_endpoint}"
    }
    tags {
        environment = "myTerraformDemo"
    }
}

# Add template for data to use custom data:
data "template_file" "user_data" {
  template = "${file("ctrl.sh")}"
  
  vars {
    ctrl_mgmt_ip = "${azurerm_network_interface.myterraformnic1.private_ip_address}"
    netmask_eth0 = "${azurerm_subnet.myterraformsubnet.address_prefix}"
    ctrl_nb_ip = "${azurerm_network_interface.myterraformnic2.private_ip_address}"
    netmask_eth1 = "${azurerm_subnet.myterraformsubnet.address_prefix}"
    ctrl_sb_ip = "${azurerm_network_interface.myterraformnic3.private_ip_address}"
    netmask_eth2 = "${azurerm_subnet.myterraformsubnet.address_prefix}"
  }
}

=========================================================================================================
ctrl.sh file content is below:

#!/bin/bash
log_path="/tmp/log.txt"
echo "Starting cloud init script..." > $log_path
modify_interface() {
echo "Modifying /etc/network/interface file.." >> $log_path
cp /etc/network/interfaces /etc/network/interfaces.bak
cat > /etc/network/interfaces << EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address ${ctrl_mgmt_ip}
netmask ${netmask_eth0}
mtu 1200
dns-nameservers 8.8.8.8

# The secondary network interface
auto eth1
iface eth1 inet static
address ${ctrl_nb_ip}
netmask ${netmask_eth1}
mtu 1200

# The third network interface
auto eth2
iface eth2 inet static
address ${ctrl_sb_ip}
netmask ${netmask_eth2}
EOF
}

edit_resolv_conf() {
echo "Updating /etc/resolv.conf file.." >> $log_path
cat > /etc/resolv.conf << EOF
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
EOF
}

main() {
modify_interface
edit_resolv_conf
}
main
exit 0


Any help here would be appreciated.
Please confirm if I am missing something here.


Thanks,
Suraj Muthreja.


Suraj Muthreja

unread,
May 18, 2018, 7:17:04 PM5/18/18
to terrafo...@googlegroups.com
Any idea/suggestion here??


Thanks,
Suraj Muthreja.

From: terrafo...@googlegroups.com <terrafo...@googlegroups.com> on behalf of Suraj Muthreja <sur...@versa-networks.com>
Sent: Friday, May 18, 2018 6:06:27 PM
To: Terraform
Subject: [terraform] Azure VM stuck in creating state when custom_data is passed
 
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/445203c6-1c37-425b-9f2f-ac3d93c6f6c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rajinder Singh

unread,
May 19, 2018, 2:11:20 PM5/19/18
to Terraform
Can you log into the VM and examine logs?
Have you tried to use a simple script that just creates a file in the file system?
I have run into issues when my VM does not have access to the internet and my script is trying to access the internet.

If you cannot log into the VM to view logs I recommend using external log aggregation service like loggly. You init script should do verbose logging that can be view in Loggly without a need to log into the VM.

Suraj Muthreja

unread,
May 19, 2018, 3:18:47 PM5/19/18
to Terraform
Hi Rajinder,

I tried with simple echo "Hello World" and it worked fine.
But when using my bash script, it is failing. I dont have any internet dependency in my script.

Below is my script for reference.

cat > /etc/network/interfaces << EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address ${ ethaddress }
netmask ${ subnet }
mtu 1200
dns-nameservers 8.8.8.8

Similarly I have to update hostname and /etc/resolv.conf file.

Note: Here, I am updating the IP address and netmask via variables defined in data field which will further fetch the data from  {azurerm_network_interface.myterraformnic1.private_ip_address}
and
netmask ${azurerm_subnet.myterraformsubnet.address_prefix}

Please see if this creates any problem.

Further logging detail I can provide some time later on as dont have access to the system now.


Thanks,
Suraj Muthreja.



From: terraform -tool@ googlegroups.com < terraform -tool@ googlegroups.com > on behalf of Rajinder Singh <rejoinder@ gmail.com >
Sent: Saturday, May 19, 2018 6:11:19 PM
To: Terraform
Subject: Re : [ terraform ] Azure VM stuck in creating state when custom_data is passed
 
Can you log into the VM and examine logs?
Have you tried to use a simple script that just creates a file in the file system?
I have run into issues when my VM does not have access to the internet and my script is trying to access the internet.

If you cannot log into the VM to view logs I recommend using external log aggregation service like loggly. You init script should do verbose logging that can be view in Loggly without a need to log into the VM.

On Friday, May 18, 2018 at 6:17:04 PM UTC-5, Suraj Muthreja wrote:
Any idea/suggestion here??


Thanks,
Suraj Muthreja.
From: terrafo ...@ googlegroups.com < terrafo ...@ googlegroups.com > on behalf of Suraj Muthreja < sur ...@versa- networks.com >
        disable_password_ authentication = true
This mailing list is governed under the HashiCorp Community Guidelines - https :// www.hashicorp.com /community- guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
For more options, visit https :// groups.google.com /d/ optout.
--
This mailing list is governed under the HashiCorp Community Guidelines - https :// www.hashicorp.com /community- guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform -tool+unsubscribe@ googlegroups.com.

Rajinder Singh

unread,
May 19, 2018, 3:25:41 PM5/19/18
to terrafo...@googlegroups.com
Can you run the script manually in the vm? 


Sent from my iPhone
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/KMmUbP4yd7w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/BN6PR11MB1283E5472FF24837319697ECF4970%40BN6PR11MB1283.namprd11.prod.outlook.com.

Suraj Muthreja

unread,
May 20, 2018, 12:22:56 AM5/20/18
to terrafo...@googlegroups.com
I tried that running the script manually and it worked fine..


Thanks,
Suraj Muthreja.

From: terrafo...@googlegroups.com <terrafo...@googlegroups.com> on behalf of Rajinder Singh <rejo...@gmail.com>
Sent: Sunday, May 20, 2018 12:55:35 AM
To: terrafo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages