6

In my Azure Function, I have specified an Environment Variable/App Setting for a database connection string. I can use the Environment Variable when I run the Function locally on my Azure Data Science Virtual Machine using VS Code and Python.

However, when I deploy the Function to Azure, I get an error: KeyValue is None, meaning that it cannot find the Environment Variable for the connection string. See error:

Exception while executing function: Functions.matchmodel Result: Failure
Exception: KeyError: 'CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING'
Stack:   File "/azure-functions 
  host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 315, in 
  _handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args)
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
  result = self.fn(*self.args, **self.kwargs)
File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", 
  line 434, in __run_sync_func
  return func(**params)
File "/home/site/wwwroot/matchmodel/__init__.py", line 116, in main
File "/home/site/wwwroot/matchmodel/production/dataload.py", line 28, in query_dev_database
  setting = os.environ["CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING"]
File "/usr/local/lib/python3.7/os.py", line 679, in __getitem__
  raise KeyError(key) from None'

I have tried the following solutions:

  • Added "CONNECTIONSTRINGS" to specify the Environment Variable in the Python script (which made it work locally) setting = os.environ["CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING"]

  • Used logging.info(os.environ) to output my Environment Variables in the console. My connection string is listed.

  • Added the connection string as Application Setting in the Azure Function portal.

enter image description here

  • Added the Connection String as Connection Strings in the Azure Function portal.

enter image description here

Does anyone have any other solutions that I can try?

4
  • You could try removing CONNECTIONSTRINGS: from the name of the environment variable. : can cause issues, I believe mainly on Linux app service plans. Take care to remove it both in your code and in the application settings of the AppService.
    – Alex AIT
    Commented Mar 20, 2020 at 22:23
  • I had to add CONNECTIONSTRINGS: in order to get it to run locally. It would not run locally until I added the key name.
    – JCM
    Commented Mar 23, 2020 at 21:00
  • Any update now? Commented Mar 26, 2020 at 8:27
  • I was able to edit the local.settings.json to remove the CONNECTIONSTRINGS:. I had nested JSON and removed that. Now it runs locally with just "PDMPDBCONNECTIONSTRING". Trying to get it to run on Azure as Function still.
    – JCM
    Commented Mar 27, 2020 at 19:18

4 Answers 4

7

Actually you are almost get the right the connection string, however you use the wrong prepended string. Further more detailed information you could refer to this doc:Configure connection strings.

Which string to use it depends on which type you choose, like in my test I use a custom type. Then I should use os.environ['CUSTOMCONNSTR_testconnectionstring'] to get the value.

From the doc you could find there are following types:

  • SQL Server: SQLCONNSTR_
  • MySQL: MYSQLCONNSTR_
  • SQL Database: SQLAZURECONNSTR_
  • Custom: CUSTOMCONNSTR_

enter image description here

enter image description here

enter image description here

5
  • I have tried it with and without the prefix. When i deploy it without the prefix, the prefix is automatically added. I can see the prefix there when I print(os.environ) to the console, which I can view in the Azure Portal App Insights using func.HttpResponse(print(os.environ)). I can see the correct Environment Variables but the connection still isn't working.
    – JCM
    Commented Mar 27, 2020 at 17:53
  • So you got a new problem: could not connect the database with the connection string? Cause it's different problem, suppose you should create a new question. And if this could help you, you could accept it as the answer. Commented Mar 27, 2020 at 18:25
  • I don't think you have to specify the type of connection string. That gets picked up automatically based on your settings in the portal.
    – JCM
    Commented Mar 27, 2020 at 19:20
  • I made a new post about a slightly different issue. Please respond if you can! stackoverflow.com/questions/60893887/…
    – JCM
    Commented Mar 27, 2020 at 20:47
  • So you have solved your problem with my way, if this could help you you could accept it as the answer. Commented Mar 28, 2020 at 12:58
2

I was struggling with this and found out this solution:

import os

setting = os.getenv("mysetting")
1

In Functions application settings, such as service connection strings, are exposed as environment variables during execution. You can access these settings by declaring import os and then using

setting = os.environ["mysetting"]

As Alex said, try to remove CONNECTIONSTRINGS: from the name of the environment variable. In azure portal just add mysetting in application settings as keyName.

enter image description here

4
  • I had to add CONNECTIONSTRINGS: in order to get it to run locally. It would not run locally until I added the key name. I have tried to deploy to Azure Function with and without CONNECTIONSTRINGS:; neither options will work.
    – JCM
    Commented Mar 23, 2020 at 21:02
  • How do your store your connection? You could just store it under values like this thread.
    – Joey Cai
    Commented Mar 24, 2020 at 1:36
  • Yes, I store the connection string as a value and give the setting a name like you did with mysetting. Still does not work.
    – JCM
    Commented Mar 27, 2020 at 15:14
  • I made a new post about a slightly different issue. Please respond if you can! stackoverflow.com/questions/60893887/…
    – JCM
    Commented Mar 27, 2020 at 20:47
0

I figured out the issue with help from George and selected George's answer as the correct answer.

I changed the code to os.environ["SQLCONNSTR_PDMPDBCONNECTIONSTRING"] but also I had been deploying the package from VS Code to Azure via the following code using Azure Command Line Interface (Azure CLI). This code overwrites Azure App Settings with the local.settings.json.

func azure functionapp publish <MY_FUNCTION_NAME> --publish-local-settings -i --overwrite-settings -y

I think that was causing changes to the type of database that I had specified in Azure App Settings (SQL Server), so when I had previously tried os.environ["SQLCONNSTR_PDMPDBCONNECTIONSTRING"] it didn't work because I was overwriting my Azure settings using my local.settings.json, which did not specify a database type.

I finally got it to work by deploying using the VS Code Extension called Azure Functions (Azure Functions: Deploy to Function App). This retains the settings that I had created in Azure App Services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.