Azure Functions, Node.js and Environment Variables
Another fun project (yes another one) using 2 of my favourite frameworks: serverless with [Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node" target="_blank) and Node.js. I'm developing locally using the [Azure Functions Core Tools](https://www.npmjs.com/package/azure-functions-core-tools" target="_blank) (former CLI) and [Visual Studio Code](https://code.visualstudio.com/" target="_blank). It's a match made in heaven as I get to be super productive using the CLI and at the same time feel like a proper developer debugging issues in VS Code. I can attest that I don't have a single Console.log()
in my JavaScript code.
Since this is a real-life project, I get to do more than just simple "hello world" samples and slowly but surely I get to appreciate the effort that's gone into making the overall CLI experience as pleasant and smooth as possible.
This time, the experience I'm referring to is around working and using Environment Variables. These are usually for defining values like API keys, connection strings etc that the Function code needs to consume. Azure Functions provide us with a local.settings.json
file where we can define these variables. For this example, I'll use 3 variables
{
"StorageConnectionString":"A-Connection-String",
"API_KEY":"some-api-key",
"LOCATION":"London,UK"
{
These variables can be accessed from our code via process.env["variablename"]
. We can define as many as you want to allow your Functions to operate. By default, these values are for local testing only. This means that when we publish your Function App to Azure, we don't have to worry about overwriting settings etc.
However, there are legitimate cases that we may want to use production values in the local.settings.json
. This is usually done to test 3rd party APIs. Or for testing integration with one of the in/out bindings, e.g. Azure Storage Blobs or Queues. Consequently, it's very likely that we would want to push these variables to Azure during the deployment. Worry not my friends, as the Azure Core Tools have our back!
To publish the code along with the settings as one unit, we can use the following command from the CLI:
func azure functionapp publish <YourFunctionAppName> --publish-local-settings -i --overwrite-settings -y
Running this command should result in the following output:
Checking the Azure Portal, we can now see that my local variables have been copied across with the right values.
This last image looks like a CIA redacted document, so apologies for the obfuscation, but this is highly classified data etc etc.
Note: deployments to Azure or any production environment should be done through a proper CI/CD pipeline and not using a Right-click -> Publish approach. In my case, this is nowhere near production and it's only at the early stages of inception. Once at a good stage, VSTS will take over the complete build and deployment processes. Word to the wiser etc
I hope this post can highlight with the Azure Functions Core tools are designed with developer productivity and efficiency in mind. Do you use them and if yes, what do you think?