close

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Deploy Azure Functions can use your preferred language as C#, Java, JavaScript, PowerShell, or Python.

Tool for deployment function can use Visual Studio or Visual Studio Code for written code, running test locally and automatically deploy code  to Azure Function App.

Scenarios For Using Azure Functions as

Example: Deploy Azure Function Written By Python language and use Blob Storage Trigger for running function when has new file upload to storage. Function will  execute insert filename and datetime uploaded to Azure SQL Database Server.

1/Deploy SQL Database Server On Azure Portal:

– Get server name information and connect using sql studio management

– Create new database and table for demo

2/Create Azure Function For Inserting Data To Database:

3/Create storage account and enable blob storage:

4/Create python function using Visual Studio Code:

Install Azure Functions Core tool: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cportal%2Cbash%2Ckeda#v2

On Visual Code Studio install extension Azure Function

On Azure icon in the Activity bar > Functions > Create New Project

Choose python and Azure Blobe Storage Trigger

– Example main function for printing filename when has new file uploaded to Blob Storage

– Run function local and upload file for testing trigger execute successfully

– Edit function code for connect to Azure SQL Database Server using module pyodbc and insert new record when has trigger upload new file to blod storage

import logging

import pyodbc

import azure.functions as func

from datetime import datetime

def main(myblob: func.InputStream):

    logging.info(f”Python blob trigger function processed blob \n”

                 f”Name: {myblob.name}\n”

                 f”Blob Size: {myblob.length} bytes”)

    logging.info(‘Python Blob trigger function processed %s’, myblob.name)

    now = datetime.now()

    time_string = now.strftime(“%d/%m/%Y %H:%M:%S”)

    conn_str=“Driver={ODBC Driver 17 for SQL Server};Server=tcp:trivm-sqltest.database.windows.net,1433;Database=tracking-storage;Uid=trivm;Pwd=abcxyz!@#Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;”

    conn=pyodbc.connect(conn_str)

    cursor = conn.cursor()

    str_insert=“insert into fileupload values(‘”+ myblob.name+“‘,'”+time_string +“‘)”

    cursor.execute(str_insert)

    conn.commit()

    conn.close()

(replace connection string with your Azure Database Connection String ODBC)

5/Deploy Function To Azure Function App:

– Choose Deploy to Function App

6/Testing New Function App With Blob Storage Trigger:

– On Blob Container upload new files

– Query table fileupload on Azure SQL Database has new record blob filename and datetime uploaded

Tags : AutomationContainerDevOpsK8sKubernetesLinux-Unix

Leave a Response

error: Content is protected !!