Deploying Python Scripts to Azure Functions as a Cron Job



This content originally appeared on DEV Community and was authored by Goodluck Ekeoma Adiole

Step-by-Step Guide: Deploying Python Scripts to Azure Functions as a Cron Job

Prerequisites

  1. Azure Account (with an active subscription)
  2. VS Code installed
  3. Azure Functions Core Tools installed
  4. Python 3.9+ installed
  5. Azure Functions VS Code Extension (install from Extensions Marketplace)

Step 1: Set Up Project Structure

Create a root directory oracle-to-mssql-sync with this structure:

oracle-to-mssql-sync/
├── .vscode/                         # VS Code settings
│   └── settings.json                # Azure Functions runtime settings
├── functions/                       # Directory for all Azure Functions
│   ├── accountstatementpremium/     # Function 1
│   │   ├── __init__.py              # Your script content
│   │   └── function.json            # Trigger configuration
│   ├── agency/                      # Function 2
│   │   ├── __init__.py
│   │   └── function.json
│   ├── ...                          # Repeat for agents, allocatedpremiums, happilymaster
├── requirements.txt                 # Python dependencies
├── local.settings.json              # Local environment variables (NOT checked into Git)
└── host.json                        # Global Functions configuration

Step 2: Initialize Azure Functions Project

  1. Open VS Code → Terminal → Run:
   mkdir oracle-to-mssql-sync
   cd oracle-to-mssql-sync
   func init --python
  1. Create folders for each script:
   cd functions
   func new --name accountstatementpremium --template "timer trigger"
   func new --name agency --template "timer trigger"
   # Repeat for agents, allocatedpremiums, happilymaster

Step 3: Configure Timer Triggers (Cron Jobs)

Modify function.json for each function to run every 2 minutes:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */2 * * * *",  // Every 2 minutes
      "runOnStartup": false
    }
  ]
}

Step 4: Add Script Code

Copy your scripts into __init__.py for each function.

Example for accountstatementpremium/__init__.py:

import logging
import azure.functions as func
import cx_Oracle
import pyodbc

def main(mytimer: func.TimerRequest) -> None:
    # 1. Connect to Oracle
    oracle_conn = cx_Oracle.connect(os.environ["ORACLE_CONN_STRING"])
    oracle_cursor = oracle_conn.cursor()
    oracle_cursor.execute("SELECT * FROM your_table")

    # 2. Connect to MSSQL
    mssql_conn = pyodbc.connect(os.environ["MSSQL_CONN_STRING"])
    mssql_cursor = mssql_conn.cursor()

    # 3. Sync data (your existing logic)
    # ...

    logging.info("Sync completed at %s", utc_timestamp)

Step 5: Configure Dependencies

Populate requirements.txt:

azure-functions
cx_Oracle==8.3.0
pyodbc==4.0.39

Note: cx_Oracle requires Oracle Instant Client. Include it in your Azure Function App Settings (see Step 8).

Step 6: Local Environment Setup

Create local.settings.json (exclude from Git):

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "ORACLE_CONN_STRING": "user/password@oracle-host:port/service",
    "MSSQL_CONN_STRING": "Driver={ODBC Driver 18 for SQL Server};Server=tcp:<server>.database.windows.net,1433;Database=<db>;Uid=<user>;Pwd=<password>;Encrypt=yes;TrustServerCertificate=no;"
  }
}

Step 7: Test Locally

  1. Start the Functions runtime:
   func start
  1. Verify logs show functions triggering every 2 minutes.

Step 8: Deploy to Azure

  1. Sign in to Azure in VS Code (click the Azure icon → Sign in).
  2. Create Function App:
    • Azure Icon → Functions → Create New Function App
    • Name: oracle-mssql-sync-app
    • Runtime: Python 3.9
    • OS: Linux
  3. Deploy:
    • Right-click your Function App → “Deploy to Function App”
    • Select the oracle-to-mssql-sync folder.

Step 9: Configure Production Settings

  1. In Azure Portal → Function App → Configuration → Application Settings: Add the same settings as local.settings.json:
    • ORACLE_CONN_STRING
    • MSSQL_CONN_STRING
  2. Add Oracle Instant Client Path:
   Name  : LD_LIBRARY_PATH  
   Value : /home/site/wwwroot/oracle_instantclient
  1. Install Oracle Instant Client (Kudu Console):

    • Open https://<app-name>.scm.azurewebsites.net
    • Run:
     mkdir oracle_instantclient
     cd oracle_instantclient
     wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip
     unzip instantclient-basiclite-linuxx64.zip
    

Step 10: Verify Deployment

  1. In Azure Portal → Function App → Functions → Select a function → Monitor.
  2. Check logs for sync activity every 2 minutes.

Troubleshooting Tips

  1. Connection Issues:
    • Use ping in Kudu Console to verify network access to Oracle/MSSQL.
    • Enable Azure Application Insights for detailed logging.
  2. Dependency Errors:
    • Run pip install -r requirements.txt in Kudu Console.
  3. Cron Not Triggering:
    • Validate cron expression: 0 */2 * * * * = every 2 minutes.

Final Project Structure

oracle-to-mssql-sync/
├── .vscode/
│   └── settings.json                # { "azureFunctions.deploySubpath": "." }
├── functions/
│   ├── accountstatementpremium/
│   │   ├── __init__.py             # Your script from accountstatementpremium.py
│   │   └── function.json           # Timer config
│   ├── agency/
│   │   ├── __init__.py             # agency.py content
│   │   └── function.json
│   ├── ...                         # Other functions
├── host.json                       # { "version": "2.0" }
├── local.settings.json             # .gitignore this!
└── requirements.txt                # Dependencies

By following these steps, your Python scripts will run as scheduled Azure Functions, syncing data between Oracle and MSSQL every 2 minutes.


This content originally appeared on DEV Community and was authored by Goodluck Ekeoma Adiole