Toml environment variable available to functions

I have been testing environment variables on deploy previews at the moment.

I have a [context.deploy-preview.environment] block in my netlify.toml file with some variables defined. I unfortunately am not able to access those variables in the functions via the process.env (or dotenv package). None of the variables in the toml file are available, but the variables defined via the netlify UI are there.

Is this an intended outcome or have i been making a mistake somewhere?

1 Like

+1 I’m hitting this exact problem. The only environment variables my function picks up are those set through the netlify UI.

In the netlify docs, I found it suspicious that this sentence doesn’t mention that environment variables set in toml are picked up by functions:

Environment variables set in the UI are also accessible in other environments associated with your site, including serverless functions at runtime, snippet injection during post processing, and more.

I’ve tried the following toml settings with no luck:

  • [build.environment]
  • [context.branch-deploy.environment]
  • [context.my-branch-name.environment]

Yet this example online suggests you can…

Only environment variables in the dashboard are available at the time of a function execution in Netlify Functions at the time of this answer. :grin:

@ozdanborne The variable is not used in the function code. The variable in the netlify.toml is used by the build command for a GO build command. See here

2 Likes

Just to verify, are you guys writing your functions and storing them in the directory you specified for your functions, or are you using netlify-lambda to build from another source folder into your designated functions directory?

I’ve been able to get deploy preview environment variables to work just fine for my lambdas, and I suspect it’s because of netlify-lambda which webpacks my output. Since it’s part of the build process, I’m able to use my context-specific variables that get bundled into the output.

Hi @lcfyi

netlify-lambda is a great library, but it’s being deprecated in favor of “zip-it-and-ship-it” where we zip your node_modules in with your function automatically. This already happens automatically. This is how I deploy new apps with functions, but I still have many that still use netlify-lambda to bundle the function.

I wrote netlify-plugin-inline-functions-env-typescript - npm to fix that, check the inlineAll param, it might be what you need.