Scale up and down the Compute tier/SKU on specific time using automation in Azure PostgreSQL server

Shiva Kumar Kasappa (MINDTREE LIMITED) 1 Reputation point Microsoft Vendor
2022-08-16T16:20:12.67+00:00

Scale up and down the Compute tier/SKU on specific time using automation in Azure PostgreSQL server

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,132 questions
Azure Database for PostgreSQL
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Maxim Sergeev 6,566 Reputation points Microsoft Employee
    2022-08-17T01:13:58.8+00:00

    Hi there,

    If I understand your question correctly, you want to use some automation tools for scale-in\scale-out tasks.
    In this case I would recommend using Azure Automation Account with a required imported module.

    1. Create Azure Automation Account
    2. Import Module https://learn.microsoft.com/en-us/powershell/module/az.postgresql/?view=azps-8.2.0
    3. Create a runbook that includes the logic for scale-up\scale-down. I expect you will use Get-AzPostgreSqlServer and Update-AzPostgreSqlServer
    4. Schedule it in Azure Automation Account

    I worked on a similar scenario few years ago, this is an example for SQL Databases, but you could modify it for PostgreSqlserver using

    # PowerShell code  
    ########################################################  
    # 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=$True,Position=2)]  
        [ValidateLength(1,100)]  
        [string]$DatabaseName,  
       
        [Parameter(Mandatory=$False,Position=3)]  
        [ValidateLength(1,100)]  
        [string]$Edition,  
           
        [Parameter(Mandatory=$False,Position=4)]  
        [ValidateLength(1,100)]  
        [string]$PricingTier  
    )  
       
    # Keep track of time  
    $StartDate=(GET-DATE)  
       
     ########################################################  
    # Log in to Azure with AZ (standard code)  
    ########################################################  
    Write-Verbose -Message 'Connecting to Azure'  
        
    # Name of the Azure Run As connection  
    $ConnectionName = 'AzureRunAsConnection'  
    try  
    {  
        # Get the connection properties  
        $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)  
        {  
            # You forgot to turn on 'Create Azure Run As account'   
            $ErrorMessage = "Connection $ConnectionName not found."  
            throw $ErrorMessage  
        }  
        else  
        {  
            # Something else went wrong  
            Write-Error -Message $_.Exception.Message  
            throw $_.Exception  
        }  
    }  
    ########################################################  
        
    ########################################################  
    # Getting the database for testing and logging purposes  
    ########################################################  
    $MyAzureSqlDatabase = Get-AzSqlDatabase -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName  
    if (!$MyAzureSqlDatabase)  
    {  
        Write-Error "$($ServerName)\$($DatabaseName) not found in $($ResourceGroupName)"  
        return  
    }  
    else  
    {  
        Write-Output "Current pricing tier of $($ServerName)\$($DatabaseName): $($MyAzureSqlDatabase.Edition) - $($MyAzureSqlDatabase.CurrentServiceObjectiveName)"  
    }  
       
    ########################################################  
    # Set Pricing Tier Database  
    ########################################################  
    # Check for incompatible actions  
    if ($MyAzureSqlDatabase.Edition -eq $Edition -And $MyAzureSqlDatabase.CurrentServiceObjectiveName -eq $PricingTier)  
    {  
        Write-Error "Cannot change pricing tier of $($ServerName)\$($DatabaseName) because the new pricing tier is equal to current pricing tier"  
        return  
    }  
    else  
    {  
        Write-Output "Changing pricing tier to $($Edition) - $($PricingTier)"  
        $null = Set-AzSqlDatabase -DatabaseName $DatabaseName -ServerName $ServerName -ResourceGroupName $ResourceGroupName -Edition $Edition -RequestedServiceObjectiveName $PricingTier  
    }  
       
    ########################################################  
    # Show when finished  
    ########################################################  
    $Duration = NEW-TIMESPAN –Start $StartDate –End (GET-DATE)  
    Write-Output "Done in $([int]$Duration.TotalMinutes) minute(s) and $([int]$Duration.Seconds) second(s)"  
    
    0 comments No comments