Auto Scale Azure Database for MySQL Using Azure Automation

%3CLINGO-SUB%20id%3D%22lingo-sub-1560224%22%20slang%3D%22en-US%22%3EAuto%20Scale%20Azure%20Database%20for%20MySQL%20Using%20Azure%20Automation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1560224%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EAuto%20Scale%20Azure%20Database%20for%20MySQL%20Using%20Azure%20Automation%20%26amp%3B%20PowerShell%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIdentify%20the%20load%20and%20scaling%20up%20or%20downgrading%20your%20compute%20capacity%20is%20critical%20for%20business%20and%20to%20achieve%20it%20requires%20manual%20intervention%20%26amp%3B%20downtime.%20On%20the%20other%20hand%2C%20continuous%20efforts%5Cresources%20needed%20to%20monitor%20the%20environment.%20In%20the%20managed%20database%20scenario%2C%20most%20of%20the%20services%20already%20offering%20the%20auto%20scale%5Cdown%20capability%20in-built.%20In%20the%20case%20of%20the%20Azure%20Database%20for%20MySQL%2C%20we%20can%20configure%20the%20autoscale%20capability%20from%20the%20storage%20front%20however%20compute%20require%20manual%20intervention%20to%20achieve%20the%20same%20by%20increasing%20the%20SKU%20tiers%20or%20automate%20the%20whole%20process.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20simplify%20the%20process%2C%20I%E2%80%99m%20using%20the%20Azure%20Automation%20account%2C%20Automation%20Runbook%2C%20and%20PowerShell%2C%20explained%20the%20whole%20process%20step%20by%20step%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3ESteps%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ECreate%20Azure%20Automate%20Account%20from%20the%20Azure%20portal.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_0-1596279705828.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209524iA2C9290CC5B6AECD%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_0-1596279705828.png%22%20alt%3D%22GuptaNitin_0-1596279705828.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3EAs%20we%E2%80%99re%20accessing%20a%20couple%20of%20Azure%20PowerShell%20modules%20to%20access%20Azure%20accounts%20(AZ.%20Accounts)%20and%20MySQL%20(AZ.%20MySQL)%20resources%2C%20by%20default%20those%20modules%20are%20not%20incorporated%20hence%20will%20have%20to%20import%20it%20directly%20from%20the%20Module%20gallery.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_1-1596279705835.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209525iCE2BB0FB6D77E178%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_1-1596279705835.png%22%20alt%3D%22GuptaNitin_1-1596279705835.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%223%22%3E%0A%3CLI%3EIn%20the%20next%20step%2C%20we%20require%20Runbook%20where%20will%20have%20to%20define%20the%20flow%20or%20script%20down%20the%20complete%20process%20for%20Scale%20up%5Cdown%20(To%20ease%20I%20have%20created%20the%20two%20different%20rulebooks%20for%20both%20Scales%20up%20%26amp%3B%20down).%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EWhile%20creating%2C%20have%20to%20choose%20the%20mode%20which%20you%20want%20to%20leverage%20(In%20my%20case%20I%20have%20selected%20PowerShell%2C%20however%20you%E2%80%99re%20open%20to%20select%20Python%20as%20well).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_2-1596279705839.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209523i81EBC37CC3B4721A%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_2-1596279705839.png%22%20alt%3D%22GuptaNitin_2-1596279705839.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%224%22%3E%0A%3CLI%3ENow%20ready%20to%20show%20your%20coding%20skill%2C%20however%2C%20I%20included%20the%20basic%20code%20into%20the%20runbook%20console%20for%20changing%20your%20SKU%20values%20or%20Scaling%20up.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%23%20Parameters%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%5BCmdletBinding()%5D%0A%0Aparam(%0A%0A%20%20%20%20%5BParameter(Mandatory%3D%24True%2CPosition%3D0)%5D%0A%0A%20%20%20%20%5BValidateLength(1%2C100)%5D%0A%0A%20%20%20%20%5Bstring%5D%24ResourceGroupName%2C%0A%0A%0A%0A%20%20%20%20%5BParameter(Mandatory%3D%24True%2CPosition%3D1)%5D%0A%0A%20%20%20%20%5BValidateLength(1%2C100)%5D%0A%0A%20%20%20%20%5Bstring%5D%24ServerName%2C%0A%0A%20%20%20%20%20%0A%0A%20%20%20%20%5BParameter(Mandatory%3D%24False%2CPosition%3D2)%5D%0A%0A%20%20%20%20%5BValidateLength(1%2C100)%5D%0A%0A%20%20%20%20%5Bstring%5D%24SkuTier%0A%0A)%0A%0A%0A%0A%23%20Track%20the%20execution%20date%20%26amp%3B%20time%0A%0A%24StartDate%3D(GET-DATE)%0A%0A%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%23%20Log-in%20to%20Azure%20with%20AZ%20(standard%20code)%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0AWrite-Verbose%20-Message%20'Connecting%20to%20Azure'%0A%0A%20%20%0A%0A%23%20Name%20of%20the%20Azure%20Run%20As%20a%20connection%0A%0A%24ConnectionName%20%3D%20'AzureRunAsConnection'%0A%0Atry%0A%0A%7B%0A%0A%20%20%20%20%23%20Get%20the%20connection%20properties%20to%20connect%20Azure%20resources%0A%0A%20%20%20%20%24ServicePrincipalConnection%20%3D%20Get-AutomationConnection%20-Name%20%24ConnectionName%20%20%20%20%20%20%0A%0A%20%20%20%0A%0A%20%20%20%20'Log%20in%20to%20Azure...'%0A%0A%20%20%20%20%24null%20%3D%20Connect-AzAccount%20%60%0A%0A%20%20%20%20%20%20%20%20-ServicePrincipal%20%60%0A%0A%20%20%20%20%20%20%20%20-TenantId%20%24ServicePrincipalConnection.TenantId%20%60%0A%0A%20%20%20%20%20%20%20%20-ApplicationId%20%24ServicePrincipalConnection.ApplicationId%20%60%0A%0A%20%20%20%20%20%20%20%20-CertificateThumbprint%20%24ServicePrincipalConnection.CertificateThumbprint%20%0A%0A%7D%0A%0Acatch%20%0A%0A%7B%0A%0A%20%20%20%20if%20(!%24ServicePrincipalConnection)%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%23Missed%20to%20'Create%20Azure%20Run%20As%20account'%20%0A%0A%20%20%20%20%20%20%20%20%24ErrorMessage%20%3D%20%22Connection%20%24ConnectionName%20not%20found.%22%0A%0A%20%20%20%20%20%20%20%20throw%20%24ErrorMessage%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20else%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%23%20additional%20execption%0A%0A%20%20%20%20%20%20%20%20Write-Error%20-Message%20%24_.Exception.Message%0A%0A%20%20%20%20%20%20%20%20throw%20%24_.Exception%0A%0A%20%20%20%20%7D%0A%0A%7D%0A%0A%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%23%20Setting%20SKU%20Tier%20for%20MySQLDatabase%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%24CurrentSKU%20%3D%20(Get-AzMySqlServer%20-Name%20%24ServerName%20-ResourceGroupName%20%24ResourceGroupName).skuname%0A%0A%0A%0AIf(%24CurrentSKU%20-eq%20%24SkuTier)%0A%0A%7B%0A%0A%20%20%20%20%23%20Validating%20the%20existing%20SKU%20value%0A%0A%20%20%20%20Write-Error%20%22Cannot%20change%20pricing%20tier%20of%20%24ServerName%20because%20the%20new%20SKU%20%24SkuTier%20tier%20is%20equal%20to%20current%20SKU%20tier%20%24CurrentSKU.%22%0A%0A%20%20%20%20return%0A%0A%7D%0A%0Aelse%0A%0A%7B%0A%0A%20%20%20%20try%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%23%20updating%20the%20existing%20SKU%20value%20with%20the%20new%20one%0A%0A%20%20%20%20%20%20%20%20Update-AzMySqlServer%20-Name%20%22%24(%24ServerName)%22%20-ResourceGroupName%20%20%22%24(%24ResourceGroupName)%22%20-sku%20%22%24(%24SkuTier)%22%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20catch%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20Write-Error%20-Message%20%24_.Exception.Message%0A%0A%20%20%20%20%20%20%20%20throw%20%24_.Exception%0A%0A%20%20%20%20%7D%20%20%20%20%0A%0A%7D%0A%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3COL%20start%3D%225%22%3E%0A%3CLI%3E%3CBR%20%2F%3ESave%20and%20Test%20by%20entering%20the%20required%20parameters%20as%20mentioned%20below.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_3-1596279705852.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209526i8055DE74498CDBA5%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_3-1596279705852.png%22%20alt%3D%22GuptaNitin_3-1596279705852.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%226%22%3E%0A%3CLI%3ENow%20your%20code%20is%20ready%20to%20publish%20and%20linked%20with%20MySQL%20Alerts.%0A%3COL%3E%0A%3CLI%3EGo%20To%20MySQL%20resource%20and%20create%20a%20new%20alert%20rule%3C%2FLI%3E%0A%3CLI%3EChoose%20the%20appropriate%20signal%20type%20(In%20my%20case%20I%20have%20selected%20CPU%20percent%20and%20defined%20the%20threshold%20value%20to%2070%25)%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22464%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_4-1596279705859.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209527i002C001898F97797%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_4-1596279705859.png%22%20alt%3D%22GuptaNitin_4-1596279705859.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22464%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_5-1596279705871.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209528i080FC3EA743E7354%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_5-1596279705871.png%22%20alt%3D%22GuptaNitin_5-1596279705871.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22464%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_6-1596279705877.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209530i53199637779B4C36%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_6-1596279705877.png%22%20alt%3D%22GuptaNitin_6-1596279705877.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%227%22%3E%0A%3CLI%3ECreate%20Action%20Group%20and%20select%20the%20user-defined%20Automation%20runbook%20and%20select%20the%20one%20which%20we%20created%20above%20from%20the%20runbook%20list.%0A%3COL%3E%0A%3CLI%3EPlease%20specify%20the%20required%20parameter%20in%20the%20parameter%20selection%20and%20complete.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20width%3D%22704%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22704%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_7-1596279705885.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209531iB037F533D0836EA5%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_7-1596279705885.png%22%20alt%3D%22GuptaNitin_7-1596279705885.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22704%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_8-1596279705890.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209529iA2B7956552304ED2%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_8-1596279705890.png%22%20alt%3D%22GuptaNitin_8-1596279705890.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_9-1596279705893.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209532i9CCE7065F68B5B17%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_9-1596279705893.png%22%20alt%3D%22GuptaNitin_9-1596279705893.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%228%22%3E%0A%3CLI%3ELastly%2C%20specify%20the%20alert%20description%20and%20severity%20which%20will%20help%20to%20identify%20when%20this%20alert%20got%20triggered%20and%20action%20performed.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20width%3D%22734%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22734%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_10-1596279705901.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209533iDB1D323325AAFA2D%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_10-1596279705901.png%22%20alt%3D%22GuptaNitin_10-1596279705901.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22734%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GuptaNitin_11-1596279705917.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F209534i09474D58819BF9CB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22GuptaNitin_11-1596279705917.png%22%20alt%3D%22GuptaNitin_11-1596279705917.png%22%20%2F%3E%3C%2FSPAN%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EConclusion%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20above%20solution%20will%20help%20to%20identify%20the%20CPU%20spike%5Cthreshold%20and%20take%20appropriate%20action%20based%20on%20the%20steps%20defined%20into%20the%20Azure%20Automation%20runbook.%20Also%2C%20help%20to%20track%20the%20changes%20as%20part%20of%20the%20alert%20for%20auditing%20purpose.%20This%20solution%20helps%20to%20reduce%20the%20upfront%20cost%20(which%20occurs%20by%20procuring%20the%20tier%20tiers%5Csku%E2%80%99s%20resources)%26nbsp%3B%20and%20take%20proactive%20action%20when%20identifying%20the%20under-utilized%20or%20high-utilized%20CPU%3B%20accordingly%2C%20Scale%20up%5Cdown%20based%20on%20the%20alert%20configuration.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1560224%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Auto Scale Azure Database for MySQL Using Azure Automation & PowerShell

 

