Skip to content

Instantly share code, notes, and snippets.

@immanuelpotter
Last active May 12, 2021 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save immanuelpotter/9a34a20e6b07a9e653c658ec6e90df81 to your computer and use it in GitHub Desktop.
Save immanuelpotter/9a34a20e6b07a9e653c658ec6e90df81 to your computer and use it in GitHub Desktop.
#!/bin/bash
NAME_FORMAT="$(date +%d%m%y-azure-%H%M%S)"
# Set these
SSH_KEY_VALUE=""
RESOURCE_GROUP=""
LOCATION="" # Region the instance is in
SEC_GROUP="" # Network security group - using a pre-made here
VNET="" # virtual network to create in
IDENTITY="" # Managed identity to assign to the instance
STORAGE_ACCOUNT="" # SA the VHD should be in
CONTAINER_NAME="" # "bucket" inside SA
USER="" # Admin user for instance
VHD="" # VHD to built machine from
create_public_ip(){
az network public-ip create \
--resource-group $RESOURCE_GROUP \
--location $LOCATION \
--allocation-method Dynamic \
--name azure-public-ip-${NAME_FORMAT} \
--dns-name ${NAME_FORMAT}
}
create_nic(){
az network nic create \
--resource-group $RESOURCE_GROUP \
--location $LOCATION \
--network-security-group $SEC_GROUP \
--name azure-nic-${NAME_FORMAT} \
--subnet default \
--vnet-name $VNET \
--public-ip-address azure-public-ip-${NAME_FORMAT}
}
create_vm(){
az vm create \
--resource-group $RESOURCE_GROUP \
--location $LOCATION \
--assign-identity /subscriptions/<token>/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/${IDENTITY} \
--nics azure-nic-${NAME_FORMAT} \
--name azure-instance-${NAME_FORMAT} \
--os-type Linux \
--use-unmanaged-disk \
--image https://${RESOURCE_GROUP}.blob.core.windows.net/${CONTAINER_NAME}/Microsoft.Compute/path/to/vhd/${VHD} \
--os-disk-name ${NAME_FORMAT}.vhd \
--storage-account $STORAGE_ACCOUNT \
--storage-container-name $CONTAINER_NAME \
--admin-username $USER \
--ssh-key-value "${SSH_KEY_VALUE}" \
--ssh-dest-key-path /home/${USER}/.ssh/authorized_keys \
--os-disk-size-gb 30 \
--size Standard_B2s
}
main(){
create_public_ip
create_nic
create_vm
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment