Deploying Azure Infrastructure: Virtual Network & VMs  with Azure CLI

Deploying Azure Infrastructure: Virtual Network & VMs with Azure CLI

Navigating the world of cloud infrastructure can often seem daunting, but with the right tools and guidance, it becomes a seamless process. In this blog post, we will dive into how to deploy a virtual network and virtual machines (VMs) on Microsoft Azure using the Azure Command-Line Interface (CLI), a powerful tool that allows for managing Azure services directly from the command line. Whether you're working from Windows, Linux, or Mac OS, this guide will help you set up your cloud infrastructure smoothly and efficiently.

Prerequisites

Before we get started, you'll need:

  • An Azure account. Sign up for a free trial here if you don't have one already.

  • The Azure CLI installed on your machine. Check out the official installation guide.

Step 1: Environment Setup

For Linux and Mac OS Users

Open your terminal and create Bash shell variables for your virtual network (vnet) and subnet:

vnetName="ismt-vnet"
subnetName="ismt-public-subnet-1"
vnetAddressPrefix="172.168.0.0/16"
subnetAddressPrefix="172.168.0.0/24"
resourceGroup="az-900"
location="eastus"

For Windows Users

Launch PowerShell and initialize variables for your setup:

$vnetName="ismt-vnet"
$subnetName="ismt-public-subnet-1"
$vnetAddressPrefix="172.168.0.0/16"
$subnetAddressPrefix="172.168.0.0/24"
$resourceGroup="az-900"
$location="eastus"

Step 2: Create a Resource Group

A resource group is essentially a logical container for Azure resources. Create one with:

az group create --name $resourceGroup --location $location

Step 3: Deploy the Virtual Network

Set up your virtual network and a subnet within your resource group:

For Linux and Mac OS Users

az network vnet create \
  --name $vnetName \
  --resource-group $resourceGroup \
  --address-prefixes $vnetAddressPrefix \
  --subnet-name $subnetName \
  --subnet-prefixes $subnetAddressPrefix

For Windows Users

az network vnet create `
  --name $vnetName `
  --resource-group $resourceGroup `
  --address-prefixes $vnetAddressPrefix `
  --subnet-name $subnetName `
  --subnet-prefixes $subnetAddressPrefix

Step 4: Spin Up Your VM

Deploy an Azure VM that will be part of your network:

For Linux and Mac OS

# create Bash shell variable
vmName="ismt-server"
image="Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest"
admin_username="azureuser"
admin_password="Admin@123456789"

az vm create \
  --resource-group $resourceGroup \
  --name $vmName \
  --image $image \
  --vnet-name $vnetName \
  --subnet $subnetName \
  --admin-username $admin_username \
  --admin-password $admin_password

For Windows (Powershell)

# create Bash shell variable
$vmName="ismt-server"
$image="Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest"
$admin_username="azureuser"
$admin_password="Admin@123456789"

az vm create `
  --resource-group $resourceGroup `
  --name $vmName `
  --image $image `
  --vnet-name $vnetName `
  --subnet $subnetName `
  --admin-username $admin_username `
  --admin-password $admin_password

Step 5: Configure Network Accessibility

Ensure your website can be reached by opening the necessary ports:

 az vm open-port --name $vmName --resource-group $resourceGroup --port 80 --priority 900
 az vm open-port --name $vmName --resource-group $resourceGroup --port 443 --priority 901

SSH to your VM

ssh azureuser@<public-ip-address>

Step 6: Deploy Your Static Website with Nginx

#!/bin/bash

# Ensure the script is run with root privileges
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Step 1: Update the operating system packages
apt update -y

# Step 2: Install Nginx
apt install nginx -y

# Step 3: Navigate to /var/www and create a folder named your_domain.com
cd /var/www
mkdir mystic.com
cd mystic.com

# Step 4: Clone the GitHub repository into the directory
# Replace 'https://github.com/siddhantbhattarai/e-commerce-static-site.git' with your actual GitHub repository URL
git clone https://github.com/siddhantbhattarai/e-commerce-static-site.git .

# Ensure git is installed or handle its absence
if ! command -v git &> /dev/null
then
    echo "git could not be found, installing now..."
    apt install git -y
    git clone https://github.com/siddhantbhattarai/e-commerce-static-site.git .
fi

# Step 5: Configure Nginx to serve your site
# Navigate to Nginx sites-available directory and create a new configuration
cd /etc/nginx/sites-available/

# Note: It's better to use the sites-available and sites-enabled pattern with symlinks
# Replace 'mystic.com' with the actual domain name file for clarity, e.g., your_domain.com
sudo tee mystic.com <<EOF
server {
    listen 80;
    listen [::]:80;
    server_name mystic.com;

    root /var/www/mystic.com;
    index index.html;

    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOF

# Create a symlink to enable the site
ln -s /etc/nginx/sites-available/mystic.com /etc/nginx/sites-enabled/

# Optional: Remove the default Nginx site configuration to avoid conflicts
rm /etc/nginx/sites-enabled/default

# Step 7: Test Nginx configuration for syntax errors
nginx -t

# Step 8: Restart Nginx to apply the changes
systemctl restart nginx

# Display a success message
echo "Nginx has been configured to serve your site and restarted successfully!"

Resource Cleanup

To avoid unnecessary charges, it's important to delete resources you no longer need:

  az group delete --name $resourceGroup --yes --no-wait

Wrapping Up

Deploying infrastructure on Azure using the CLI not only simplifies the process but also introduces flexibility and automation capabilities to your workflow. This guide covered setting up a virtual network and deploying a VM to host a static website, showcasing the Azure CLI's power and efficiency.

As you become more comfortable with these tools, you'll find that the Azure CLI is an invaluable resource for managing your cloud infrastructure. Dive in, explore, and happy deploying!