0

Can you create and use appsettings.json in the repo of an Azure Function in Python (v2 programming model), and so that when you deploy this function the settings end-up in App Settings in the Azure Portal? How to do this?

I thought such method is mentioned in the documentation for other languages and I tried it for Python function but it did not work. I am looking for some pragmatic way to version-control and easily deploy app settings (other than using "advanced edit" functionality in the portal).

average.everyman
  • 123
  • 1
  • 12

2 Answers2

1

The app settings in appsettings.json will not be automatically deployed to App Settings in the Azure Portal. Instead, you can use Azure CLI or Azure PowerShell to deploy the app settings to App Settings.

I have tried with the same approach that you provided in the above MS doc in python.

Firstly added new application settings in azure portal and setup name and value.

enter image description here

Created an appsettings.json file in the root directory of the function app.

enter image description here

Then updated __init.py__ as below.

import configparser
import os

config = configparser.ConfigParser()
config.read('appsettings.json')

my_setting = os.environ["MySetting"]
connection_string = os.environ["ConnectionString"]

Then, Iam unable to run the function with following error below.

enter image description here

I have checked mysetting in environmental variable but, no luck.

Try the below one in your environment.

  • Add your settings to the App Configuration store. Install the Azure App Configuration Python library by running pip install azure-appconfiguration.

enter image description here

  • Set an environment variable named AZURE_APPCONFIG_CONNECTION_STRING with the connection string of your App Configuration store.
from azure.appconfiguration import AzureAppConfigurationClient
from azure.functions import _app_name

connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)

def  main(req: func.HttpRequest) -> func.HttpResponse:
    setting = app_config_client.get_configuration_setting(key="setting_key")
    return func.HttpResponse(f"Setting value: {setting.value}")

Check for further reference that may help to you.

Suresh Chikkam
  • 623
  • 2
  • 2
  • 6
0

Alright, so I know I'm kinda late to this but I stumbled upon this when I wanted to accomplish the same thing. Tracking appsettings as part of a repo and automatically applying them to your App is now one of the features of https://github.com/luis261/aztraphile which might suit you perfectly since you mention you're working with the V2 programming model in Python (which is exactly what aztraphile is made for/ships with)

If you don't want/need the additional automation features, you could just copy this line from the contained pipelines file: https://github.com/luis261/aztraphile/blob/5de5da9300b27eedc01dede66c1d80a180303320/cfg/azure-pipelines.yml#L61C144-L61C144

az functionapp config appsettings set -g '$(ResourceGroupName)' -n '$(FunctionAppName)' --settings '@$(Build.SourcesDirectory)/appsettings.json'
Luis
  • 1
  • 1