paint-brush
How to use Azure Functions Core Tools to create a local.settings.json file and run Functions locallyby@willvelida
9,344 reads
9,344 reads

How to use Azure Functions Core Tools to create a local.settings.json file and run Functions locally

by Will VelidaAugust 13th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Microsoft Data Platform MVP based in Auckland, New Zealand. Software Engineer and Microsoft Data platform MVP. Before deploying our Functions, it's good to debug our Functions locally to ensure that it works as expected. Using Azure Function Core Tools can help speed up our development process by allowing us to retrieve our app settings from Azure. Using these tools, we can import our Function settings to that file so we can run our functions locally as if we were running it against that environment. We wouldn't want to waste our time copying and pasting all those settings!

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to use Azure Functions Core Tools to create a local.settings.json file and run Functions locally
Will Velida HackerNoon profile picture

Before deploying our Azure Functions, it's good to debug our Functions locally to ensure that it works as expected. Running our Functions locally requires a local.settings.json file to store our application settings.


When we first create a Function in Visual Studio, a local.settings.json file is generated for us. However, if we clone a Function app from a repository, this file won't be cloned! (Hopefully, it won't be cloned. It's good practice not to commit this file to your repo since it has application secrets!).


Thankfully, we can use Azure Function Core Tools to create a local.settings.json file and import our Function settings to that file so we can run our Functions locally as if we were running it against that environment!

Why would we do this?

Say if we have multiple environments (DEV, Test, UAT etc) and we wanted to locally debug a Function using that environments settings, we can use Azure Function Core Tools to simplify the retrieval of those settings. Especially for Functions that have a lot of settings. We wouldn't want to waste our time copying and pasting all those settings!

Before we start

You'll need to make sure that the Azure Function Core Tools have been installed on your machine. Azure Functions Core Tools include a version of the same runtime that powers Azure Functions Runtime that we can run on our machine.


While we'll just be focusing on fetching our app settings, Functions Core Tools come with commands that allow us to create Functions, deploy our Functions, and more!


If you haven't installed the tools yet, check out this documentation to get started.

Let's begin

For this tutorial, I've cloned one of my existing projects from GitHub. I've deployed this Function to Azure already and my .gitignore file excludes the local.settings.json file that was generated when I created the project.


To help me with local debugging, I'm going to use the Function Core tools to help fetch my app settings from Azure. Just to verify that the local settings file isn't in my project directory, here's a screenshot:


image

We'll need to create a JSON file to write our App settings to. To do this, right-click your Functions Project and create a new file. Create a JavaScript JSON Configuration File and give it the name local.settings.json

Step 1: Set our Azure Subscription

If you work with multiple Azure Subscriptions, we'll need to set it using AZ CLI to ensure that when we run our Function Core Tools, it's looking in the right place.


Open up the command line or PowerShell on your machine and log into your Azure account by running the following command:


az login


Once you're logged in, you should see a list of subscriptions to which you have access. To set the subscription that your Function is hosted in, run this command:


az account set -s "<subscription-name-or-id>"


-s being the shorthand parameter for the subscription.

Step 2: Fetch our App Settings

Now we can fetch our app settings from our Function App in Azure. Make sure you are in your Project's directory and run the following command in your terminal:


func azure functionapp fetch-app-settings '<function-name>' --output-file local.settings.json


You should see an output in your terminal similar to this:


image

Go back into your project in Visual Studio and check out your local.settings.json file. We can see that our Function settings have been retrieved and written to our local settings file!


However, there's just one problem...


image

Our settings are encrypted! That's not going to help us when running our Function locally! Function app settings are encrypted when stored and are only decrypted before being injected into your Function's process memory when it starts.


To learn more about application settings in Azure Functions, check out this article.

Step 3: Decrypting our settings.

Thankfully, we can decrypt our settings using Azure Function Core Tools! In your terminal, run the following command:


func settings decrypt


Head back into Visual Studio and you'll see that your settings have been decrypted and ready to use for your local debugging needs!


(Of course, I'm not going to give you a screenshot of that! It's a secret! 😂😉).


We can now start up our Function locally as if we were running it against our Azure environment!

One thing to note here! If you are storing some of your application settings in Key Vault (as you should!), then when you retrieve the setting, you may only get the Key Vault URL of which the secret is stored in. To use the actual value, you'll need to retrieve this from your Key Vault.

Wrapping up

As you can see, using Azure Function Core Tools can help speed up our development process by allowing us to quickly retrieve our Function App settings.


If you want to learn more, check out the following resources:


If you have any questions, feel free to reach out to me on Twitter or ask away in the comments!


Happy Coding! 👨‍💻👩‍💻💻