Domain 4 β€” Module 4 of 4 100%
22 of 28 overall
Domain 4: Configure and Manage Automation of Tasks Free ⏱ ~13 min read

Elastic Jobs and Azure Automation

Create and configure elastic jobs for multi-database T-SQL execution. Use Azure Automation runbooks for resource management. Configure alerts and troubleshoot.

Multi-database automation

Simple explanation

Elastic jobs are like sending a memo to every office branch β€” one command, executed across many databases. β€œRun this index maintenance on all 30 tenant databases.” Done.

Azure Automation is like a virtual assistant that manages your Azure resources β€” β€œScale up the database at 8 AM, scale down at 8 PM, email me if anything fails.” It runs PowerShell scripts on a schedule.

Elastic jobs

Architecture

ComponentPurpose
Job agentAzure resource that hosts and executes jobs
Job databaseAzure SQL DB that stores job definitions, history, and state
Target groupSet of databases where the job runs (individual DBs, elastic pool, server, custom list)
JobCollection of steps (T-SQL) to execute
Job executionA single run of the job against the target group

Setting up elastic jobs

# 1. Create a job agent (Azure CLI)
az sql elastic-job agent create \
  --resource-group ScaleWaveRG \
  --server scalewave-sql \
  --name scalewave-job-agent \
  --database-name JobDB

# 2. Create credentials for target databases
az sql elastic-job credential create \
  --resource-group ScaleWaveRG \
  --server scalewave-sql \
  --agent-name scalewave-job-agent \
  --name job-cred \
  --username jobuser \
  --password <secure>

Creating and running a job

-- Connect to the job database
-- Create a target group (all databases in the elastic pool)
EXEC jobs.sp_add_target_group 'TenantDatabases';
EXEC jobs.sp_add_target_group_member 'TenantDatabases',
    @target_type = 'SqlElasticPool',
    @server_name = 'scalewave-sql.database.windows.net',
    @elastic_pool_name = 'TenantPool';

-- Create a job
EXEC jobs.sp_add_job @job_name = 'NightlyIndexMaintenance';

-- Add a step
EXEC jobs.sp_add_jobstep @job_name = 'NightlyIndexMaintenance',
    @step_name = 'RebuildFragmentedIndexes',
    @command = N'ALTER INDEX ALL ON dbo.Orders REBUILD WITH (ONLINE = ON);',
    @credential_name = 'job-cred',
    @target_group_name = 'TenantDatabases';

-- Execute immediately
EXEC jobs.sp_start_job 'NightlyIndexMaintenance';

-- Check execution status
SELECT * FROM jobs.job_executions
WHERE job_name = 'NightlyIndexMaintenance'
ORDER BY start_time DESC;

Scheduling elastic jobs

Elastic jobs support cron-style scheduling or one-time execution. Schedule via the Azure portal, PowerShell, or T-SQL stored procedures.

Azure Automation

Runbooks for database management

# Example: Scale database based on time of day
$resourceGroup = "ScaleWaveRG"
$serverName = "scalewave-sql"
$databaseName = "ProductionDB"

$hour = (Get-Date).Hour

if ($hour -ge 8 -and $hour -lt 18) {
    # Business hours: scale up
    Set-AzSqlDatabase -ResourceGroupName $resourceGroup `
        -ServerName $serverName -DatabaseName $databaseName `
        -Edition "GeneralPurpose" -Vcore 8 -ComputeGeneration "Gen5"
    Write-Output "Scaled up to 8 vCores"
} else {
    # Off-hours: scale down
    Set-AzSqlDatabase -ResourceGroupName $resourceGroup `
        -ServerName $serverName -DatabaseName $databaseName `
        -Edition "GeneralPurpose" -Vcore 2 -ComputeGeneration "Gen5"
    Write-Output "Scaled down to 2 vCores"
}

Azure Automation components

ComponentPurpose
Automation accountContainer for runbooks, schedules, credentials
RunbookPowerShell or Python script
ScheduleWhen to run (recurring or one-time)
CredentialStored credentials for authentication
Managed identityRecommended auth method (no secrets)
WebhookHTTP endpoint to trigger a runbook externally

Alerts and notifications for automated tasks

ToolAlert MethodHow
Elastic jobsQuery job_executions table; set Azure Monitor alert on failuresMonitor the job database for failed executions
Azure AutomationBuilt-in job status tracking + Azure Monitor alertsAlert on runbook failure status
SQL AgentOperators + Database MailConfigure notifications on job success/failure
Azure MonitorAlert rules on metrics and logsEmail, SMS, webhook, Logic App actions

Troubleshooting

IssueToolWhere to Look
Elastic job fails on specific databasesJob databaseSELECT * FROM jobs.job_executions WHERE lifecycle = 'Failed'
Job credential rejectedTarget databaseCheck the login/user exists on the target with needed permissions
Runbook times outAzure AutomationCheck runbook job output; increase Fair Share timeout or split runbook
Runbook can’t authenticateAutomation accountVerify managed identity is enabled and has RBAC on target resources
Question

What is an elastic job agent?

Click or press Enter to reveal answer

Answer

An Azure resource that hosts and executes elastic jobs. It requires a dedicated Azure SQL Database as its job database (stores definitions, history, and state).

Click to flip back

Question

How does Azure Automation authenticate to Azure SQL resources?

Click or press Enter to reveal answer

Answer

Recommended: managed identity assigned to the Automation account with appropriate RBAC roles. Alternative: stored credentials (service principal) in the Automation account.

Click to flip back

Question

Where do you check elastic job execution results?

Click or press Enter to reveal answer

Answer

Query the jobs.job_executions view in the job database. It shows status (Succeeded, Failed, InProgress), start/end times, and per-database results.

Click to flip back

Knowledge Check

Priya's elastic job fails on 3 out of 30 target databases with 'Login failed for user jobuser.' The other 27 succeed. What should she check?

You’ve completed Domain 4! You can now automate database tasks across all Azure SQL platforms.

Next up: HA/DR Strategy: RPO, RTO, and Architecture β€” plan high availability and disaster recovery for Azure SQL solutions.