In my last post, I went over how to edit, run and debug Azure Functions on your local machine. Eventually, however, you want to place these functions in the cloud. They are, after all, designed to do things in the cloud on dynamic compute. There are two levels of automation you can use:
- Continuous Deployment
- Automated Resource Creation
Most of you will be interested in continuous deployment. That is, you create your Azure Functions app once and then you just push updates to it via a source code control system. However, a true DevOps mindset requires “configuration as code”, so we’ll go over how to download an Azure Resource Manager (ARM) template for the function app and resource group.
Creating a Function App in the Portal
Creating an Azure Functions app in the portal is a straight forward process.
- Create a resource group.
- Create a function app in the resource group.
Log into the Azure portal. Select Resource Groups in the left-hand menu (which may be under the “More Services” link in your case), then click on the + Add link in the top bar to create a new resource group. Give it a unique name (it only has to be unique within your subscription), select a nominal location for the resource group, then click on Create:
Once the resource group is created, click into it and then select + Add inside the resource group. Enter “Function App” in the search box, then select the same and click on Create
Fill in the name and select the region that you want to place the Azure Functions in. Ensure the “Consumption Plan” is selected. This is the dynamic compute plan, so you only pay for resources when your functions are actually being executed. The service will create an associated storage account for storing your functions in the same resource group.
In my last blog post, I created a git repository to hold my Azure Function code. I can now link this git repository to the Function App in the cloud as follows:
- Open the Function App.
- Click the Function app settings link in the bottom right corner.
- Click the Go to App Service Settings button.
- Click the Deployment Credentials menu option.
- Fill in the form for the deployment username and password (twice).
- Click Save at the top of the blade.
You need to know the username and password of your git repository in the cloud that is attached to your Function App so that you can push to it. You’ve just set those credentials.
- Click the Deployment options menu option.
- Click Choose Source.
- Click Local Git Repository
- Click OK.
I could have just as easily linked my function app to GitHub, Visual Studio Team Services or BitBucket. My git repository is local to my machine, so a local git repository is suitable for this purpose.
- Click the Properties menu option.
- Copy the GIT URL field.
I now need to add the Azure hosted git repository as a remote on my local git repository. To do this, open a PowerShell console, change directory to the function app and type the following:
git remote add azure <the-git-url> git push azure master
This will push the contents of the git repository up to the cloud, which will then do a deployment of the functions for you. You will be prompted for your username and password that you set when setting up the deployment credentials earlier.
Once the deployment is done, you can switch back to the Function App in the portal and you will see that your function is deployed. An important factor is that you are now editing the files associated with the function on your local machine. You can no longer edit the files in the cloud, as any changes would be overwritten by the next deployment from your local machine. To remind you of this, Azure Functions displays a helpful warning:
If you edit your files on the local machine, remember to push them to the Azure remote to deploy them.
Saving the ARM Template
You are likely to only need the Azure Function process shown above. However, in case you like checking in the configuration as code, here is how you do it. Firstly, go to your resource group:
Note the menu item entitled Automation script – that’s the one you want. The portal will generate an Azure Resource Manager (ARM) template plus a PowerShell script or CLI script to run it. Click on Download to download all the files – you will get a ZIP file.
Before extracting the ZIP file, you need to unblock it. In the File Explorer, right-click on the ZIP file and select Properties.
Check the Unblock box and then click on OK. You can now extract the ZIP file with your favorite tool. I just right-click and select Extract All….
Creating a new Azure Function with the ARM Template
You can now create a new Azure Function App with the same template as the original by running
.\deploy.ps1 and filling in the fields. Yep – it’s that simple!