Identify the load and scaling up or downgrading your compute capacity is critical for business and to achieve it requires manual intervention & downtime. On the other hand, continuous efforts\resources needed to monitor the environment. In the managed database scenario, most of the services already offering the auto scale\down capability in-built. In the case of the Azure Database for MySQL, we can configure the autoscale capability from the storage front however compute require manual intervention to achieve the same by increasing the SKU tiers or automate the whole process.

 

To simplify the process, I’m using the Azure Automation account, Automation Runbook, and PowerShell, explained the whole process step by step below.

 

Steps:

  1. Create Azure Automate Account from the Azure portal.

 

GuptaNitin_0-1596279705828.png

 

 

  1. As we’re accessing a couple of Azure PowerShell modules to access Azure accounts (AZ. Accounts) and MySQL (AZ. MySQL) resources, by default those modules are not incorporated hence will have to import it directly from the Module gallery.

 

GuptaNitin_1-1596279705835.png

 

 

  1. In the next step, we require Runbook where will have to define the flow or script down the complete process for Scale up\down (To ease I have created the two different rulebooks for both Scales up & down).

While creating, have to choose the mode which you want to leverage (In my case I have selected PowerShell, however you’re open to select Python as well).

 

GuptaNitin_2-1596279705839.png

 

 

 

  1. Now ready to show your coding skill, however, I included the basic code into the runbook console for changing your SKU values or Scaling up.
