Update Environment Variable for Azure function APP in Python

Xiuyang Bobby Sun 105 Reputation points
2024-08-13T20:26:21.18+00:00

When I deployed the Azure function, I set the environment variables. Is there a way to change the value while the function is running?

For example:

I have a variable called fetched = false, and inside the function, after I fetched the data, is there any logic in the code I could use to update the environment variable fetched to true?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-08-14T14:56:08.4433333+00:00

    Hello : @Xiuyang Bobby Sun Unfortunately, you cannot change the environment variables while the function is running. Environment variables are set when the function app starts up and are read-only during the lifetime of the function app.

    If you need to store data that can be updated during the lifetime of the function app, you can use other storage options such as Azure Blob Storage, Azure Table Storage, or Azure Cosmos DB.

    For your specific use case, you could use a flag or a variable to keep track of whether the data has been fetched or not, and update that variable accordingly.

    For example, you could use a boolean variable called fetched and set it to False initially. Once you fetch the data, you can set fetched to True. Here's an example:

    import os
    fetched = False 
    def main(req): global fetched 
    if not fetched: 
    	# fetch data 
    	fetched = True 
    else: # data has already been fetched 
    	pass
    

    This way, you can keep track of whether the data has been fetched or not without relying on environment variables.


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.