Created
May 28, 2025 15:50
-
-
Save brandonmartinez/ef8f7f172f21f1b2b02d18e7ea09a066 to your computer and use it in GitHub Desktop.
Attempts to reserve capacity across Azure zones for a selected VM SKU.
This file contains hidden or 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 | |
# ----------------------------------------------------------------------------- | |
# DISCLAIMER: | |
# This script is provided as a sample for informational purposes only. | |
# There is no guarantee of fitness for a particular purpose, and it may not be | |
# suitable for use in production environments. Use at your own risk. | |
# The authors and distributors accept no liability for any damages or losses | |
# resulting from the use or misuse of this script. Review, test, and modify | |
# as needed before deploying in any environment. | |
# ----------------------------------------------------------------------------- | |
set -e | |
# --- User-defined variables --- | |
SUBSCRIPTION_ID="yourSubscriptionId" | |
RG_NAME="yourResourceGroup" | |
LOCATION="eastus2" | |
CRG_NAME="yourCapacityReservationGroup" | |
VM_SIZE="Standard_E32s_v5" | |
TARGET_COUNT=15 | |
ZONES="1 2 3" | |
# --- Check if user is logged in to Azure CLI --- | |
echo "Checking if user is logged in to Azure CLI..." | |
if ! az account show >/dev/null 2>&1; then | |
echo "Not logged in to Azure CLI. Attempting to log in..." | |
az login | |
fi | |
USER=$(az account show --query user.name -o tsv 2>/dev/null) | |
if [ -n "$USER" ]; then | |
echo "Logged in as: $USER" | |
fi | |
# --- Set the subscription --- | |
az account set --subscription "$SUBSCRIPTION_ID" | |
# --- Check if Capacity Reservation Group exists --- | |
echo "Checking if Capacity Reservation Group '$CRG_NAME' exists..." | |
CRG_EXISTS=$(az capacity reservation group show \ | |
--resource-group "$RG_NAME" \ | |
-n "$CRG_NAME" \ | |
--query "name" \ | |
--output tsv 2>/dev/null || true) | |
if [ -z "$CRG_EXISTS" ]; then | |
echo "Capacity Reservation Group not found. Creating group..." | |
az capacity reservation group create \ | |
--resource-group "$RG_NAME" \ | |
--location "$LOCATION" \ | |
-n "$CRG_NAME" \ | |
--zones $ZONES | |
else | |
echo "Capacity Reservation Group '$CRG_NAME' already exists." | |
fi | |
# --- Get current reservations for the specified SKU in each zone --- | |
for ZONE in $ZONES; do | |
echo "Retrieving current reserved quantity for VM size '$VM_SIZE' in zone '$ZONE'..." | |
CURRENT_COUNT=$(az capacity reservation list \ | |
--resource-group "$RG_NAME" \ | |
--capacity-reservation-group "$CRG_NAME" \ | |
--query "[?sku.name=='$VM_SIZE' && properties.zone=='$ZONE'].properties.quantity" \ | |
--output tsv | awk '{sum+=$1} END {print sum}') | |
if [ -z "$CURRENT_COUNT" ]; then | |
CURRENT_COUNT=0 | |
fi | |
# --- Calculate how many more are needed --- | |
NEEDED_COUNT=$((TARGET_COUNT - CURRENT_COUNT)) | |
# --- Output summary --- | |
echo "Zone: $ZONE" | |
echo "Current reserved count: $CURRENT_COUNT" | |
echo "Target reserved count: $TARGET_COUNT" | |
if [ "$NEEDED_COUNT" -le 0 ]; then | |
echo "Reservation target already met or exceeded for zone '$ZONE'. No further action needed." | |
else | |
echo "Additional $NEEDED_COUNT instance(s) needed to meet target for zone '$ZONE'." | |
for ((i = 1; i <= NEEDED_COUNT; i++)); do | |
RES_NAME="${CRG_NAME}-${VM_SIZE//_/}-zone${ZONE}-resv-$((CURRENT_COUNT + i))" | |
echo "Creating capacity reservation '$RES_NAME' for VM size '$VM_SIZE' in zone '$ZONE'..." | |
az capacity reservation create \ | |
--resource-group "$RG_NAME" \ | |
--capacity-reservation-group "$CRG_NAME" \ | |
-n "$RES_NAME" \ | |
--sku "$VM_SIZE" \ | |
--capacity 1 \ | |
--location "$LOCATION" \ | |
--zone "$ZONE" | |
done | |
echo "All required reservations have been created for zone '$ZONE'." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment