Bootstrap your Container-Based Azure Function
This blog builds on this Azure docs document, but starts with my own code rather than a pre-canned solution. The solution is simple: it's a function app with a single function that is triggered by event hub content. But it's enough to demonstrate how to create a new container-based function app using the new (in preview) functions runtime v2.
Full disclosure: this blog was written in April of 2018. I used Visual Studio 2017 to prototype the function. I used an Azure-based Ubuntu 17.10 VM as my test bed.
Start by creating a new Azure Functions Function App:
Leave off the offered triggers but browse for a storage account or create one. All Azure Functions need a storage account to store their own state. Be sure you select the v2 runtime.
Right-click the project (in my case FunctionApp1) and Add a New Azure Function.
Then select the event hub trigger and add some configuration details:
Note that the "Connection string setting" isn't the actual connection string. It's a pointer to the actual setting that's going to be in the local.settings.json file for VS tests and in an environment variable for the Docker container. The Event Hub name (insights-operational-logs) that I selected is the name of the hub that receives Azure Activity Log telemetry if the Activity Log has a diagnostics profile defined that sends Activity Log telemetry to an event hub.
Add the Connection String Setting to local.settings.json:
Hit F5 to test. You should see telemetry pouring out of the event hub. If not, check that the hub exists. This is a good indicator that there is a correctly configured diagnostics profile. The hub is created in the hub namespace automatically when telemetry starts to flow.
Next task: let's get this into a Docker container. At this point, I committed my code and then cloned it in my Linux VM. In my actual test environment, this is what it looks like (only the names are different – the structure is identical. Rather than 'goliveHubConnectionString', I used 'goliveEhConnection'.)
I added Dockerfile out of band, and here it is:
This requires some explanation. The FROM designates an alpha release of the Azure Functions v2 runtime. Follow the image here and upgrade to GA when it's available. The ENV values for AzureWebJobsStorage and AzureWebJobsDashboard are required for the function to operate. And the ENV's have semicolon's ( ; ) embedded. Note that the semicolons must be quoted with a backslash (e.g. \; ). The COPY line gets the results of my dotnet publish step. Here's how that is done:
Next step is the Docker build:
And then docker run it:
- docker run -p 8080:80 –rm -it funcv2
You should see telemetry streaming to the console just as you did when testing in Visual Studio.