Python Script to automate pause-resume of SQL Dedicated Pools in Azure Automation Account with…

Python Script to automate pause-resume of SQL Dedicated Pools in Azure Automation Account with Managed Identity In this post i’ll present you two simple scripts to automate an Azure SQL Dedicated Pool pause and resume through a Automatin Ac

Source comparison and review notes
  • Automated pair-review warning (AI bilingual content review): The English source includes full pause and resume Python code blocks that are not present in the Spanish published body; preserve both originals rather than assuming textual identity.

Python Script to automate pause-resume of SQL Dedicated Pools in Azure Automation Account with Managed Identity

In this post i’ll present you two simple scripts to automate an Azure SQL Dedicated Pool pause and resume through a Automatin Account Runbook.

A standout feature of these scripts is their utilization of Azure Managed Identity. The Managed System Identity is enabled at the Automation Account level with a unique identity, allowing them to connect seamlessly to resources that support Azure AD authentication. This ensures that you can authenticate to Azure services without having the hassle of embedding credentials in your code.

Repository: stop-resume-azure-sql-dedicated-pool-py

Pause script

Python
from azure.identity import ManagedIdentityCredential
from azure.mgmt.sql import SqlManagementClient
from azure.core.polling import LROPoller
resource_group_name = ''
server_name = ''
database_name = ''
subscription_id = ''
credentials = ManagedIdentityCredential()
sql_client = SqlManagementClient(credentials, subscription_id)
def pause_sql_pool(resource_group_name, server_name, database_name):
poller = sql_client.databases.begin_pause(resource_group_name, server_name, database_name)
# Wait for the operation to complete
poller.result()
try:
database = sql_client.databases.get(resource_group_name, server_name, database_name)
if database.status == 'Online':
pause_sql_pool(resource_group_name, server_name, database_name)
else:
print(f"Database status: {database.status}")
except Exception as e:
print(f"An error occurred: {e}")

Resume script

Python
from azure.identity import ManagedIdentityCredential
from azure.mgmt.sql import SqlManagementClient
from azure.core.polling import LROPoller
resource_group_name = ''
server_name = ''
database_name = ''
subscription_id = ''
credentials = ManagedIdentityCredential()
sql_client = SqlManagementClient(credentials, subscription_id)
def resume_sql_pool(resource_group_name, server_name, database_name):
poller = sql_client.databases.begin_resume(resource_group_name, server_name, database_name)
# Wait for the operation to complete
poller.result()
try:
database = sql_client.databases.get(resource_group_name, server_name, database_name)
if database.status == 'Paused':
resume_sql_pool(resource_group_name, server_name, database_name)
else:
print(f"Database status: {database.status}")
except Exception as e:
print(f"An error occurred: {e}")

Dependences

Install the following python packages:

  • azure_core.
  • azure_identity.
  • azure_mgmt_core.
  • azure_mgmt_sql.

Enable Manage Identity in your Automatin Account

Go to your Azure Automation Account and enable the system-managed identity:

Assign the necessary permissions

The managed identity would typically need the Microsoft.Sql/servers/databases/pause/action and Microsoft.Sql/servers/databases/resume/action permissions in Azure RBAC (Role-Based Access Control). You could assign the following permission (from the most to the least restrictive):

  • SQL DB Contributor: This role allows for management of SQL DBs, but not their security-related policies. Given its broad range of permissions, it will include the ability to pause and resume.
  • SQL Server Contributor: This is a more comprehensive role than the SQL DB Contributor, allowing management of SQL servers and their databases, but not their security-related policies. Again, due to its broad permissions, it would encompass the ability to pause and resume.
  • Contributor: This is also a very broad role that provides full management rights to the assigned resource except for changing permissions. This would also encompass the ability to pause and resume.
  • Owner: This is a very broad role that provides full management rights to the assigned resource, which means it can manage everything including pausing and resuming of databases.

Create Runbook

  1. Select Your Automation Account. If you already have an Automation Account created, click on its name. If not, you’ll need to create one first.
  2. Runbooks Panel: Within your Automation Account, under the section “Process Automation”, click on “Runbooks”.
  3. Create a New Runbook: At the top of the “Runbooks” pane, click the “+ Add a runbook” button.
  4. Runbook Details:
  • Name: Enter a name for your runbook.
  • Language: Python.
  • Python runtime: 3.8
  • Description (optional): Provide a brief description of the runbook’s purpose or functionality.

5. Create: Click the “Create” button. After a few moments, your runbook will be created and you’ll be directed to the runbook editor.

Upload and test the Python Code:

Upload your code and complete the variables input in lines 5 to 8:

  1. Test the Runbook: Before publishing, it’s always a good idea to test the runbook to ensure it works as expected. Click on “Test pane” to test your runbook in a non-production environment.

The script uses the Long Runing processes pool in python, which enables the script to finish only if the process of pausing or resuming completes succesfully.

  1. Save and publish the Runbook: Once you’ve tested and are satisfied with your runbook, click on the “Publish” button to save the runbook and make it available for triggers, schedules, or manual starts.

Then you can test it in the runbook section

  1. Run the Runbook: Back in the main pane for your runbook, you can click on “Start” to manually execute it.
  2. View Jobs: In the left pane, under “Jobs”, you can monitor the status and view the output of runbook executions.

Thanks for reading. The learning continues in the next article.