Hopefully this saves someone some time (might be good to add the official documentation because I fell for this once in January and again today - although I realized faster why things weren’t working).
Question:
I overwrite some of my environment variables in my netlify.toml file, for my staging environment deploys. However, I’ve noticed that these overwrites only apply to the site, they don’t apply to my staging branch lambda functions. Is this a bug?
Support Answer (see update below, couldn’t get this to work):
It’s an open feature request to support environment variables from the toml file in functions.
In the meantime you may be able to leverage different build commands for deploy contexts per the TOML file to achieve your goal:
- set both variables in our UI, say “API_BACKEND_STAGE = https://stage.com” and “API_BACKEND_PROD = https://prod.com”
- then create conditional build commands for each branch like this:
[context.production] command = "export API_BACKEND=$API_BACKEND_PROD && npm run build" [context.stage] command = "export API_BACKEND=$API_BACKEND_STAGE && npm run build"
and use only plain $API_BACKEND in your build
Update: I tried the above, where API_BACKEND_STAGE and API_BACKEND_PROD are both set in the Netlify UI, but API_BACKEND is not available as an environment variable when my function executes. So it does not work. Will be looking for alternative solutions.
This is related to the discussion here: https://github.com/netlify/netlify-lambda/issues/59#issuecomment-477383840 They’ve created an example to reproduce the behavior.
Update: I tried eval
instead of export
in the example above, and API_BACKEND is still not available in process.env when my function executes. I also tried both export
and eval
API_BACKEND=Test
just to see if I could hard code something, but that also failed, when the staging function executes it does not have API_BACKEND in process.env