Last active
February 12, 2024 05:04
-
-
Save knrt10/b7562620430a7e766f800c60b0e92dc7 to your computer and use it in GitHub Desktop.
Create a VM on azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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