-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A standard or better way to populate local environments with azd env variables #4384
Comments
^^ Added related issues/PRs to the description. |
@vhvb1989 might be a good use-case for Named/Specialized hooks feature? |
IIRC, at the top of the wish-list is to use the python app as the starting point. For example, we currently support running hooks where azd automatically set all the .env values as env vars for the script defined in the hook, but this requires folks to use azd as the starting point. Either by running the azd command that triggers the hook, or by running We also considered having something like In a world where we want people to just run |
Yeah I tend to agree, for Python we would like to be able to say "python bla.py" or "py bla.py" or whatever works on an OS. Or even use other Python-specific runners like the new I feel a little silly making a Pypi package out of my 10 line script, but I could do it! Or are you saying you'd do it? I'm curious what Yohan would want for JS environments though, I've asked him to comment. |
I think this is problematic because now we're saying that 'azd' abstractions are leaking into the app, and we have an extra package that we have to maintain for every language. With .env files the benefit is that you don't know where they came from, they just exist. |
Creating an sdk lib for azd would bring more things. List environments, find azd projects, even calling commands from your App. I can definitely help/collaborate to a project like this, but it would require more than one happy developer XD. I think an sdk lib could be added under the Azurw SDK umbrella. Instead of an azure service, the target would be a local azd service or just the cli |
@richardpark-msft |
In the .env libraries I've used they usually have an option of specifying the actual .env file you load (with the default being Looking at it now, it has |
In almost all contexts I've been working with (and not only JS/Node.js contexts), The fact that
@richardpark-msft I'm not sure what you means by having built-in filtering to
|
The filter could help, as AZURE_ENV_NAME is definitely the most problem-causing of the env variables. However, many people do currently have a flow where an outputted env variable is also an input env variable for main.parameters.json, so we would need to discourage that flow. Otherwise you'll have weird things leaking, like some customization for one environment leaking into an environment where you don't want it. I'll file an issue with Python extension about the global leaking. I don't know if other language extensions also do that. |
For the purpose of this specific issue, I wonder if, along the lines of what @richardpark-msft is proposing,
I'd lean towards regex here -- most times I also wonder if, we may want to think about:
In general, the What isn't covered here by this simplistic proposal, is that there are cases where users want environment values to "flow" into their client-side builds without the necessary exporting of
I created #4387 since this is also a topic of interest that I think about a lot. Would love to hear your feedback here. |
Should we be prefixing certain env variables with APP_ then? We don't have a convention currently, so I wouldnt have a regex that would work with my current templates, but I could move towards a convention. |
@weikanglim Adding a prefix to get the filtering we want would not be working here: many frameworks or SDK needs specific env vars names, and most Azure tools use AZURE_* for env vars, which is also used by AZD. |
@sinedied Happy to learn more from your example. In my mind, with the simplistic model, you could simply rerun azd env get-values --filter AZURE_CLIENT_ID --set-or-append web/.env
azd env get-values --filter ^VITE_ --set-or-append web/.env We can also build towards something where "app referencing" is more of a first-party concept that can be expressed via some azure.yaml configuration -- happy to learn from any observations you have. |
If we're talking full regexes we can also do something like this:
Using alternation, and that would also be valid. So just having that support, on it's own, would be enough to specify all the variables we want to grab. |
I likely would not use a complex regex and just build the file using individual azd env get-value calls as I'm doing now, but there's still the issue that azd environment variables can be tainted by the global env variables. Stepping back a bit, what if we could be more specific about the env variable references in main.parameters.json? Right now, something like $AZURE_OPENAI_KEY can come from both .azure/CURRENT-ENV/.env or come from the global environment. That caused so many issues for developers with one of my templates, because I had mistakenly named my azd variable the same as a commonly set environment variable, without realizing it, but the value didnt have the same meaning. What if we were instead explicit in main.parameters.json, like: $azdenv:AZURE_OPENAI_KEY And that value could only come from the current azd environment? Then we'd have less accidental variable name collisions, less effects from global env variable tainting, etc. And we could still have ones that are allowed to come from a global env, like $GITHUB_ACTIONS |
Just wondering, how are you currently supporting users that already have an existing For example, if I have
One thing that could help here: if azd encourages/supports a mapping of |
For situations where users start of with a .env file, then I usually just tell them how to update that file after running |
Would the output variables could also be given a standard prefix? If the variable's also get formatted with a specific name then it could easily be regexed against in other spots as well. |
I really don't think using using regex or filters this way when you need to extract multiple values is user-friendly. It might work for our samples which have a small amount of env vars, but in real world scenario you have dozens of vars, and you don't always control their naming as it comes from frameworks and libs requirements. What I hear as feedbacks from customers is that they're looking for more control over what the tooling does (automatically), not more complexity and I think this falls in this use case. What @pamelafox is proposing for avoiding conflict seems more in the right direction, and I would even go further to explicitly "namespace" all input vars, similar to how you do it in GitHub Actions for example:
|
@pamelafox drew my attention to this thread after I mentioned to her I created a small Python azd env loading library, while it doesn't cover the full scope of this discussion, it addresses some of the use cases for Python applications so I figured it might be useful to share here: I personally use that lib to switch environments using |
I like the May I suggest |
We currently have many templates that need access to azd environment variables to be able to run either hooks, scripts, or local dev server.
There are two ways that templates often do that:
Write the full azd env into a .env file, and then load it with a language package like python-dotenv:
azd env get-values > .env
Use shell commands to write the env variables into the environment, and call programs from the shell script:
Why those are bad
Both of these approaches are problematic as they can leak the azd environment variables into the global environment.
For example, the " > .env" approach leaks into the global environment when you're using the Python extension, as that extension (as a default behavior) automatically copies .env variables into the global environment. It took me months to figure out why my global env was getting tainted constantly.
The Powershell code above can also leak into the Windows shell, depending on how the rest of the script issues commands.
It is very bad when the full azd env variables leak into a global environment, since they include AZURE_ENV_NAME, AZURE_LOCATION, AZURE_SUBSCRIPTION_ID. If you then try to switch environments, you will find azd constantly trying to deploy with the values of the old environment. It's very confusing and caused me days of work over the last year trying to figure out what was happening.
Better approaches
I am now taking one of two approaches:
https://github.com/Azure-Samples/azure-openai-keyless-python/pull/7/files#diff-129e0db6b0e28f105813de4b3029d708f8012191253104aaadc5086e69a51aa3
That's not super robust, since it has the constraint that you can't also have those variables as inputs, but it can work for some simple samples.
https://github.com/Azure-Samples/azure-search-openai-demo/pull/1986/files#diff-6099ee740b8b4a7f97ac1e1dfff11776df721ed84c635e510c9aba8f922ca612
That is my current preferred approach, though it has the drawback of feeling a little overly complex for samples that are designed as teaching samples.
EVEN better approaches??
These are related issues and PRs around this issue:
#4078
#4131
#1163
#4067
The text was updated successfully, but these errors were encountered: