Auto start/stop Flexible Server using Azure Automation Python RunBook

Published Dec 01 2020 04:56 PM 1,967 Views
Microsoft

Flexible Server is a new deployment option for Azure Database for PostgreSQL that gives you the control you need with multiple configuration parameters for fine-grained database tuning along with a simpler developer experience to accelerate end-to-end deployment. With Flexible Server, you will also have a new way to optimize cost with stop/start capabilities. The ability to stop/start the Flexible Server when needed is ideal for development or test scenarios where it’s not necessary to run your database 24x7. When Flexible Server is stopped, you only pay for storage, and you can easily start it back up with just a click in the Azure portal.

 

Azure Automation delivers a cloud-based automation and configuration service that supports consistent management across your Azure and non-Azure environments. It comprises process automation, configuration management, update management, shared capabilities, and heterogeneous features. Automation gives you complete control during deployment, operations, and decommissioning of workloads and resources. The Azure Automation Process Automation feature supports several types of runbooks such as Graphical, PowerShell, Python. Other options for automation include PowerShell runbook, Azure Functions timer trigger, Azure Logic Apps. Here is a guide to choose the right integration and automation services in Azure.

 

Runbooks support storing, editing, and testing the scripts in the portal directly. Python is a general-purpose, versatile, and popular programming language. In this blog, we will see how we can leverage Azure Automation Python runbook to auto start/stop a Flexible Server on weekend days (Saturdays and Sundays).

 

Prerequisites

 

Steps

1. Create a new Azure Automation account with Azure Run As account at:

https://ms.portal.azure.com/#create/Microsoft.AutomationAccount

 

NOTE: An Azure Run As Account by default has the Contributor role to your entire subscription. You can limit Run As account permissions if required. Also, all users with access to the Automation Account can also use this Azure Run As Account.

 

b1.png

 

2. After you successfully create the Azure Automation account, navigate to Runbooks.

Here you can already see some sample runbooks.

 

b2.png

 

3. Let’s create a new python runbook by selecting+ Create a runbook.

 

4. Provide the runbook details, and then select Create.

 

b3.png

 

After the python runbook is created successfully, an Edit screen appears, similar to the image below.

 

b4.png

 

5. Copy paste the below python script. Fill in appropriate values for your Flexible Server’s subscription_id, resource_group, and server_name, and then select Save.

 

 

import azure.mgmt.resource
import requests
import automationassets
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD
from datetime import datetime

def get_token(runas_connection, resource_url, authority_url):
    """ Returns credentials to authenticate against Azure resoruce manager """
    from OpenSSL import crypto
    from msrestazure import azure_active_directory
    import adal

    # Get the Azure Automation RunAs service principal certificate
    cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
    pks12_cert = crypto.load_pkcs12(cert)
    pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM, pks12_cert.get_privatekey())

    # Get run as connection information for the Azure Automation service principal
    application_id = runas_connection["ApplicationId"]
    thumbprint = runas_connection["CertificateThumbprint"]
    tenant_id = runas_connection["TenantId"]

    # Authenticate with service principal certificate
    authority_full_url = (authority_url + '/' + tenant_id)
    context = adal.AuthenticationContext(authority_full_url)
    return context.acquire_token_with_client_certificate(
            resource_url,
            application_id,
            pem_pkey,
            thumbprint)['accessToken']

action = ''
day_of_week = datetime.today().strftime('%A')
if day_of_week == 'Saturday':
    action = 'stop'
elif day_of_week == 'Monday':
    action = 'start'

subscription_id = '<SUBSCRIPTION_ID>'
resource_group = '<RESOURCE_GROUP>'
server_name = '<SERVER_NAME>'

if action: 
    print 'Today is ' + day_of_week + '. Executing ' + action + ' server'
    runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
    resource_url = AZURE_PUBLIC_CLOUD.endpoints.active_directory_resource_id
    authority_url = AZURE_PUBLIC_CLOUD.endpoints.active_directory
    resourceManager_url = AZURE_PUBLIC_CLOUD.endpoints.resource_manager
    auth_token=get_token(runas_connection, resource_url, authority_url)
    url = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group + '/providers/Microsoft.DBforPostgreSQL/flexibleServers/' + server_name + '/' + action + '?api-version=2020-02-14-preview'
    response = requests.post(url, json={}, headers={'Authorization': 'Bearer ' + auth_token})
    print(response.json())
