I am trying to make my first python function app. The app is just a simple app that creates a blob file. The code runs locally. However when I deploy to Azure I dont see the function app.
If I remove this, then it works:
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
Full code:
import logging
import datetime
import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
app = func.FunctionApp()
@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False)
def timer_trigger(myTimer: func.TimerRequest) -> None:
logging.info('Python timer trigger function executed.')
Configuration in azure:
[
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "xx-6dd2-xx-9ab8-xx",
"slotSetting": true
},
{
"name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
"value": "InstrumentationKey=xx;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/;ApplicationId=xxx",
"slotSetting": false
},
{
"name": "AzureWebJobsStorage",
"value": "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xx;EndpointSuffix=core.windows.net",
"slotSetting": false
},
{
"name": "BUILD_FLAGS",
"value": "UseExpressBuild",
"slotSetting": false
},
{
"name": "AzureWebJobsStorage__accountname",
"value": "stsxxxondev",
"slotSetting": false
},
{
"name": "ENABLE_ORYX_BUILD",
"value": "true",
"slotSetting": false
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~4",
"slotSetting": false
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "python",
"slotSetting": false
},
{
"name": "SCM_DO_BUILD_DURING_DEPLOYMENT",
"value": "1",
"slotSetting": false
},
{
"name": "XDG_CACHE_HOME",
"value": "/tmp/.cache",
"slotSetting": false
}
]
I have created a Python Azure function App and deployed your function code. Able to sync the timer trigger in portal.
function_app.py:
import datetime
import logging
import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
app = func.FunctionApp()
def upload_blob_data(blob_service_client: BlobServiceClient, container_name: str):
time_now = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S')
blob_client = blob_service_client.get_blob_client(container=container_name, blob=f"{time_now}.txt")
data = b"Sample data for blob"
# Upload the blob data - default blob type is BlockBlob
blob_client.upload_blob(data, blob_type="BlockBlob")
@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False)
def timer_trigger(myTimer: func.TimerRequest) -> None:
account_url = "https://<storage>.blob.core.windows.net"
credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=credential)
upload_blob_data(blob_service_client, "json")
logging.info('Python timer trigger function executed.')
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}
Enable Managed Identity in the function app.
Navigate to Storage Account=>Access Control=>Add Role Assignment, assign Storage Blob Data Contributor
role to the functionapp's managed identity.
AzureWebJobsStorage__accountname= <storagename>
in the Function app=>settings=>Environment Variables:Deployment logs:
Portal:
Response:
Blobs in Storage Container: