(Re)Distributing Azure Functions
Distribution of Azure Functions is really necessary if we are a software vendor developing functions as a part of our software offering. Sometimes this can be useful when we offer a SaaS and use Azure Functions to support it. In this post lets see how this distribution of Azure Functions, specially the pre-compiled functions can be done, so it can be deployed on any function app services.
Step 1 : Develop the Function
Visual Studio 2017/Visual Studio Code can be used to develop the function. We can use any triggers and write our own logic.
Step 2 : Publish
Once the development of the function is done, right click on the functions project and select "Publish". A dialog box will appear to pick a publish target. This is the important part, since we are about to create a redistributable Azure function, we have to publish the output assemblies to a folder. So select "Folder" in the dialog and we can just leave the default publish path or choose a different one.
https://image.ibb.co/hPMUSd/func_publishtarget.png
Now press "Create Profile" to create the publish profile. Now we can see the Publish window
https://image.ibb.co/cCi9Sd/func_publishprofile.png
Now, we can just click the "Publish" button to publish the function, which will run a build, and go to the target location to check if we can see the binaries. In the target location, we should be able to see
- Bin - A folder which contains all the assemblies
- One of more folder(s) named with the name of our function
- host.json
- [function project name].deps.json
https://image.ibb.co/izp50y/func_afterpublished.png
Step 3 : Zip the contents
Now zip these 4 items into a single zip file. In this step, we have just created a redistributable package of azure functions. This package may contain one or more functions.
Deployment in Azure
Now we have to test the package by deploying it in Azure. Go to Azure portal and create an empty azure function. After the initial creation of the function, we can get its URL from the "overview" page
https://image.ibb.co/nh9f0y/func_azportalurl.png
Now go to the Kudu service of the webapp by adding ".scm" in between the function webapp name and .azure websites - in this case, https://publisedfunc2.scm.azurewebsites.net/ .
In Kudu service of the app, get CMD by clicking on "CMD" under "Debug Console" menu - we will get a explorer view and a console view in the webpage. Now, in the page, navigate to "/site/wwwroot"
https://image.ibb.co/hrb4Sd/func_kudu.png
Now just drag the contents inside the "PublishedOutput" folder (in Step 2) [not the zipped folder] and drop it into the windows explorer view in the above window. The Kudu service will upload the files.
https://image.ibb.co/fEZcDJ/func_kudu_afterupload.png
Now if we navigate to the "Functions" blade inside our Functions app, we will see the deployed functions.
https://image.ibb.co/dZx3nd/func_deployedfunc.png
Why did we compress in Step 3
Azure webapps support a mechanism called "Zip-push" for the deployment. So, we can provide the zipped package in step 3 to deploy the functions using zip-push. This zip push supports REST API as well, for the programmatic deployment of the app. Or using CURL, we can simply use the following command to deploy the contents of the functions.
curl -X POST -u <publishing-user> --data-binary @<zipfile> https://%7Bsitename%7D.scm.azurewebsites.net/api/zipdeploy
So using this mechanism we can simply create any redistributable azure functions.