Skip to content

Instantly share code, notes, and snippets.

@knrt10
Last active February 12, 2024 05:04
Show Gist options
  • Save knrt10/b7562620430a7e766f800c60b0e92dc7 to your computer and use it in GitHub Desktop.
Save knrt10/b7562620430a7e766f800c60b0e92dc7 to your computer and use it in GitHub Desktop.
Create a VM on azure
#!/bin/bash
set -euo pipefail
set -x
export VM_NAME=stateless
export RESOURCE_GROUP=headlamp-stateless
export LOCATION="EAST US"
export IMAGE="Ubuntu2204"
export VM_SIZE=Standard_D8as_v5
# Create a key value pair for the ssh key with name headlamp and no password
ssh-keygen -m PEM -t rsa -b 4096 -N "" -f ~/.ssh/headlamp
ssh-add ~/.ssh/headlamp
export SSH_KEY=$(cat ~/.ssh/headlamp.pub)
export VM_USER="headlamp"
# Create a resource group
az group create \
--name "${RESOURCE_GROUP}" \
--location "${LOCATION}"
# Create a VM
az vm create \
--name "$VM_NAME" \
--resource-group "$RESOURCE_GROUP" \
--location "$LOCATION" \
--image "$IMAGE" \
--size "$VM_SIZE" \
--ssh-key-values "$SSH_KEY" \
--admin-username "$VM_USER" \
# Get the public IP of the VM
PUBLIC_IP=$(az vm show \
--resource-group "$RESOURCE_GROUP" \
--name "$VM_NAME" \
--show-details \
--query 'publicIps' \
--output tsv)
echo $PUBLIC_IP;
# Connect to the VM
ssh $VM_USER@$PUBLIC_IP -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment