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"
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.