This Azure Function App contains two main functions for the ScanIQ service:
GetScanIQSearchTerms: A timer-triggered function that runs on a schedule to fetch search terms from the database and initiate a data scraping batch process.ProcessBlob: A blob-triggered function that activates when a new file is uploaded to the designated Azure Storage container, processing the data within that file.
The application is built to be run locally for development and deployed to Azure for production.
Follow these steps to run and test the Function App on your local machine.
- Python 3.9+: Ensure you have a compatible version of Python installed.
- Node.js and npm: Required to install the Azure Functions Core Tools.
- Azure Functions Core Tools: A command-line tool that lets you develop, test, and publish Azure Functions from your local computer. Install it globally using npm:
npm install -g azure-functions-core-tools@4
-
Navigate to the App Directory: Open your terminal and change the directory to this folder.
cd ScanIQFunctionApp -
Install Python Dependencies: Create a virtual environment and install the required packages from
requirements.txt.python -m venv .venv source .venv/bin/activate # On Windows use `.venv\Scripts\activate` pip install -r requirements.txt
-
Configure Local Settings: The
local.settings.jsonfile holds all the necessary connection strings and secrets for the application to run. Ensure all values are correctly filled in. This file should not be committed to source control. -
Start the Local Functions Host: Run the following command to start the local runtime. It will load your functions and display the local endpoints.
func start
- Timer Trigger (
GetScanIQSearchTerms): The function will run automatically when the application starts and then again according to theSCHEDULEdefined inlocal.settings.json. You can also manually invoke it for testing via the endpoint URL that thefunc startcommand outputs. - Timer Trigger (
GetPropertyPulseIQSearchTerms): This function runs according to thePPI_SCHEDULEdefined inlocal.settings.json. - Blob Trigger (
ProcessBlob): To test this, upload a file to the Azure Storage container and path specified in yourlocal.settings.json(AZURE_STORAGE_CONTAINER). The function will be triggered automatically.
When you edit and save any of the Python (.py) files in the project, the Azure Functions Core Tools host will automatically detect the changes and reload the function workers. You do not need to restart the func start command manually. Simply save your file, watch the terminal for the reload confirmation, and you can test the new version of your function immediately.
These instructions will guide you through deploying the Function App to a Consumption Plan (Serverless), which is eligible for the Azure Functions free grant.
- Azure Account: You need an active Azure subscription. You can create one for free.
- Azure CLI: You need the Azure command-line interface installed. You can install it here.
-
Login to Azure: Open your terminal and run:
az login
-
Create a Resource Group: A resource group is a container that holds related resources for an Azure solution. Replace
<ResourceGroupName>and<Location>(e.g.,eastus).az group create --name <ResourceGroupName> --location <Location>
-
Create a Storage Account: This storage account will be used by the Function App for its internal operations (
AzureWebJobsStorage). Replace<StorageAccountName>with a globally unique name.az storage account create --name <StorageAccountName> --location <Location> --resource-group <ResourceGroupName> --sku Standard_LRS
-
Create the Function App: This command creates the Function App on a serverless Consumption plan. Replace
<FunctionAppName>with a globally unique name.az functionapp create \ --name <FunctionAppName> \ --storage-account <StorageAccountName> \ --resource-group <ResourceGroupName> \ --consumption-plan-location <Location> \ --runtime python \ --python-version 3.9 \ --functions-version 4 -
Configure Application Settings: Your app needs the same settings that are in
local.settings.json. Set them in the Azure environment using the following command for each key-value pair. This is a critical step.Get the connection string for your storage account:
STORAGE_CONNECTION_STRING=$(az storage account show-connection-string --name <StorageAccountName> --resource-group <ResourceGroupName> --query "connectionString" --output tsv)Set the required settings:
az functionapp config appsettings set --name <FunctionAppName> --resource-group <ResourceGroupName> --settings "AzureWebJobsStorage=$STORAGE_CONNECTION_STRING" az functionapp config appsettings set --name <FunctionAppName> --resource-group <ResourceGroupName> --settings "AZURE_STORAGE_CONNECTION_STRING=$STORAGE_CONNECTION_STRING" az functionapp config appsettings set --name <FunctionAppName> --resource-group <ResourceGroupName> --settings "AZURE_STORAGE_CONTAINER=scaniq" # Add all other secrets from your .env file here az functionapp config appsettings set --name <FunctionAppName> --resource-group <ResourceGroupName> --settings "DB_USER=<YourDbUser>" az functionapp config appsettings set --name <FunctionAppName> --resource-group <ResourceGroupName> --settings "DB_PASSWORD=<YourDbPassword>" # ... and so on for all other required secrets.
-
Deploy the Code: Finally, from within the
ScanIQFunctionAppdirectory, publish your project to Azure.# Make sure you are in the ScanIQFunctionApp directory func azure functionapp publish uncapscrapefunction
Your Function App is now live on Azure. ```
Your Function App is now live on Azure.