Jump to Most used commands and quick guide
Deployment technologies in Azure Functions
Deployment technologies in Azure Functions | Microsoft Learn
Create a Python function in Azure from the command line
Create a Python function from the command line – Azure Functions | Microsoft Learn
Check environment, after all prerequisites is installed.
Prerequisite check
Powershell is used for a code
# Check that the Azure Functions Core Tools version is 4.x.
func --version
# Verify version 5.0 or later
(Get-Module -ListAvailable Az).Version
# Sign in to Azure and verify an active subscription
Connect-AzAccount
# Check your Python version reports 3.9.x, 3.8.x, or 3.7.x.
python --version
Could create a env
Create a local function project
PS C:\giti2022not> mkdir azfunctionsrepo
PS C:\giti2022not> cd .\azfunctionsrepo\
PS C:\giti2022not\azfunctionsrepo> func init LocalFunctionQA --python
PS C:\giti2022not\azfunctionsrepo> cd .\LocalFunctionQA\
This folder contains various files for the project, including configuration files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
Add a function
Add a function to your project by using the following command, where the –name argument is the unique name of your function (HttpExample) and the –template argument specifies the function’s trigger (HTTP).
func new --name coinfun01qa --template "HTTP trigger" --authlevel "anonymous"
func new creates a subfolder matching the function name that contains a code file appropriate to the project’s chosen language and a configuration file named function.json.
Get the list of templates by using the following command:
func templates list -l python
init.py
init.py contains a main() Python function that’s triggered according to the configuration in function.json.
function.json
function.json is a configuration file that defines the input and output bindings for the function, including the trigger type.
If desired, you can change scriptFile to invoke a different Python file.
Run the function locally
func start
The powershell output has the URL and logs the requests.
http://localhost:7071/api/coinfun01qa
Visit the URL
Ctrl+c for quit
Change the init.py
Lets change the init.py so we can start coding.
import logging
import azure.functions as func
# clean template
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
clean_template = "Welcome to clean template"
return func.HttpResponse(clean_template,mimetype="text/json", status_code=200)
run func start and visit the URL again.
Create supporting Azure resources for your function
Before you can deploy your function code to Azure, you need to create three resources:
- A resource group, which is a logical container for related resources.
- A storage account, which maintains the state and other information about your projects.
- A function app, which provides the environment for executing your function code. A function app maps to your local function project and lets you group functions as a logical unit for easier management, deployment, and sharing of resources.
Create a rg named rg-wecoinfun01qa in we
Create a storage account named staccwecoinfun01qa in we, standard LRS
Create a function app named, coinfun01qa, code Python 3.9, we, Linux, consumption, in the rg we created above and with the sta we created above. Select yes to application insight new (coinfun01qa). Disable continuous deployment now.
Now we have the following in rg-wecoinfun01qa:
(Log analytics is outside, hm)
Deploy the function project to Azure
func azure functionapp publish coinfun01qa
Much output, local had higher Python version then 3.9, this is the essential to know
Updated 10.07.2023, it can take some seconds before Azure is ready for connection for publish.
Can use cli Zip push deployment for Azure Functions | Microsoft Learn
or wait a bit
Sign in issues
Visual Studio Code does not connect to my Azure account – Microsoft Q&A
>Azure sign in with device code
Use powershell
PowerShell 7.3.9
PS C:\Users\username> az login --tenant ######
PS C:\Users\username> cd C:\giti2022not\azfunctionsrepo\LocalFunctionQA\
PS C:\giti2022not\azfunctionsrepo\LocalFunctionQA> ls
PS C:\giti2022not\azfunctionsrepo\LocalFunctionQA> func azure functionapp publish coinfun01qa
# update 19.03.2024 start, if you are to alter code and have already deployed it
# run
func azure functionapp publish coinfun01qa --python
# update 19.03.2024 end
Local python version '3.10.7' is different from the version expected for your deployed Function App. This may result in 'ModuleNotFound' errors in Azure Functions. Please create a Python Function App for version 3.10 or change the virtual environment on your local machine to match 'Python|3.9'.
Getting site publishing info...
Creating archive for current directory...
Performing remote build for functions project.
Deleting the old .python_packages directory
Uploading 10.36 KB [##############################################################################]
Now go to Azure and copy the URL and visit it.
https://coinfun01qa.azurewebsites.net/api/coinfun01qa
Verify in Azure or just look in metrics
Run the following command to view near real-time streaming logs in Application Insights in the Azure portal.
func azure functionapp logstream coinfun01qa --browser
Now we are done and can start to code more and deploy, remember to update requirements.txt if you add some libs with pip.
Import a new class
Deploy locally, then deploy to Azure.
To debug start
func start --verbose
logging default
logging.info etc will come in the Powershell output
Most used commands and quick guide
# New function after cd to dir
func new --name coinfun01qa --template "HTTP trigger" --authlevel "anonymous"
# Start
func start
# Ctrl+c for quit
# Debug
func start --verbose
Create resources in Powershell (then workspace and all will be in same rg)
New-AzResourceGroup -Name rg-wecoinfunc01qa -Location 'West Europe'
New-AzStorageAccount -ResourceGroupName rg-wecoinfun01qa -Name staccwecoinfun01qa -SkuName Standard_LRS -Location 'West Europe'
New-AzFunctionApp -Name coinfun01qa -ResourceGroupName rg-wecoinfun01qa -StorageAccountName staccwecoinfun01qa -FunctionsVersion 4 -RuntimeVersion 3.9 -Runtime python -Location 'West Europe'
# OS type is default linux
Edit the quota for fileshare
# Deploy after cd to location for coinfun01qa
func azure functionapp publish coinfun01qa
Stream logs
Stream execution logs in Azure Functions | Microsoft Learn
Do a request to URL, and view in Portal, stop and start and so on.
Publish without seeing changes? (Maybe good to test local first)
But view the log stream after visit the URL 1-3 times
Choose level
View the code in portal
We can vie the code and files in the portal but since it is published with VSC, we cannot edit, we do that locally and then deploy.
Code + Test
Bonus: Using managed identity
Quickstart: Azure Queue Storage client library for Python – Azure Storage | Microsoft Learn
Connect to a queue from the function using managed identity
Create the queue in the same st account
Then code
NOTE: Remember to update requirements.txt
from azure.identity import DefaultAzureCredential
from azure.storage.queue import QueueServiceClient, QueueClient, QueueMessage
class StorageQueue:
def __init__(self):
self.queue_client = None
def connect_queue(self):
try:
logging.info("Azure Queue storage, trying to connect")
# Quickstart code goes here
account_url = "https://your-storage-account.queue.core.windows.net"
default_credential = DefaultAzureCredential()
self.queue_client = QueueClient(account_url, queue_name="your-queue-name" ,credential=default_credential)
logging.info("Azure Queue storage, connection success")
except Exception as ex:
logging.error(ex)
return self.queue_client
def send_msg(self,message):
self.connect_queue()
try:
self.queue_client.send_message(message)
logging.info("Sent msg to Azure Queue storage")
except Exception as ex:
logging.error(ex)
Enable system assigned to the function.
On the st account, add the function app.
The Principle of Least Privilege states that a subject should be given only those privileges needed for it to complete its task
Visit ULR and the queue is updated with msg:
Logic app is timer
Add a HTTP logic app and run it every x hour