|
| 1 | +# Autoscale IOPS for multiple Azure MySQL Flexible Server |
| 2 | + |
| 3 | +Costa Rica |
| 4 | + |
| 5 | +[](https://github.com/) |
| 6 | +[brown9804](https://github.com/brown9804) |
| 7 | + |
| 8 | +Last updated: 2025-05-09 |
| 9 | + |
| 10 | +---------- |
| 11 | + |
| 12 | +> Using [Python 3.7+](https://www.python.org/downloads/source/) |
| 13 | +
|
| 14 | +> [!NOTE] |
| 15 | +> Enable Autoscale IOPS via REST API, as now this is the only way to automate enabling Autoscale IOPS since, Azure CLI and PowerShell do not support this setting yet. |
| 16 | +
|
| 17 | +> [!IMPORTANT] |
| 18 | +> Autoscale IOPS is `only available` for the `General Purpose` and `Business Critical tiers`. `Burstable tier` (B-series) servers (e.g., B1ms) `do not support autoscale IOPS`. |
| 19 | +
|
| 20 | +## Pre-requisites |
| 21 | + |
| 22 | +- Install azure-identity with: `py -m pip install azure-identity requests` |
| 23 | + |
| 24 | + <img width="550" alt="image" src="https://github.com/user-attachments/assets/fa74f47c-bef2-4ad3-8b0f-2ee50813c486" /> |
| 25 | + |
| 26 | +- [Install Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to work with both Terraform and Azure commands. |
| 27 | + |
| 28 | + <img width="550" alt="image" src="https://github.com/user-attachments/assets/3f552ecc-8e07-453a-9655-8bb5a89e1791" /> |
| 29 | + |
| 30 | +## By Resource Group |
| 31 | + |
| 32 | +> - Automatically retrieves your **Azure subscription ID** using the Azure CLI. <br/> |
| 33 | +> - Prompts you only for the **resource group name**. <br/> |
| 34 | +> - Lists all MySQL Flexible Servers in that resource group. <br/> |
| 35 | +> - Sends a `PATCH request`` to enable `autoIoScaling` for each server using the `Azure REST API` |
| 36 | +
|
| 37 | +1. Make sure you're logged in: `az login` |
| 38 | + |
| 39 | + |
| 40 | +## Across a Subscription |
| 41 | + |
| 42 | +> You can also enable autoscale IOPS across an entire subscription, it requires: <br/> |
| 43 | +> - Listing all MySQL Flexible Servers in the subscription. <br/> |
| 44 | +> - For each server, retrieving its resource group. <br/> |
| 45 | +> - Applying the update if the server is in a supported tier (General Purpose or Business Critical). <br/> |
| 46 | +
|
| 47 | +## Azure CLI Script to Enable Autoscale IOPS |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> The script below will execute: <br/> |
| 51 | +> - Using Python 3.13.3 <br/> |
| 52 | +> - Checks if Azure CLI is available at the specified path. <br/> |
| 53 | +> - Lists all MySQL Flexible Servers. <br/> |
| 54 | +> - Tries to get each server’s resource group. <br/> |
| 55 | +> - If the resource group is missing, it prompts you to enter it. <br/> |
| 56 | +> - Asks for confirmation before enabling autoscale IOPS. <br/> |
| 57 | +
|
| 58 | +```python |
| 59 | +import subprocess |
| 60 | +import json |
| 61 | + |
| 62 | +# Step 1: Get list of MySQL Flexible Servers |
| 63 | +servers_output = subprocess.check_output( |
| 64 | + ["az", "mysql", "flexible-server", "list", "--query", "[].name", "-o", "tsv"], |
| 65 | + text=True |
| 66 | +) |
| 67 | +servers = servers_output.strip().splitlines() |
| 68 | + |
| 69 | +# Step 2: Loop through each server and enable autoscale IOPS |
| 70 | +for server in servers: |
| 71 | + # Get the resource group for the server |
| 72 | + rg_output = subprocess.check_output( |
| 73 | + ["az", "mysql", "flexible-server", "show", "--name", server, "--query", "resourceGroup", "-o", "tsv"], |
| 74 | + text=True |
| 75 | + ) |
| 76 | + resource_group = rg_output.strip() |
| 77 | + |
| 78 | + print(f"Enabling autoscale IOPS for {server} in {resource_group}...") |
| 79 | + |
| 80 | + # Update the server to enable autoscale IOPS |
| 81 | + subprocess.run([ |
| 82 | + "az", "mysql", "flexible-server", "update", |
| 83 | + "--name", server, |
| 84 | + "--resource-group", resource_group, |
| 85 | + "--iops", "Auto" |
| 86 | + ]) |
| 87 | +``` |
| 88 | + |
| 89 | +## How to execute it Azure CLI Script to Enable Autoscale IOPS |
| 90 | + |
| 91 | +1. Download [the script](./enable_autoscale_iops.py) to your local machine or a cloud shell environment. |
| 92 | +2. Make sure you're logged in: `az login` |
| 93 | +4. Run the script: `py enable_autoscale_iops.py` |
| 94 | + |
| 95 | +> Example: enabling Autoscale IOPS on three different servers, each hosted in a separate resource group but under the same subscription. |
| 96 | +
|
| 97 | +<img width="550" alt="image" src="https://github.com/user-attachments/assets/22aa763d-b358-441a-b5b9-aa0197ce680d" /> |
| 98 | + |
| 99 | + |
| 100 | +<div align="center"> |
| 101 | + <h3 style="color: #4CAF50;">Total Visitors</h3> |
| 102 | + <img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/> |
| 103 | +</div> |
0 commit comments