0
votes

I'm trying to add an environment variable to azure functions, there is the "local.settings.json", but it's only for local development, though the documentation doesn't give any clue as to how I can set settings in production.

Any documentation about environment variable only explains how to READ environment variables, but I wish to WRITE an environment variable...

If no such file option exists, is it possible to set environment variables using the AZ cli?

2

2 Answers

1
votes

There are several options and it depends a bit how you are deploying your function to Azure.

Azure CLI

az functionapp config appsettings set [--ids]
                                      [--name]
                                      [--resource-group]
                                      [--settings]
                                      [--slot]
                                      [--slot-settings]
                                      [--subscription]

Where the -settings argument contains space separated name=value pairs.

More info: https://docs.microsoft.com/en-us/cli/azure/functionapp/config/appsettings?view=azure-cli-latest#az-functionapp-config-appsettings-set

Azure Function Core Tools

This publishes your function app including your local settings to the cloud:

func azure functionapp publish <FunctionAppName> --publish-local-settings

This publishes only your local settings to your function app in the cloud:

func azure functionapp publish <FunctionAppName> --publish-settings-only

See https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#project-file-deployment

Azure DevOps

If you're using Azure DevOps with Release Pipelines the Azure Function App task is convenient to use:

Supply the settings as space separated -key value pairs in the appSettings parameter in the task.

More info: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-function-app?view=azure-devops

The Azure Portal

As Matt described you could add/update using the Azure Portal as well via Configuration -> Application Settings but I recommend against that if you're doing automated deployments (which you should ;).

0
votes

To add the equivalent settings into Azure you need to create the setting under Configuration:

enter image description here

There are some issues/inconsistencies with how settings work in Azure for Azure Functions, see this github issue for more info, for example if you're trying to use a connection string with GetEnvironmentVariable you actually need to put this in as a normal setting in Azure e.g.:

enter image description here