########################################################

# Parameters

########################################################

[CmdletBinding()]

param(

    [Parameter(Mandatory=$True,Position=0)]

    [ValidateLength(1,100)]

    [string]$ResourceGroupName,



    [Parameter(Mandatory=$True,Position=1)]

    [ValidateLength(1,100)]

    [string]$ServerName,

     

    [Parameter(Mandatory=$False,Position=2)]

    [ValidateLength(1,100)]

    [string]$SkuTier

)



# Track the execution date & time

$StartDate=(GET-DATE)



########################################################

# Log-in to Azure with AZ (standard code)

########################################################

Write-Verbose -Message 'Connecting to Azure'

  

# Name of the Azure Run As a connection

$ConnectionName = 'AzureRunAsConnection'

try

{

    # Get the connection properties to connect Azure resources

    $ServicePrincipalConnection = Get-AutomationConnection -Name $ConnectionName      

   

    'Log in to Azure...'

    $null = Connect-AzAccount `

        -ServicePrincipal `

        -TenantId $ServicePrincipalConnection.TenantId `

        -ApplicationId $ServicePrincipalConnection.ApplicationId `

        -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint 

}

catch 

{

    if (!$ServicePrincipalConnection)

    {

        #Missed to 'Create Azure Run As account' 

        $ErrorMessage = "Connection $ConnectionName not found."

        throw $ErrorMessage

    }

    else

    {

        # additional execption

        Write-Error -Message $_.Exception.Message

        throw $_.Exception

    }

}



