netlify-lambda: environment context variables do not override build variables
If you define environment variables in the netlify.toml only the build.environment variables are being used.
Eg:
[build.environment]
GREETING="Hello World! I am a variable set in a .env file"
# When building on the production environment this variable will NOT be used.
[context.production.environment]
GREETING="production greeting"
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 46 (20 by maintainers)
Commits related to this issue
- playground-server: fixed env-var bug (see netlify/netlify-lambda#59) — committed to counterfactual/monorepo by joelalejandro 6 years ago
- [playground-server] Adds extra Webpack configuration to build process (#377) * playground-server: passing webpack config to build * playground-server: fixed local routing issues * playground-se... — committed to counterfactual/monorepo by joelalejandro 6 years ago
- [playground-server] Adds extra Webpack configuration to build process (#377) * playground-server: passing webpack config to build * playground-server: fixed local routing issues * playground-se... — committed to counterfactual/dapps-bots by joelalejandro 6 years ago
Hi folks, this is somewhat orthogonal to the bug in
netlify.tomlconfig, but we now support contextual environment variables (including branch-based values) in the Netlify UI, CLI, and API. If you’d prefer to set your env vars with one of those methods, you can move your site over to the new experience today! Hope that helps.In order to access environment variables that are present at build time (such as those you might specify in
netlify.tomlor the special build env variables such as DEPLOY_URL) in a function you can do the following if you’re building your functions withnetlify-lambda.webpack.functions.jswith the following content and simply add the env vars that you need. You could even export the entire build env withnew webpack.EnvironmentPlugin(process.env)but you probably shouldn’t unless you fully understand what that does.What is happening? The issue is that the aforementioned env variables don’t get exported to your function env - they are only present at build time. What we’re basically doing is that we’re replacing instances of
process.env.VARIABLEin the function code at build time where the env variables we want are present. You can read about the environment plugin here.Hi, I see that this issue was closed, but was it resolved? I am having similar issues when trying to use environment variables for
deploy-previewin my lambda functions.never stale
This plugin worked perfectly for me. Thank you for building it an linking it @Elyx0 .
i have left the company and dont maintain this anymore sorry
thanks for your patience 😃 as with everything, the best place is Netlify Support: https://www.netlify.com/support/ - they have the right process and knowledge to help you. in this case, we suspect that there may be a known bug that is in our backlog to solve. in the mean time, Support can work with you to suggest workarounds, and/or notify you when the bug is done. best to take it there.
ok thank you for the detailed repro so i can understand your issue.
so first of all,
netlify-lambdais just a tool for local emulation of functions and building of them for deployment into the main Netlify service. anything main service related as you have shown above, is not at all part ofnetlify-lambda’s responsibility. so this is not actually the right place to resolve this issue.however, we still care that you are seeing this behavior that is unexpected. Basically, what is being set in the UI is overriding the more specific context dependent env variables set from netlify.toml. I agree that this is unintuitive. I’m referring this to our support team, who have probably seen this before. please stay tuned…
Ok, here you go. Let me know if this is enough info to explain what I mean.
I created a lambda function that only returns
Hello <process.env.WHO_AM_I>!. In production it should be “World”, in staging (deploy previews) should be “Developer”.Production: https://netlify-context-in-previews.netlify.com/.netlify/functions/hello should return “Hello World!”
Deploy Preview 1: https://deploy-preview-1--netlify-context-in-previews.netlify.com/.netlify/functions/hello should return “Hello Developer!”
Both return “Hello Build!”, as
WHO_AM_Iwas set to “Build” when creating the app (see image below). My understanding is that it should be overwritten, correct? Am I doing something wrong?Repo: https://github.com/pgarciacamou/netlify-context-in-previews
Netlify App: (notice the ENV variable)