We have a Azure-Function with Docker image that works on a Http-Trigger ...as per this article .. https://learn.microsoft.com/en-us/azure/azure-functions/functions-deploy-container?tabs=acr%2Cbash%2Cazure-cli&pivots=programming-language-python
However when we add a CosmosDB trigger ... it would fail with
@app.function_name(name="CosmosDBTrigger")
@app.cosmos_db_trigger(
arg_name="documents",
connection="COSMOSDB_CONNECTION_STRING",
database_name="sz-ue2XXXXX",
container_name="****",
lease_container_name="leases",
create_lease_container_if_not_exists="true",
)
def CosmosDBTrigger(documents: func.DocumentList) -> str:
if documents:
logging.info("Document id: %s", documents[0]["id"])
2024-06-18T21:15:33.567Z ERROR - Container ****eto-s-func-01_1_153a76b5 for site sz-ue2-seto-s-func-01 did not start within expected time limit. Elapsed time = 230.4885576 sec
2024-06-18T21:15:33.608Z ERROR - Container ****s-func-01_1_153a76b5 didn't respond to HTTP pings on port: 80, failing site start. See container logs for debugging.
Question 1: Can "Azure function with Docker image" have a trigger for "CosmosDB" ?
Question 2: If the answer is yes for question 1, then is there a sample of the Dockerfile ?
Thanks.
To trigger cosmos_db trigger
in container function app you need to pass the Connection string value inside the Dockerfile.
Prerequisite for Cosmosdb Trigger
leases
container should be available inside the database with PartitionKey as /partitionKey
.funcdocker_DOCUMENTDB
is my Connection name. Check ENV in docker file.
Dockerfile
:
FROM mcr.microsoft.com/azure-functions/python:4-python3.11
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
funcdocker_DOCUMENTDB=AccountEndpoint=https://funcdocker.documents.azure.com:443/;AccountKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
COPY requirements.txt /
RUN pip install -r /requirements.txt
EXPOSE 443
COPY . /home/site/wwwroot
function_app.py
:
import azure.functions as func
import logging
app = func.FunctionApp()
@app.cosmos_db_trigger(arg_name="azcosmosdb", container_name="Test",
database_name="ToDoList", connection="funcdocker_DOCUMENTDB")
def cosmosdb_trigger(azcosmosdb: func.DocumentList):
logging.info('Python CosmosDB triggered.')
OUTPUT
: