What Is Cloud-init And How To Use It For Azure Linux VM

Let's say you have Linux VM and you want to install a web server on it then you have to login into VM and open the terminal and then run the script. But what if there is a way that will help you do this when your VM boots up? 

You can use cloud-init, which is widely used to customize Linux VM as it boots for the first time. So, if you want to install a web server on your Linux VM then you can use cloud-init on Azure when you're deploying VM on Azure. Cloud-init is another way in which you can pre-install packages on a new Linux virtual machine. This is specific to the Linux platform.

What is Cloud-init?

Cloud-init is a way to customize a Linux VM as it boots up for the first time. We can use cloud-init to install packages, write files and configure the security of the VM.

Advantages of using Cloud-init

  • Automate the installation of the package.
  • Automate configurations on VM.
  • Saves time.

How to use cloud-init on Azure VM

You need to have a Yaml file that stores the configuration for the installation of packages using this particular format for Linux. So let us create a configuration.yaml file and add the following commands. The file for the script is attached along with this article. Feel free to download and use it. Here the first command is to update all of the packages first and the second command is used to install the Nginx package on the virtual machine.

package_upgrade: true
 - nginx

Now go to the Azure portal.

Click go to the virtual machine and add new. If you don't know how to create a virtual machine then you can follow this article where I have explained  how to create a virtual machine. Now fill in all the details on Basic Tab and leave everything as it is and go to the Advanced tab. Now in the inbound ports, please select port 80 since we are going to be installing a web server on this Linux-based virtual machine.

What is Cloud init and how to use for Azure Linux VM

You will see custom data that's where you need to paste the code. Now paste the code here and click on Review and Create.

Give it some time for the deployment and once the deployment is complete, you can go to the overview page of the Virtual machine. Take the public IP of the newly created virtual machine and open it in a new tab.

You will see the Nginx has been installed and running on VM. So using this cloud-init. You don't have to manually install the packages. it could be automated.

What is Cloud init and how to use for Azure Linux VM

I hope this is helpful and happy learning. Stay safe.