dwyl / learn-heroku

:checkered_flag: Learn how to deploy your web application to Heroku from scratch step-by-step in 7 minutes!
153 stars 395 forks source link

Get List of Environment Variables from Heroku #35

Open nelsonic opened 5 years ago

nelsonic commented 5 years ago

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: https://github.com/dwyl/learn-security/issues/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: image

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:

export-heroku-config-vars-script-output

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!

nelsonic commented 5 years ago

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 ... image

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);
nelsonic commented 4 years ago

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