Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Heroku environment variable without restarting app

Is it possible to set a Heroku environment variable without restarting the app?

My app connects out to different online services via OAuth2. For each service I connect to, I need to set an OAuth2 ID and secret. To keep these configuration variables outside of my code, I'm using environment variables, and reading them in on process.env (node.js).

Each time I add a new service to my app, I need to add the corresponding environment variables for the ID and secret. I need to do this before pushing the latest code, so that when the app next starts up with the new service connection, the OAuth2 ID and secret variables are available.

Currently my workflow is as follows:

  1. Set the environment variables using the Heroku toolbelt: heroku config:set <SERVICE>_ID=foo <SERVICE>_SECRET=bar
  2. Push the latest code: git push heroku master

Currently, both of these operations will restart the app. I'd really prefer the first operation to not restart the app, as the changes to these config vars don't need to take effect until step 2). By restarting at step 1) my app will experience unnecessary downtime.

So, is there any way to prevent step 1) from restarting the app?

like image 290
Tom Spencer Avatar asked Jan 12 '16 08:01

Tom Spencer


People also ask

Should I push .env to Heroku?

env file on Heroku isn't a good approach. Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable. But for testing these config vars in local, we need to set up a .

Is Heroku config vars safe?

Heroku config vars are designed to be safe for storing sensitive information. All config vars are stored in an encrypted form and safely stored. These are only decrypted and loaded when booting your app in a dyno itself.

Can Heroku access environment variables?

Accessing config var values from code Config vars are exposed to your app's code as environment variables. For example, in Node. js you can access your app's DATABASE_URL config var with process.

What is the .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.


2 Answers

According to this article it pretty explicitly states that

Whenever you set or remove a config var, your app will be restarted.

Personally I also wish there was a way to do what you're asking. On larger apps, a system-wide hard restart can be painful when you have many process types running. Many times I set environment variables that aren't crucial for the app to grab ahold of immediately, such as that involving future functionality, or settings that are OK having the old value but you want the new value to take effect in a rolling-restart fashion.

like image 183
Brian Nguyen Avatar answered Oct 18 '22 19:10

Brian Nguyen


At the present, is not possible to avoid the app restart. But you can use the command heroku config:edit to edit your env at once or even paste a new env set, avoiding many restarts.

In according to the heroku config help:

(...)

COMMANDS
  config:edit   interactively edit config vars
  config:get    display a single config value for an app
  config:set    set one or more config vars
  config:unset  unset one or more config vars

So you can run

heroku config:edit

Additionally, you might want to take a look on this issue (proposal): https://github.com/heroku/cli/issues/1570

like image 44
Felippe Regazio Avatar answered Oct 18 '22 18:10

Felippe Regazio