0

I thought it would be pretty simple, but I seems as if I am missing something

import os

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

#for testing only
@app.route(route="get_tested", methods=["POST"])
def get_tested(req: func.HttpRequest) -> func.HttpResponse:

    logging.info("Processing 'get_tested' request")
    VAR = os.environ['VAR_NAME']
    print(VAR)
    return VAR 

at the moment all I am trying to achieve is to print the "VAR" but it just comes back as None

I have verified that secret is set in the "Environmental Variables" on the left-hand panel.

when I do the tests and invoke the https request from the function portal/ postman, I get a 500 error

Edit: added tag environmental-variables, changed env var name from "secret" to "VAR" and python var from "secret" to "VAR" to get rid of confusion. I am already using keyvault for actual secrets, 2, updated script and added more information

2 Answers 2

0

Now, I'm more familiar with Synapse but if you are simply trying to get a secret, you may want to consider using Azure Key Vault for this. It's free until like a million requests per month, so no worries there unless you need to access your secret every 2 seconds. You can easily import it using the following code:

# make sure you have these libraries installed of course
from azure.identity import DefaultAzureCredential 
from azure.keyvault.secrets import SecretClient

key_vault_url = "https://vault_name.vault.azure.net/"
secret_name = "secret_name"

credential = DefaultAzureCredential()
client = SecretClient(vault_url=key_vault_url, credential=credential)

retrieved_secret = client.get_secret(secret_name)
print(retrieved_secret.value)

Key Vault stores you secret securely and allows for separate access control, since it is a resource by itself. Meaning, this also requires your Azure User (EntraID) to have the respective permissions, but that is fairly easy to do in Azure, if you are and Admin and are able to subscribe to new resources.

Let me know if this helps! Not the environment-variable solution you may be looking for but should get the job done as well.

EDIT:
I'm a dork, should read your code properly... try adding:

from dotenv import load_dotenv

load_dotenv()

underneath your "import os" line.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'm actually using KV for actual secrets already. I just used the word "secrets" as the env var label
load_dotenv() was my first try. actually have it in there as part of local testing.
0

The code itself looks correct to me. If that’s returning None it could be due to your Python version. Azure's flex consumption plan does not fully support Python 3.13 yet.

Can you confirm what Python version your Function App is set to in Configuration → General settings? At the moment, Azure Functions officially supports Python 3.10, 3.11, and 3.12. If the app is configured to use 3.13, the runtime will not load any environment variables.

Reference: Azure Functions Flex Consumption plan hosting

1 Comment

ran into the same issue... back to pyhton 3.12 and it works like charm.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.