In past blog posts, I have explained about deploying Azure Function Apps through the portal and through Azure DevOps.
I have not written a post about deploying an Azure Function App through Visual Studio Code. The reason for that is that the Microsoft docs have an excellent guide on how you can do that, so I didn’t think I’d have much to add. The only thing that is not explained on that page is how you can take care of the prerequisites. There is quite a list there. Let’s walk through them and see if we could actually automate the steps to prepare to create an Azure PowerShell Function with Visual studio Code.
Why should you care if you have the portal and Azure DevOps? Well Visual Studio Code gives you an extra dimension: Local debugging. You can run the function on your local machine and quickly find issues in your code.
When you are done, you have the option to deploy directly to your Azure subscription. This method is great for development work and I have used this exclusively for 25daysofServerless
What do you need
When you open the Microsoft Doc I talked about earlier, everything is explained step by step. Except the prerequisites. They are just typed out:
The reason for that is that there are multiple ways to install all these requirements, depending on what operating system you use.
Let’s go through them.
Although this is called PowerShell Core in the doc, this officially is called just PowerShell.
Azure PowerShell Function apps work with the most recent GA version of PowerShell. At this time that is 6.2. So you need to download PowerShell if you haven’t yet.
Visual Studio Code
As the deployment is meant for Visual studio code, it makes sense that you need that software. If you are not using it now you should consider it anyway, it is really powerful tooling
The PowerShell extension for Visual studio Code
If you work with PowerShell, you should have been using this as well. It can easily be downloaded within Visual Studio Code. While you are at it, you might want to try the PowerShell Preview extension. It has the latest features and until now has been compatible with Azure Function Apps. Of course you do need to consider it a preview version, so no guarantees for the future.
.NET Core SDK 2.2
This is a bit counter intuitive, as version 2.2 is end of live, while version 2.1 is the lts version. The thing is, the Azure Functions Core Tools require this version to work with v2 Function Apps. You can install multiple .net Core versions in parallel though. So I would recommend installing the newest version (3.1 at the time of writing) as well. With PowerShell Function Apps, you can use both versions, depending on if you create a v2 app or a v3 app. If you don’t want to install 2.2, you could try using extension bundles, but I don’t have experience with those.
Azure Function Core Tools
This is the package for local development. You can install this tool through Chocolatey or through npm, which you can install with Node.js. If you have one of those already installed, that one can be your preference.
Node.js has more options, like creating v3 apps. But if you only install Node.js for this purpose, you should be careful with the install wizard, as the default installation is very extensive and not all tools are needed.
Chocolatey is pretty straight forward. You can only use v2 apps at this time though and it does not work on Mac or Linux
Seems like a lot of work?
Spoiler alert: it’s not that bad and you only have to do it once.
But of course, I wanted to see the options for automation. And it is possible to install everything through a PowerShell script!
There are a few disclaimers though:
- This script uses the registry to find if software is already installed. It also uses Chocolatey for installation. This means that the script will only work on Windows
- I make use of scripts created by the great community for PowerShell and VSCode. They are called directly from the internet, which means you are not always sure what you are running. Take the time to read the scripts if you use this in a production environment. You can find the used scripts here
Find the script below. It can run in both Windows PowerShell (tested with 5 and up) as PowerShell 6.
To prepare to create an Azure PowerShell Function with Visual studio Code sounds like a lot of work, but it’s not that bad. And you can automate it completely if you want. More than that, it is absolutely worth it if you work with Azure Function Apps.