{"id":1053,"date":"2025-02-05T11:07:21","date_gmt":"2025-02-05T17:07:21","guid":{"rendered":"https:\/\/www.sqlfreelancer.com\/blog\/?p=1053"},"modified":"2025-02-05T11:25:35","modified_gmt":"2025-02-05T17:25:35","slug":"automating-azure-sql-database-scaling-to-save-costs","status":"publish","type":"post","link":"https:\/\/www.sqlfreelancer.com\/blog\/automating-azure-sql-database-scaling-to-save-costs\/","title":{"rendered":"Automating Azure SQL Database Scaling to Save Costs"},"content":{"rendered":"<p>Managing cloud costs efficiently is crucial, especially when dealing with Azure SQL Databases that may not need their full performance capacity 24\/7. By automating the process of scaling down at night and scaling up in the morning, you can optimize costs while maintaining performance when needed.<\/p>\n<p>In this post, I&#8217;ll show you how to use <strong>Azure Automation<\/strong> and <strong>PowerShell<\/strong> to automatically scale your Azure SQL Database.<\/p>\n<p><strong>Step 1: Create an Azure Automation Account<\/strong><\/p>\n<ul>\n<li>Go to the Azure Portal.<\/li>\n<li>Navigate to Automation Accounts and click Create.<\/li>\n<li>Provide a name, select your subscription, resource group (bradyupton_rg), and region.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1054 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account.png\" alt=\"\" width=\"936\" height=\"572\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-300x183.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-768x469.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<ul>\n<li>Ensure that System Assigned Managed Identity is enabled (this will allow the automation account to interact with Azure resources).<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1056\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-2.png\" alt=\"\" width=\"936\" height=\"502\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-2.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-2-300x161.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Automation-Account-2-768x412.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<p><strong>Step 2: Assign Permissions to the Automation Account<\/strong><\/p>\n<ul>\n<li>Go to Azure SQL Server (bradyupton).<\/li>\n<li>Navigate to Access Control (IAM) \u2192 Add Role Assignment.<\/li>\n<li style=\"text-align: left;\">Assign the Contributor or SQL Server Contributor role to your Automation Account\u2019s Managed Identity.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1058 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment.png\" alt=\"\" width=\"936\" height=\"459\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-300x147.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-768x377.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1057 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-2.png\" alt=\"\" width=\"936\" height=\"382\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-2.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-2-300x122.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Add-Role-Assignment-2-768x313.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><strong>Step 3: Create a Runbook for Scaling Down<\/strong><\/p>\n<ul>\n<li>In Azure Automation, go to Runbooks and click Create a Runbook.<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1059 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook.png\" alt=\"\" width=\"936\" height=\"444\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-300x142.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-768x364.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<ul>\n<li>Choose PowerShell as the runbook type<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1060\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-2.png\" alt=\"\" width=\"936\" height=\"423\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-2.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-2-300x136.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Create-Runbook-2-768x347.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<ul>\n<li>Add the following PowerShell script:<\/li>\n<\/ul>\n<pre>param \r\n(\r\n[string]$resourceGroupName = \"bradyupton_rg\", #Change to your Resource Group\r\n[string]$serverName = \"bradyupton\", #Change to your SQL Server\r\n[string]$databaseName = \"bradydatabase\", #Change to your database\r\n[string]$newSku = \"Basic\"\u00a0 #Change to the desired lower tier\r\n)\r\n\r\n#Connect to Azure\r\nConnect-AzAccount -Identity\r\n\r\n#Scale down the SQL Database\r\nSet-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName -DatabaseName $databaseName -Edition \"Basic\" -ComputeModel \"Provisioned\"\r\n\r\nWrite-Output \"Database scaled down to $newSku\"<\/pre>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/PowerShell.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1065 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/PowerShell.png\" alt=\"\" width=\"936\" height=\"364\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/PowerShell.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/PowerShell-300x117.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/PowerShell-768x299.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<ul>\n<li>Save and Publish the runbook.<\/li>\n<\/ul>\n<p><strong>Step 4: Schedule the Runbook<\/strong><\/p>\n<ul>\n<li>In the runbook, go to Schedules \u2192 Add a Schedule.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1066 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule.png\" alt=\"\" width=\"936\" height=\"559\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule.png 936w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-300x179.png 300w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-768x459.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<ul>\n<li>Set it to run every night at your desired time to scale down.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1067 aligncenter\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-2.png\" alt=\"\" width=\"243\" height=\"482\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-2.png 470w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2025\/02\/Runbook-Schedule-2-151x300.png 151w\" sizes=\"auto, (max-width: 243px) 100vw, 243px\" \/><\/a><\/p>\n<ul>\n<li>(Optional) Create a second runbook to scale up in the morning with a similar script but with a higher-tier SKU (e.g., S3).<\/li>\n<\/ul>\n<p>Example for scaling up:<\/p>\n<pre>param \r\n(\r\n[string]$resourceGroupName = \"bradyupton_rg\",\r\n[string]$serverName = \"bradyupton\",\r\n[string]$databaseName = \"bradydatabase\",\r\n[string]$newSku = \"S3\"\u00a0 # Higher-tier SKU for peak hours\r\n)\r\n\r\n#Connect to Azure\r\nConnect-AzAccount -Identity\r\n\r\n#Scale up the SQL Database\r\nSet-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $serverName -DatabaseName $databaseName -Edition \"Standard\" -RequestedServiceObjectiveName $newSku\r\n\r\nWrite-Output \"Database scaled up to $newSku\"\r\n<\/pre>\n<p><strong>Conclusion<\/strong><\/p>\n<p>By automating SQL Database scaling with Azure Automation, you can significantly reduce costs during off-peak hours while maintaining performance when needed. This approach ensures your resources are utilized efficiently without manual intervention.<\/p>\n<p>If you have any questions or need further customization, feel free to reach out!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing cloud costs efficiently is crucial, especially when dealing with Azure SQL Databases that may not need their full performance capacity 24\/7. By automating the process of scaling down at night and scaling up in the morning, you can optimize costs while maintaining performance when needed. In this post, I&#8217;ll show you how to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1069,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[116,77],"class_list":["post-1053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","tag-automation","tag-azure"],"_links":{"self":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":8,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions\/1072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/media\/1069"}],"wp:attachment":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}