# Day 23: Automating User Data Configuration Using Azure CLI
🎯 Objective
Create an Azure Virtual Machine using Azure CLI and automatically configure it at launch using a User Data / cloud-init script.
In this example, we will:
👉 This eliminates the need to SSH into the VM and manually install Nginx after deployment.
🧠 Concept
Azure CLI allows us to create and configure Azure resources directly from the terminal.
Instead of manually creating a VM through the Azure Portal, we can use:
az vm createWe can also provide a startup script using:
--custom-data nginx.shThe VM's cloud-init process reads the script during the initial boot and executes the configuration automatically.
Flow
Azure CLI
↓
az vm create
↓
Azure VM Created
↓
cloud-init reads nginx.sh
↓
Install Nginx
↓
Start Nginx
↓
VM ready to serve HTTP traffic⚙️ Step 1: Check Azure Account
Before creating resources, verify that Azure CLI is authenticated with the correct account.
az account showThis displays information such as:
📦 Step 2: List Resource Groups
Check the available resource groups:
az group list -o tableThe -o table option displays the result in an easier-to-read table format.
🔧 Step 3: Store Resource Group Name
Instead of repeatedly typing the resource group name, store it in a shell variable:
rg_name="{resource-group-name}"Example:
rg_name="devops-rg"Now we can reference it using:
$rg_name📝 Step 4: Create the User Data Script
Create a shell script:
vim nginx.shAdd:
#!/bin/bash
apt update -y
apt install -y nginx
systemctl start nginx
systemctl enable nginxSave and exit the file.
What This Script Does
apt update
↓
Update package repository
apt install nginx
↓
Install Nginx
systemctl start nginx
↓
Start Nginx immediately
systemctl enable nginx
↓
Automatically start Nginx after reboot🚀 Step 5: Create the Azure VM
Create the VM and pass the Nginx script as custom data:
az vm create \
--resource-group "$rg_name" \
--name devops-vm \
--image Ubuntu2404 \
--location eastus \
--size Standard_B1s \
--admin-username azureuser \
--generate-ssh-keys \
--custom-data nginx.sh \
--os-disk-size-gb 30 \
--storage-sku Standard_LRSImportant Options
The important part for automation is:
--custom-data nginx.shThis allows the VM to configure itself during the initial boot.
📋 Step 6: Check Created Resources
List all resources created inside the resource group:
az resource list \
--resource-group "$rg_name" \
-o tableYou should see resources related to the VM, such as:
🌐 Step 7: Open HTTP Port 80
Nginx listens on TCP port 80 by default.
Allow incoming HTTP traffic:
az vm open-port \
--resource-group "$rg_name" \
--name devops-vm \
--port 80This creates or updates an inbound rule in the VM's Network Security Group (NSG).
Without this rule, Nginx may be running correctly but won't be accessible from the internet.
🔐 Step 8: Verify NSG Rules
Check the Network Security Group rules:
az network nsg rule list \
--resource-group "$rg_name" \
--nsg-name devops-vmNSG \
-o tableLook for an inbound rule allowing:
TCP → Port 80 → Allow> The actual NSG name can differ depending on how the VM/network resources were created. Check your resource list if devops-vmNSG is not the generated name.
🔍 Step 9: Check VM Details
Display detailed VM information:
az vm show \
--resource-group "$rg_name" \
--name devops-vm \
--show-detailsThis provides information such as:
For a cleaner output, you can also use:
az vm show \
--resource-group "$rg_name" \
--name devops-vm \
--show-details \
-o table🌍 Step 10: Test Nginx
Get the public IP address:
az vm show \
--resource-group "$rg_name" \
--name devops-vm \
--show-details \
--query publicIps \
-o tsvThen test it:
curl http://<PUBLIC_IP>Or open it in a browser:
http://<PUBLIC_IP>If the configuration was successful, the default Welcome to nginx! page should appear.
🔍 Troubleshooting User Data
If Nginx was not installed correctly, SSH into the VM:
ssh azureuser@<PUBLIC_IP>Check Nginx:
sudo systemctl status nginxCheck cloud-init status:
cloud-init statusCheck the cloud-init output:
sudo cat /var/log/cloud-init-output.logThese logs are especially useful when the nginx.sh script fails during the first boot.
⚠️ Important Notes
🧪 Validation Checklist
🔗 Reference
Microsoft Azure CLI — VM Commands:
https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest
📌 Summary
Azure CLI can automate both infrastructure provisioning and initial server configuration.
Instead of:
Create VM
→ SSH into VM
→ Update packages
→ Install Nginx
→ Start Nginx
→ Configure networkingwe can automate most of the process:
nginx.sh
↓
az vm create --custom-data
↓
Azure creates VM
↓
cloud-init executes script
↓
Nginx installed and started
↓
Port 80 opened through NSG
↓
Web server readyThis approach makes VM deployment faster, repeatable, and less dependent on manual configuration.