Skip to main content

Lab 001: Assigning Public IP to Virtual Machines


Requirements

  1. Create an Azure VM named devops-vm using any available Ubuntu image, with the VM size Standard_B1s.
  2. Generate an SSH public key on the azure-client host and associate it with the VM for SSH access.
  3. Associate a Static Public IP address named devops-pip with this VM.
  4. Ensure the VM is accessible via SSH using the generated public key.

Note

The solution can be implemented using both the Azure Cloud Console and the Azure CLI. This document outlines the CLI-based approach to accomplish these tasks. It is recommended to first explore the Azure Cloud Console for hands-on experience and a practical understanding of the process before utilizing the CLI approach, unless specifically instructed otherwise.

Prerequisites

  • Azure CLI is authenticated on the client host.
  • A resource group containing kml exists.
  • SSH key ~/.ssh/id_rsa.pub exists or can be generated.

Steps

RESOURCE_GROUP=$(az group list --query "[?contains(name, 'kml')].name | [0]" --output tsv)
LOCATION="eastus"
VM_NAME="devops-vm"
IMAGE="Ubuntu2404"
SIZE="Standard_B1s"
ADMIN_USERNAME="azureuser"
PUBLIC_IP_NAME="devops-pip"

if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -t rsa -b 2048 -f $HOME/.ssh/id_rsa -q -N ""
fi

# Create static public IP
az network public-ip create \
--resource-group "$RESOURCE_GROUP" \
--name "$PUBLIC_IP_NAME" \
--location "$LOCATION" \
--sku Standard \
--allocation-method Static

# Create VM
az vm create \
--resource-group "$RESOURCE_GROUP" \
--name "$VM_NAME" \
--image "$IMAGE" \
--size "$SIZE" \
--admin-username "$ADMIN_USERNAME" \
--ssh-key-values "$HOME/.ssh/id_rsa.pub" \
--location "$LOCATION" \
--storage-sku Standard_LRS \
--public-ip-address ""

# Get VM NIC name
NIC_NAME=$(az vm show \
--resource-group "$RESOURCE_GROUP" \
--name "$VM_NAME" \
--query "networkProfile.networkInterfaces[0].id" \
--output tsv | awk -F '/' '{print $NF}')

# Attach static public IP to VM NIC
az network nic ip-config update \
--resource-group "$RESOURCE_GROUP" \
--nic-name "$NIC_NAME" \
--name "ipconfig${VM_NAME}" \
--public-ip-address "$PUBLIC_IP_NAME"

VM_PUBLIC_IP=$(az network public-ip show \
--resource-group "$RESOURCE_GROUP" \
--name "$PUBLIC_IP_NAME" \
--query "ipAddress" \
--output tsv)

echo "VM Public IP: $VM_PUBLIC_IP"

Verification

  • Confirm devops-vm exists and is running.
  • Confirm devops-pip has a static public IP assigned.
  • Confirm SSH access is available: ssh azureuser@<public-ip>.

Resources

Azure CLI Docs