Environment variable per branch for lambda functions

Hello folks

I want to maintain a development and production instance of my application.
I’m using master and develop branches to do this.
I’ve configured the netlify.toml file like so

[context.production.environment]
A = ‘A’
B= ‘B’
[context.develop.environment]
A = ‘x’
B=‘z’

this is working fine for the react application, however, the functions on the develop branch are still using the environment variables from [context.production.environment]. Is there a different way to update environment variables on develop instance?

Hi @skippednote, at the moment the only environment variables that are available in to Netlify Functions are those you specify in the netlify UI. The ones you have in the netlify.toml won’t be used in your function. We have an open issue to get this correct. Sorry about that!

If you want to dynamically inject variables in to your function then you’ll need to use webpack or write your own script or another library that runs during the build process to interpolate or inject the variables.

Hi,

Is this still valid? This seems like a huge gap in the functionality. I seem to having the same issue.

Yup, that is still the case. I think you can still use branch specific environment variables that you set in the UI - you’d just need to write your function code using a pattern like this:

  1. request received
  2. check $CONTEXT to figure out how it was called (your options will be: production, branch-deploy (if branch other than production), deploy-preview if your PR is targeting some branch other than production one))
  3. use the corresponding variable you’ve pre-set based on logic during execution:

$PRODUCTION_API_ENDPOINT=“https://api.com/
$BRANCH_API_ENDPOINT=“https://stagingapi.com/
$DEPLOY_PREVIEW_API_ENDPOINT=“https://testapi.com/

Since you’ve pre-set all of those in the UI, they’ll all be available in every context, so you can swap back and forth based on the request.

Yes, this is a workaround and not the best experience, but it should work already today.

We’ll still follow up in this thread when our system’s behavior changes to what you’re expecting/hoping for! :slight_smile:

Hey, thanks for the reply. I hope you do add this functionality soon! One thing I would make note of, is that in the UI it should be clear whether the variables should apply to functions or just the FE. This is important to prevent accidentally switching environments in the case of split testing.

It would also be helpful to make this lack of functionality clear in netlify docs, as it’s very misleading that the variables set in netlify.toml are picked up at build time but not at run time.

For anyone else finding the thread, another way to work around this issue is:

You can use a combination of

A) https://github.com/cball/netlify-plugin-contextual-env to automatically replace env variables based on prefix/suffix.

B) either the Babel plugin https://github.com/jordansexton/babel-plugin-transform-inline-environment-variables or if you only have files in the functions folder https://github.com/bencao/netlify-plugin-inline-functions-env

I think this is better because we don’t modify our code explicitly.

Thanks so much for the suggestions! We’ll review with our documentation team later this week and try to improve.

Our build plugins team did point out this third party plugin that in effect “exports” your UI variables to functions:

This is not written or supported by Netlify, but it may be a better workaround than the one I presented for your use. I’m not sure why there are two authors from seemingly similar plugins, but I guess you can evaluate them to see which one you prefer if you’ll experiment like that :slight_smile:

Thanks @fool, yeah mentioned the plugin in the post above (also contributed to it!)

1 Like

Thank you for your work on the plugin and for taking the time to share the potential solutions for others - this is what helps our community thrive!

1 Like

Hi,

I’m trying to set environment variables in my netlify functions based on the deploy context. For example, I want to use an env var named PRODUCTION_ACCESS_TOKEN on the master branch, and SANDBOX_ACCESS_TOKEN on my dev branch. I’ve tried many solutions to this problem, described below, but have been unsuccessful so far.

I’ve seen a solution about checking $CONTEXT in the netlify function posted by @fool multiple times, but I have yet to get anything like it to work. I have tried accessing process.env.CONTEXT from my vue app build, but it’s undefined when running locally with netlify dev, and also after it’s deployed on netlify. I’ve also tried accessing process.env.CONTEXT from my netlify functions. It’s undefined there too, both when I’m running locally with netlify dev, and also after deploy.

Here’s my function code:

exports.handler = async (event, context) => {
  try {
    return {
      statusCode: 200,
      body: JSON.stringify({
        context: process.env.CONTEXT,  // undefined
      }),
    };
  } catch (err) {
    return { statusCode: 500, body: err.toString() };
  }
};

This post in netlify guides and tutorials, which describes how to build a plugin very similar to netlify-plugin-contextual-env, seems to imply that accessing process.env.CONTEXT should be possible in netlify functions, but it doesn’t work for me as described.

Using a combination of netlify-plugin-contextual-env and netlify-plugin-inline-functions-env, as described by @edamame, works locally if I run netlify build and then netlify dev, but not when using netlify dev alone, which is what I need. If I run the netlify build command first, my functions are re-written locally with my environment variables inlined (as intended by netlify-plugin-inline-functions-env), but I can’t commit these re-written functions to source control because the environment variables that I’ve defined in the netlify UI contain secrets.

My takeaway is that these plugin solutions are not very reliable, and I would love to see this functionality added to netlify natively.

$CONTEXT is not set in the UI, thus not available per the workaround discussed above. I apologize since I did not realize that when I posted my initial workaround. I think you’ll need a workflow more like this:

  1. Instead of trying to use the environment variable, use its value
  2. You can save the value during build using a pattern like the one shown here (literally for $CONTEXT): [Support Guide] Using environment variables on Netlify correctly
  3. You can do that across many files, using a search-and-replace setup something like sed -i s/PLACEHOLDER/${CONTEXT}/g function-source/*/*.js

Just to understand as I’m facing the same challenge: Wouldn’t it be possible to at least provide CONTEXT for Netlify functions in process.env? Reading through questions here I think this would make sense for more customers.

Yup, it would be possible, but it might break people’s builds - there are folks who have very carefully tuned their builds to send the maximum amount of data via environment variables and when we add one more it would break.

So, we probably won’t do it too soon since one of our top priorities on builds is not to break existing builds without customer changes. Since you know the workaround, you can use it though :wink:

There is an open feature request on the topic, though - it’s not a bad idea, just hard to implement - so we have recorded your interest and will follow up here in this thread in case the behavior does change in some way, which should send you a notification.

1 Like