Get List of Environment Variables from Heroku
Opened this issue ยท 2 comments
Story
Getting an app running on localhost
should be easy for new team members,
(ideally it should be a single command ...) but sadly, currently it's not! ๐
@dwyl we have apps deployed to a variety of infrastructure, see: dwyl/learn-devops
Most of the time we recommend that people start with Heroku because the convenience and much simpler interface far outweighs the cost (and switching away is relatively easy!)
One of the most tedious steps in getting an app running on localhost
is getting the Environment Variables. We don't want to share environment variables through insecure means (some people share secrets through Chat message, Google Docs or even email, but none of these means of communication are truly secure ... all store your data somewhere that is not in your direct control so it's possible for keys to leak ...)
We need to a bulletproof way of storing application secrets for our own apps.
See: dwyl/learn-security#43
But for now, using Heroku as the "single source of truth" for Secrets (Environment Variables) is a good starting point for the apps we have deployed on Heroku.
The Manual Way
First visit the "Settings" tab for your Heroku App.
And click on the "Reveal Config Vars" button:
Then Run this script in your web browser's developer console:
var keys = document.getElementsByClassName('config-var-key');
var vals = document.getElementsByClassName('config-var-value');
var vars = '';
for (var i = 0; i < keys.length - 1; i++) {
var index = (i == 0) ? 0 : i * 2; // cause there are two values for every key ... ๐
vars = vars + 'export ' + keys[i].value + '=' + vals[index].value + '\n';
}
console.log(vars);
You should see something like this:
Copy the output from the browser console and paste it into your .env
file.
export AWS_ACCESS_KEY_ID=AKIA****************
export AWS_S3_BUCKET=bucket-name
export AWS_S3_REGION=eu-west-1
export AWS_SECRET_ACCESS_KEY=****************
export DATABASE_URL=postgres://databasename@compute.amazonaws.com:5432/password
export ENCRYPTION_KEYS='****************='
export GOOGLE_MAPS_API_KEY=****************
export HEROKU_POSTGRESQL=postgres://databasename@compute.amazonaws.com:5432/password
export IMPORT_FILES_DIR=temp
export SECRET_KEY_BASE=****************
export SENDERS_EMAIL=hello@example.com
export SES_PORT=25
export SES_SERVER=email-smtp.eu-west-1.amazonaws.com
export SITE_URL=https://www.example.com
export SMTP_PASSWORD=****************
export SMTP_USERNAME=AKIA****************
export URL=your-app.herokuapp.com
Todo: Automate This!
- Use Heroku Toolbelt (CLI) #36 to automatically export the Environment Variables
from the "Staging" App tolocalhost
for club-soda/club-soda-guide#512
Start Here: https://devcenter.heroku.com/articles/heroku-cli
I've just used this and realised that the DATABASE_URL
and HEROKU_POSTGRESQL
and are not needed on localhost
and attempting to connect to them will result in a permissions error ...
therefore a revised script to extract only the variables that are useful on localhost
is:
var keys = document.getElementsByClassName('config-var-key');
var vals = document.getElementsByClassName('config-var-value');
var vars = '';
for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i].value
if (key && key !== 'DATABASE_URL' && key !== 'HEROKU_POSTGRESQL') {
var index = (i == 0) ? 0 : i * 2; // cause there are two values for every key ... ๐
vars = vars + 'export ' + keys[i].value + '=' + vals[index].value + '\n';
}
}
console.log(vars);
Script to copy environment variables from one Heroku App to another:
#!/bin/bash
set -e
sourceApp="$1"
targetApp="$2"
while read key value; do
key=${key%%:}
echo "Setting $key=$value"
heroku config:set "$key=$value" --app "$targetApp"
done < <(heroku config --app "$sourceApp" | sed -e '1d')
via: http://blog.nonuby.com/blog/2012/07/05/copying-env-vars-from-one-heroku-app-to-another