else: 
    print 'Today is ' + day_of_week + '. No action taken'

 

 

After you save this, you can test the python script using “Test Pane”. When the script works fine, then select Publish.

 

Next, we need to schedule this runbook to run every day using Schedules.

 

6. On the runbook Overview blade, select Link to schedule.

 

b5.png

 

7. Select Link a schedule to your runbook.

 

b6.png

 

8. Select Create a new schedule.

 

b7.png

 

9. Create a schedule to run every day at 12:00 AM using the following parameters

 

b8.png

 

10. Select Create and verify that the schedule has been successfully created and verify that the Status is “On“.

 

b9.png

 

After following these steps, Azure Automation will run the Python runbook every day at 12:00 AM. The python script will stop the Flexible Server if it’s a Saturday and start the server if it’s a Monday. This is all based on the UTC time zone, but you can easily modify it to fit the time zone of your choice. You can also use the holidays Python package to auto start/stop Flexible Server during the holidays.

 

If you want to dive deeper, the new Flexible Server documentation is a great place to find out more. You can also visit our website to learn more about our Azure Database for PostgreSQL managed service. We’re always eager to hear your feedback, so please reach out via email using the Ask Azure DB for PostgreSQL alias.

 

2 Comments
Occasional Visitor

That's really great. 
We started to think about switching from Single Server to Flexible.

 

What is not clear:  what means "Preview" state? It's not ready for production yet?

If  so  - when it will be ready?

 

Sorry if I'm asking in inappropriate place - can you tell me where and whom to ask?

 

Microsoft
%3CLINGO-SUB%20id%3D%22lingo-sub-1946701%22%20slang%3D%22en-US%22%3EAuto%20start%2Fstop%20Flexible%20Server%20using%20Azure%20Automation%20Python%20RunBook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1946701%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fintroducing-flexible-server-for-azure-database-for-postgresql%2Fba-p%2F1686616%22%20target%3D%22_blank%22%3EFlexible%20Server%3C%2FA%3E%20is%20a%20new%20deployment%20option%20for%20Azure%20Database%20for%20PostgreSQL%20that%20gives%20you%20the%20control%20you%20need%20with%20multiple%20configuration%20parameters%20for%20fine-grained%20database%20tuning%20along%20with%20a%20simpler%20developer%20experience%20to%20accelerate%20end-to-end%20deployment.%20With%20Flexible%20Server%2C%20you%20will%20also%20have%20a%20new%20way%20to%20optimize%20cost%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fpostgresql%2Fflexible-server%2Fhow-to-stop-start-server-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Estop%2Fstart%20capabilities%3C%2FA%3E.%20The%20ability%20to%20stop%2Fstart%20the%20Flexible%20Server%20when%20needed%20is%20ideal%20for%20development%20or%20test%20scenarios%20where%20it%E2%80%99s%20not%20necessary%20to%20run%20your%20database%2024x7.%20When%20Flexible%20Server%20is%20stopped%2C%20you%20only%20pay%20for%20storage%2C%20and%20you%20can%20easily%20start%20it%20back%20up%20with%20just%20a%20click%20in%20the%20Azure%20portal.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-intro%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Automation%3C%2FA%3E%20delivers%20a%20cloud-based%20automation%20and%20configuration%20service%20that%20supports%20consistent%20management%20across%20your%20Azure%20and%20non-Azure%20environments.%20It%20comprises%20process%20automation%2C%20configuration%20management%2C%20update%20management%2C%20shared%20capabilities%2C%20and%20heterogeneous%20features.%20Automation%20gives%20you%20complete%20control%20during%20deployment%2C%20operations%2C%20and%20decommissioning%20of%20workloads%20and%20resources.%20The%20Azure%20Automation%20Process%20Automation%20feature%20supports%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-runbook-types%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eseveral%20types%20of%20runbooks%3C%2FA%3E%20such%20as%20Graphical%2C%20PowerShell%2C%20Python.%20Other%20options%20for%20automation%20include%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Flearn%2Fautomation-tutorial-runbook-textual-powershell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPowerShell%20runbook%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-bindings-timer%3Ftabs%3Dcsharp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Functions%20timer%20trigger%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Flogic-apps%2Ftutorial-build-schedule-recurring-logic-app-workflow%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Logic%20Apps%3C%2FA%3E.%20Here%20is%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-compare-logic-apps-ms-flow-webjobs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eguide%3C%2FA%3E%20to%20choose%20the%20right%20integration%20and%20automation%20services%20in%20Azure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunbooks%20support%20storing%2C%20editing%2C%20and%20testing%20the%20scripts%20in%20the%20portal%20directly.%20Python%20is%20a%20general-purpose%2C%20versatile%2C%20and%20popular%20programming%20language.%20In%20this%20blog%2C%20we%20will%20see%20how%20we%20can%20leverage%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Flearn%2Fautomation-tutorial-runbook-textual-python2%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Automation%20Python%20runbook%3C%2FA%3E%20to%20auto%20start%2Fstop%20a%20Flexible%20Server%20on%20weekend%20days%20(Saturdays%20and%20Sundays).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--482488976%22%20id%3D%22toc-hId--482488976%22%20id%3D%22toc-hId--482488976%22%20id%3D%22toc-hId--482488976%22%20id%3D%22toc-hId--482488976%22%20id%3D%22toc-hId--482488976%22%3EPrerequisites%3C%2FH4%3E%0A%3CUL%3E%0A%3CLI%3EAzure%20subscription.%20If%20you%20don't%20have%20one%20yet%2C%20you%20can%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fpricing%2Fmember-offers%2Fmsdn-benefits-details%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eactivate%20your%20MSDN%20subscriber%20benefits%3C%2FA%3E%20or%20sign%20up%20for%20a%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Ffree%2F%3FWT.mc_id%3DA261C142F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Efree%20account%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%3EAzure%20Database%20for%20PostgreSQL%20-%20Flexible%20Server.%20If%20you%20don't%20have%20one%20yet%2C%20you%20can%20create%20one%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fflexible-server%2Fquickstart-create-server-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH4%20id%3D%22toc-hId-2005023857%22%20id%3D%22toc-hId-2005023857%22%20id%3D%22toc-hId-2005023857%22%20id%3D%22toc-hId-2005023857%22%20id%3D%22toc-hId-2005023857%22%20id%3D%22toc-hId-2005023857%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId-197569394%22%20id%3D%22toc-hId-197569394%22%20id%3D%22toc-hId-197569394%22%20id%3D%22toc-hId-197569394%22%20id%3D%22toc-hId-197569394%22%20id%3D%22toc-hId-197569394%22%3ESteps%3C%2FH4%3E%0A%3CP%3E1.%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-quickstart-create-account%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20a%20new%20Azure%20Automation%20account%3C%2FA%3E%20with%20Azure%20Run%20As%20account%20at%3A%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fms.portal.azure.com%2F%23create%2FMicrosoft.AutomationAccount%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fms.portal.azure.com%2F%23create%2FMicrosoft.AutomationAccount%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3E%20An%20Azure%20Run%20As%20Account%20by%20default%20has%20the%20Contributor%20role%20to%20your%20entire%20subscription.%20You%20can%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fmanage-runas-account%23limit-run-as-account-permissions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Elimit%20Run%20As%20account%20permissions%3C%2FA%3E%20if%20required.%20Also%2C%20all%20users%20with%20access%20to%20the%20Automation%20Account%20can%20also%20use%20this%20Azure%20Run%20As%20Account.%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%22b1.png%22%20style%3D%22width%3A%20333px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237101iB57785622305C24A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b1.png%22%20alt%3D%22b1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorramkychan_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E2.%20After%20you%20successfully%20create%20the%20Azure%20Automation%20account%2C%20navigate%20to%20%3CSTRONG%3ERunbooks%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EHere%20you%20can%20already%20see%20some%20sample%20runbooks.%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%22b2.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237103iCA25E7C9B991F58E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b2.png%22%20alt%3D%22b2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E3.%20Let%E2%80%99s%20create%20a%20new%20python%20runbook%20by%20selecting%3CSTRONG%3E%2B%20Create%20a%20runbook%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.%20Provide%20the%20runbook%20details%2C%20and%20then%20select%20%3CSTRONG%3ECreate%3C%2FSTRONG%3E.%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%22b3.png%22%20style%3D%22width%3A%20313px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237105i1536FE89A6F97A82%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b3.png%22%20alt%3D%22b3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20the%20python%20runbook%20is%20created%20successfully%2C%20an%20%3CSTRONG%3EEdit%3C%2FSTRONG%3E%20screen%20appears%2C%20similar%20to%20the%20image%20below.%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%22b4.png%22%20style%3D%22width%3A%20560px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237106i706AC35A418121C5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b4.png%22%20alt%3D%22b4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E5.%20Copy%20paste%20the%20below%20python%20script.%20Fill%20in%20appropriate%20values%20for%20your%20Flexible%20Server%E2%80%99s%20%3CEM%3Esubscription_id%3C%2FEM%3E%2C%20%3CEM%3Eresource_group%3C%2FEM%3E%2C%20and%20%3CEM%3Eserver_name%3C%2FEM%3E%2C%20and%20then%20select%20%3CSTRONG%3ESave%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Eimport%20azure.mgmt.resource%0Aimport%20requests%0Aimport%20automationassets%0Afrom%20msrestazure.azure_cloud%20import%20AZURE_PUBLIC_CLOUD%0Afrom%20datetime%20import%20datetime%0A%0Adef%20get_token(runas_connection%2C%20resource_url%2C%20authority_url)%3A%0A%20%20%20%20%22%22%22%20Returns%20credentials%20to%20authenticate%20against%20Azure%20resoruce%20manager%20%22%22%22%0A%20%20%20%20from%20OpenSSL%20import%20crypto%0A%20%20%20%20from%20msrestazure%20import%20azure_active_directory%0A%20%20%20%20import%20adal%0A%0A%20%20%20%20%23%20Get%20the%20Azure%20Automation%20RunAs%20service%20principal%20certificate%0A%20%20%20%20cert%20%3D%20automationassets.get_automation_certificate(%22AzureRunAsCertificate%22)%0A%20%20%20%20pks12_cert%20%3D%20crypto.load_pkcs12(cert)%0A%20%20%20%20pem_pkey%20%3D%20crypto.dump_privatekey(crypto.FILETYPE_PEM%2C%20pks12_cert.get_privatekey())%0A%0A%20%20%20%20%23%20Get%20run%20as%20connection%20information%20for%20the%20Azure%20Automation%20service%20principal%0A%20%20%20%20application_id%20%3D%20runas_connection%5B%22ApplicationId%22%5D%0A%20%20%20%20thumbprint%20%3D%20runas_connection%5B%22CertificateThumbprint%22%5D%0A%20%20%20%20tenant_id%20%3D%20runas_connection%5B%22TenantId%22%5D%0A%0A%20%20%20%20%23%20Authenticate%20with%20service%20principal%20certificate%0A%20%20%20%20authority_full_url%20%3D%20(authority_url%20%2B%20'%2F'%20%2B%20tenant_id)%0A%20%20%20%20context%20%3D%20adal.AuthenticationContext(authority_full_url)%0A%20%20%20%20return%20context.acquire_token_with_client_certificate(%0A%20%20%20%20%20%20%20%20%20%20%20%20resource_url%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20application_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pem_pkey%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20thumbprint)%5B'accessToken'%5D%0A%0Aaction%20%3D%20''%0Aday_of_week%20%3D%20datetime.today().strftime('%25A')%0Aif%20day_of_week%20%3D%3D%20'Saturday'%3A%0A%20%20%20%20action%20%3D%20'stop'%0Aelif%20day_of_week%20%3D%3D%20'Monday'%3A%0A%20%20%20%20action%20%3D%20'start'%0A%0Asubscription_id%20%3D%20'%3CSUBSCRIPTION_ID%3E'%0Aresource_group%20%3D%20'%3CRESOURCE_GROUP%3E'%0Aserver_name%20%3D%20'%3CSERVER_NAME%3E'%0A%0Aif%20action%3A%20%0A%20%20%20%20print%20'Today%20is%20'%20%2B%20day_of_week%20%2B%20'.%20Executing%20'%20%2B%20action%20%2B%20'%20server'%0A%20%20%20%20runas_connection%20%3D%20automationassets.get_automation_connection(%22AzureRunAsConnection%22)%0A%20%20%20%20resource_url%20%3D%20AZURE_PUBLIC_CLOUD.endpoints.active_directory_resource_id%0A%20%20%20%20authority_url%20%3D%20AZURE_PUBLIC_CLOUD.endpoints.active_directory%0A%20%20%20%20resourceManager_url%20%3D%20AZURE_PUBLIC_CLOUD.endpoints.resource_manager%0A%20%20%20%20auth_token%3Dget_token(runas_connection%2C%20resource_url%2C%20authority_url)%0A%20%20%20%20url%20%3D%20'https%3A%2F%2Fmanagement.azure.com%2Fsubscriptions%2F'%20%2B%20subscription_id%20%2B%20'%2FresourceGroups%2F'%20%2B%20resource_group%20%2B%20'%2Fproviders%2FMicrosoft.DBforPostgreSQL%2FflexibleServers%2F'%20%2B%20server_name%20%2B%20'%2F'%20%2B%20action%20%2B%20'%3Fapi-version%3D2020-02-14-preview'%0A%20%20%20%20response%20%3D%20requests.post(url%2C%20json%3D%7B%7D%2C%20headers%3D%7B'Authorization'%3A%20'Bearer%20'%20%2B%20auth_token%7D)%0A%20%20%20%20print(response.json())%0Aelse%3A%20%0A%20%20%20%20print%20'Today%20is%20'%20%2B%20day_of_week%20%2B%20'.%20No%20action%20taken'%0A%3C%2FSERVER_NAME%3E%3C%2FRESOURCE_GROUP%3E%3C%2FSUBSCRIPTION_ID%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20you%20save%20this%2C%20you%20can%20test%20the%20python%20script%20using%20%E2%80%9CTest%20Pane%E2%80%9D.%20When%20the%20script%20works%20fine%2C%20then%20select%20Publish.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20we%20need%20to%20schedule%20this%20runbook%20to%20run%20every%20day%20using%20Schedules.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E6.%20On%20the%20runbook%20%3CSTRONG%3EO%3C%2FSTRONG%3E%3CSTRONG%3Everview%3C%2FSTRONG%3E%20blade%2C%20select%20%3CSTRONG%3ELink%20to%20schedule%3C%2FSTRONG%3E.%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%22b5.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237108i8C7BCE88239F6E15%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b5.png%22%20alt%3D%22b5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E7.%20Select%20%3CSTRONG%3ELink%20a%20schedule%20to%20your%20runbook%3C%2FSTRONG%3E.%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%22b6.png%22%20style%3D%22width%3A%20350px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237109iD4E1277A0DD34E65%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b6.png%22%20alt%3D%22b6.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E8.%20Select%20%3CSTRONG%3ECreate%20a%20new%20schedule%3C%2FSTRONG%3E.%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%22b7.png%22%20style%3D%22width%3A%20318px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237111i3B059F2AE109EE1B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b7.png%22%20alt%3D%22b7.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E9.%20Create%20a%20schedule%20to%20run%20every%20day%20at%2012%3A00%20AM%20using%20the%20following%20parameters%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%22b8.png%22%20style%3D%22width%3A%20316px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237124i11A4EACD61B3A2E0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b8.png%22%20alt%3D%22b8.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorramkychan_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E10.%20Select%20%3CSTRONG%3ECreate%20and%20verify%20that%20the%20schedule%20has%20been%20successfully%20created%3C%2FSTRONG%3E%20and%20verify%20that%20the%20%3CSTRONG%3EStatus%3C%2FSTRONG%3E%20is%20%E2%80%9COn%E2%80%9C.%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%22b9.png%22%20style%3D%22width%3A%20605px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237113i0EB5DC6DF38AC9BA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b9.png%22%20alt%3D%22b9.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20following%20these%20steps%2C%20Azure%20Automation%20will%20run%20the%20Python%20runbook%20every%20day%20at%2012%3A00%20AM.%20The%20python%20script%20will%20stop%20the%20Flexible%20Server%20if%20it%E2%80%99s%20a%20Saturday%20and%20start%20the%20server%20if%20it%E2%80%99s%20a%20Monday.%20This%20is%20all%20based%20on%20the%20UTC%20time%20zone%2C%20but%20you%20can%20easily%20modify%20it%20to%20fit%20the%20time%20zone%20of%20your%20choice.%20You%20can%20also%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fpypi.org%2Fproject%2Fholidays%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eholidays%3C%2FA%3E%20Python%20package%20to%20auto%20start%2Fstop%20Flexible%20Server%20during%20the%20holidays.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20dive%20deeper%2C%20the%20new%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fflexible-server%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EFlexible%20Server%20documentation%3C%2FA%3E%20is%20a%20great%20place%20to%20find%20out%20more.%20You%20can%20also%20visit%20our%20website%20to%20learn%20more%20about%20our%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fpostgresql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%26nbsp%3Bmanaged%20service%3C%2FA%3E.%20We%E2%80%99re%20always%20eager%20to%20hear%20your%20feedback%2C%20so%20please%20reach%20out%20via%20email%20using%20the%20%3CA%20href%3D%22mailto%3AAskAzureDBforPostgreSQL%40service.microsoft.com%3Fsubject%3DWhat-is-Flexible-Server-in-Azure-Database-for-PostgreSQL%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAsk%20Azure%20DB%20for%20PostgreSQL%3C%2FA%3E%20alias.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1946701%22%20slang%3D%22en-US%22%3E%3CP%3EAuto%20start%2Fstop%20Flexible%20Server%20using%20Azure%20Automation%20Python%20RunBook%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22b10.png%22%20style%3D%22width%3A%20908px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237114iE1194BF8EB6CF3B3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22b10.png%22%20alt%3D%22b10.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1946701%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAutomation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECost%20Management%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECost%20Optimization%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eflexible%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgres%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPostgreSQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epython%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Erunbook%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EServer%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EStart%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EStop%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2120036%22%20slang%3D%22en-US%22%3ERe%3A%20Auto%20start%2Fstop%20Flexible%20Server%20using%20Azure%20Automation%20Python%20RunBook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2120036%22%20slang%3D%22en-US%22%3E%3CP%3EThat's%20really%20great.%26nbsp%3B%3CBR%20%2F%3EWe%20started%20to%20think%20about%20switching%20from%20Single%20Server%20to%20Flexible.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20is%20not%20clear%3A%26nbsp%3B%20what%20means%20%22Preview%22%20state%3F%26nbsp%3BIt's%20not%20ready%20for%20production%20yet%3F%3C%2FP%3E%3CP%3EIf%26nbsp%3B%20so%26nbsp%3B%20-%20when%20it%20will%20be%20ready%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESorry%20if%20I'm%20asking%20in%20inappropriate%20place%20-%20can%20you%20tell%20me%20where%20and%20whom%20to%20ask%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2147695%22%20slang%3D%22en-US%22%3ERe%3A%20Auto%20start%2Fstop%20Flexible%20Server%20using%20Azure%20Automation%20Python%20RunBook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2147695%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F961984%22%20target%3D%22_blank%22%3E%40Alexander_Yuryshev%3C%2FA%3E%26nbsp%3B%3A%20Please%20email%26nbsp%3B%3CA%20href%3D%22mailto%3AAskAzureDBforPostgreSQL%40service.microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAskAzureDBforPostgreSQL%40service.microsoft.com%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Dec 01 2020 05:29 PM
Updated by: