Get possible reserved instances in a subscription by region
Below is a little script that returns all possible reserved instances in a region for a subscription
Prerequisites:
- Be connected to AZ via an “az login” command
- The script takes for argument the target subscription id for which we want the possible reservations
The script lists the possible reserved instances in a subscription by first listing the available regions for a subscription then looping through them to get the possible instance reservations.
To run the script:
- Create a file possible-reserved-instances.sh and copy in it below code:
#!/bin/bash
SUBSCRIPTION_ID=$1
az account set -s "$SUBSCRIPTION_ID"
az account list-locations --query "[].{Name:name}" -o table>regions-list
sed -i -e '1,2d' regions-list
while read REGION; do
echo 'Reservations for region "'"$REGION"'" are available in ./regions/'"$REGION"
az reservations catalog show --reserved-resource-type "VirtualMachines" --location "$REGION" --subscription-id "$SUBSCRIPTION_ID" > output.json --only-show-errors>./regions/"$REGION"
done
- Add relevant permissions to run the script
chmod +x possible-reserved-instances.sh
- Launch script
./test-reserved.sh xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx