Until very recently, my project had been using netlify-lambda
to serve (locally) and build (for production) our functions - functions that do a few different jobs, from auth to sitemap delivery.
All of the relevant environment variables were managed by dotenv
locally, and by the netlify.toml
for production configuration.
In an effort to drag our repo into the present day, I’ve migrated to netlify-cli (ie, using netlify functions:serve
and netlify functions:build
. Now, it’s made very clear in the docs that functions don’t have access to the env vars defined in netlify.toml, so I guess my questions are:
- They were available to the functions before, right?
- Why has that changed? It was previously rather convenient to be able to set bespoke (and often rather ephemeral) env vars according to build context using the .toml file, but now there has to be a load of floating values set in the Netlify web console which become stale and need further housekeeping when deploy contexts cease to be relevant.
It’s not a massive issue but I would like to better understand how the newer configuration works - for example, the build output is now zip files, rather than uncompressed files/folders (I realise this is something that changed years ago, but I’m only coming to it all now!)
netlify-lambda
has been long lost (I believe even before I joined Netlify which was more than 4 years ago), so I really don’t know if the environment variables ever worked with that package.
When using CLI, for the most part, you don’t need functions:serve
and functions:build
- those commands have a very specific use-case. Most users simply need netlify dev
which will run a dev server of their website along with Netlify Functions, Edge Functions, etc. The dev
command does inject environment variables from netlify.toml
.
As for building a production output, you should run netlify build
or if you wish to deploy, you should run netlify deploy
(no need to run a separate build anymore, Release v21.0.0 · netlify/cli runs the build for you). However, this won’t inject the variabes. Only the ones in the UI would be used when the site runs in production.
@hrishikesh thanks for the reply - yeah we first implemented this in 2020, so it’s long overdue a refit!
I suppose my use case must be unusual, but I found it convenient to have deploy-specific (and not just deploy context-specific) env vars available to the build (eg, a different AWS user pool for a specific test branch).
I’ll no doubt look again at netlify dev
sooner or later - I had originally avoided it as I’m using older Vue2 framework with a lot of custom config on the local dev build, which I didn’t fancy having to migrate…!