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
- Azure Account (with an active subscription)
- VS Code installed
- Azure Functions Core Tools installed
- Python 3.9+ installed
- 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
- Open VS Code → Terminal → Run:
mkdir oracle-to-mssql-sync
cd oracle-to-mssql-sync
func init --python
- 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
- Start the Functions runtime:
func start
- Verify logs show functions triggering every 2 minutes.
Step 8: Deploy to Azure
- Sign in to Azure in VS Code (click the Azure icon → Sign in).
-
Create Function App:
- Azure Icon → Functions → Create New Function App
- Name:
oracle-mssql-sync-app
- Runtime: Python 3.9
- OS: Linux
-
Deploy:
- Right-click your Function App → “Deploy to Function App”
- Select the
oracle-to-mssql-sync
folder.
Step 9: Configure Production Settings
- In Azure Portal → Function App → Configuration → Application Settings:
Add the same settings as
local.settings.json
:-
ORACLE_CONN_STRING
-
MSSQL_CONN_STRING
-
- Add Oracle Instant Client Path:
Name : LD_LIBRARY_PATH
Value : /home/site/wwwroot/oracle_instantclient
-
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
- Open
Step 10: Verify Deployment
- In Azure Portal → Function App → Functions → Select a function → Monitor.
- Check logs for sync activity every 2 minutes.
Troubleshooting Tips
-
Connection Issues:
- Use
ping
in Kudu Console to verify network access to Oracle/MSSQL. - Enable Azure Application Insights for detailed logging.
- Use
-
Dependency Errors:
- Run
pip install -r requirements.txt
in Kudu Console.
- Run
-
Cron Not Triggering:
- Validate cron expression:
0 */2 * * * *
= every 2 minutes.
- Validate cron expression:
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