Automating Azure Function App creation with the Azure CLI

I’m currently in the process of writing an Azure Function that I’ll be using with a Starling Bank webhook to “tax” myself every time I purchase junk food…..more on that in a future post though!

I love automating things and that coupled with getting bored of using the Azure Portal led me to taking a closer look at the Azure CLI, to automate the creation and configuration of the Function App.

The Azure CLI can be installed on Windows, macOS and Linux. I installed it on Ubuntu which runs on my Windows 11 device using Windows Subsystem for Linux (WSL). I wanted to experience it on a non-Windows platform, which is why I used the convoluted approach of running it on Linux on Windows 😀. Installation was straightforward and required a single command:

curl -L https://aka.ms/InstallAzureCli | bash

My aim was to use the Azure CLI to create an Azure Function App that runs on Windows with the PowerShell runtime based in the UK South region, I also wanted to add a key/value pair to the Application Settings which I will use to store my Personal Access Token from Starling. The PAT will be used to connect to my bank account and “tax” fast food purchases! I could/should have used Azure Key Vault for this but didn’t want to introduce extra complexity into a hobby project.

After logging into my Azure subscription using az login from Ubuntu I ran the following to declare the appname and region variables. I’m lazy and use appname for the Resource Group, Storage Account and Function App name. I used the Bash function $RANDOM to append a random number to the app name, which was useful during testing, so I didn’t have to update the app name manually after each run of the script (and there were many as I got to grips with the syntax of the Azure CLI!)

appname=burgertax$RANDOM
region=uksouth

I then created a Resource Group to house the Function App, located in the UK South region and named burgertax (appended with a random number).

az group create --name $appname --location $region

Once the Resource Group had been created, I created a Storage Account which is used to host the content of the Function App.

az storage account create \
  --name $appname \
  --location $region \
  --resource-group $appname \
  --sku Standard_LRS

Now that I had a Resource Group and Storage Account, I could create the Function App.

az functionapp create \
  --name $appname \
  --storage-account $appname \
  --consumption-plan-location $region \
  --resource-group $appname \
  --os-type Windows \
  --runtime powershell \
  --functions-version 3

Once this had completed, I ran the following to add a key/value to the Application Settings of the Function App

az functionapp config appsettings set --name $appname --resource-group $appname --settings "MyKey=MyValue"

…and this to verify that it had created the Application Setting.

az functionapp config appsettings list --name $appname --resource-group $appname

Lastly, I fired up the Azure Portal to admire my automation.

Now the fun part……writing the code for my “Burger Tax” solution 🍔.

One thought on “Automating Azure Function App creation with the Azure CLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s