########################################################

# Setting SKU Tier for MySQLDatabase

########################################################

$CurrentSKU = (Get-AzMySqlServer -Name $ServerName -ResourceGroupName $ResourceGroupName).skuname



If($CurrentSKU -eq $SkuTier)

{

    # Validating the existing SKU value

    Write-Error "Cannot change pricing tier of $ServerName because the new SKU $SkuTier tier is equal to current SKU tier $CurrentSKU."

    return

}

else

{

    try

    {

        # updating the existing SKU value with the new one

        Update-AzMySqlServer -Name "$($ServerName)" -ResourceGroupName  "$($ResourceGroupName)" -sku "$($SkuTier)"

    }

    catch

    {

        Write-Error -Message $_.Exception.Message

        throw $_.Exception

    }    

}



  1. Save and Test by entering the required parameters as mentioned below.

 

GuptaNitin_3-1596279705852.png

 

 

  1. Now your code is ready to publish and linked with MySQL Alerts.
    1. Go To MySQL resource and create a new alert rule
    2. Choose the appropriate signal type (In my case I have selected CPU percent and defined the threshold value to 70%)

 

GuptaNitin_4-1596279705859.png

 

 

GuptaNitin_5-1596279705871.png

 

 

GuptaNitin_6-1596279705877.png

 

 

 

  1. Create Action Group and select the user-defined Automation runbook and select the one which we created above from the runbook list.
    1. Please specify the required parameter in the parameter selection and complete.

 

GuptaNitin_7-1596279705885.png

 

 

GuptaNitin_8-1596279705890.png

 

 

GuptaNitin_9-1596279705893.png

 

 

  1. Lastly, specify the alert description and severity which will help to identify when this alert got triggered and action performed.

 

GuptaNitin_10-1596279705901.png

 

 

GuptaNitin_11-1596279705917.png

 

 

Conclusion

The above solution will help to identify the CPU spike\threshold and take appropriate action based on the steps defined into the Azure Automation runbook. Also, help to track the changes as part of the alert for auditing purpose. This solution helps to reduce the upfront cost (which occurs by procuring the tier tiers\sku’s resources)  and take proactive action when identifying the under-utilized or high-utilized CPU; accordingly, Scale up\down based on the alert configuration.

 

 

0 Replies