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

Most upvoted comments

Hi folks, this is somewhat orthogonal to the bug in netlify.toml config, 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.toml or the special build env variables such as DEPLOY_URL) in a function you can do the following if you’re building your functions with netlify-lambda.

  1. Create a custom webpack config file. Let’s call it webpack.functions.js with the following content and simply add the env vars that you need. You could even export the entire build env with new webpack.EnvironmentPlugin(process.env) but you probably shouldn’t unless you fully understand what that does.
// webpack.functions.js
const webpack = require('webpack')
module.exports = {
    plugins: [
        new webpack.EnvironmentPlugin(['VARIABLE1', 'VARIABLE2', ...])
    ]
}
  1. Change your build command to include the config file:
netlify-lambda build --config ./webpack.functions.js functions/
  1. Use process.env like you normally would in your functions.

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.VARIABLE in 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-preview in my lambda functions.

never stale

I built https://www.npmjs.com/package/netlify-plugin-inline-functions-env-typescript not sure it can help

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-lambda is 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 of netlify-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_I was 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

// functions/hello.js
exports.handler = async event => {
  const { WHO_AM_I } = process.env;

  return {
    statusCode: 200,
    body: `Hello ${WHO_AM_I}!`
  };
}
# netlify.toml
[build]
  command = "npm run build"
  functions = ".netlify/functions/"

[context.production.environment]
  WHO_AM_I = "World"

[context.deploy-preview.environment]
  WHO_AM_I = "Developer"

Netlify App: (notice the ENV variable)

Screen Shot 2019-03-27 at 4 06 32 PM