Creating a Linux VM with startup script in Azure(1/2)

Aditya Garg
3 min readMay 13, 2021

--

Hello everyone! Somehow while doing my AWS and GCP courses, I stumbled upon exploring this concept for Azure and surprisingly not too much information is clearly available out there on the Internet for this task(and using same concept further while creating VMSS. Most lectures focus on cloud init formats).

So, here’s how we can create a virtual machine(Linux) and make it run startup commands(script) as soon as it is powered on:

  1. Navigate to Virtual Machines service.

2.Add>>Virtual Machine

3.Fill in details as below(your choice of Resource Group, VM name,Region, Username/Password):

I would recommend creating a new Resource Group so that the cleanup is easy later on where we can just delete the Resource Group.

Create a CentOS Image.

Allow ports 22 and 80 inbound.

4.Leave Disks, Networking, Management sections to Default Settings and move on to ‘Advanced’ section.

Here, we have 2 ways to achieve the same task:

i-Enter the startup script(called ‘User Data’ in AWS, ’Custom Data’ in Azure!!)to be executed at boot(executed only once)here:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo “<h1>Hello World from $(hostname -f)</h1>” > /var/www/html/index.html

Review+create.

~~~~~~~

Alternatively,

~~~~~~~~

ii-Use an extension for Linux: <This is covered in article 2 here,may be avoided for practical purposes>

https://adityagarg94.medium.com/creating-a-linux-vm-with-startup-script-in-azure-2-2-aa6ee00780e3

5.Finally,it may prompt for contact detail(CentOS requirement).

Click on Create.

6.Head over to the VM once created, note its public IP address:

Use the same to launch a web browser section(!!Use http):

And Voila! httpd service was installed automatically alongwith our custom message being displayed!

Do give it a few minutes as the run time commands from script may take a while :)

7.Cleanup!!Delete the VM and also its attached disk and network interfaces.

Alternatively, you can just go to the Resource Group and delete it.

Also delete the blob(script uploaded),container and storage account if you have used the Linux Extension method.

P.S. For all those pursuing their Azure Certification journey,I used the Xaas Academy courses from Udemy to clear AZ900 and AZ104.Excellent content with hands-on and well formatted pdfs for the lectures.

--

--

Aditya Garg

Cloud Consultant |Zumba Enthusiast | Seaside Lover | 2XAzure, 1XAWS, 1XCCNP, 1XCCNA