azure create vm

Azure CLI Commands & Practice Workflow

1. Retrieve Resource Group Name

Query the first available resource group and output as tab-separated values (tsv) to strip quotes and extra formatting:

Bash

rg_name="$(az group list --query '[0].name' -o tsv)"

2. Create Key Vault Certificate

Create a new certificate using Azure Key Vault’s default policy:

Bash

az keyvault certificate create \
  --vault-name vaultname \
  -n cert1 \
  -p "$(az keyvault certificate get-default-policy)"

3. Retrieve Secret Version IDs

Get all enabled secret version IDs associated with the certificate:

Bash

secrets=$(az keyvault secret list-versions \
  --vault-name vaultname \
  -n cert1 \
  --query "[?attributes.enabled].id" \
  -o tsv)

4. Format Secret for VM Deployment

Format the secret ID to prepare it for attachment during VM creation:

Bash

vm_secrets=$(az vm secret format -s "$secrets")

5. Create Virtual Machine

Command Syntax

Bash

az vm create \
  -n nautilus-vm \
  -g $rg_name \
  --image Ubuntu2204 \
  --size Standard_DS2_v2 \
  --admin-username azureuser \
  --generate-ssh-keys \
  --data-disk-sizes-gb 30 \
  --storage-sku Standard_LRS

Command Execution

Bash

az vm create \
  -n nautilus-vm \
  -g kml_rg_main-01cde7595e7b4e3e \
  --image Ubuntu2204 \
  --size Standard_B2s \
  --admin-username azureuser \
  --generate-ssh-keys \
  --data-disk-sizes-gb 30 \
  --storage-sku Standard_LRS

Creation Output

JSON

{
  "fqdns": "",
  "id": "/subscriptions/f0c3bcdd-5ce2-4fa0-8cf3-41559747512b/resourceGroups/kml_rg_main-01cde7595e7b4e3e/providers/Microsoft.Compute/virtualMachines/nautilus-vm",
  "location": "eastus",
  "macAddress": "00-22-48-2B-7F-E6",
  "powerState": "VM running",
  "privateIpAddress": "10.0.0.4",
  "publicIpAddress": "20.25.33.164",
  "resourceGroup": "kml_rg_main-01cde7595e7b4e3e",
  "zones": ""
}

6. List Virtual Machines (Table Output)

List all VMs with details including Power State and Provisioning State in a formatted table:

Bash

az vm list -d --query "[].{Name:name, ResourceGroup:resourceGroup, PowerState:powerState, ProvisioningState:provisioningState}" -o table

0 comments:

Post a Comment