-
Notifications
You must be signed in to change notification settings - Fork 9
config vars
config-vars
コードベースのソースは本番環境、ステージング環境、各開発者のローカル環境など多数の環境にdeployされる可能性があります。オープンソースのコードの場合数百〜数千の環境が存在する可能性があります。
すべて同じコードで開発していても、各環境は固有の構成を持っています。例えば、Amazon S3などの外部サービスの認証情報がそれにあたります。ステージングサイトと本番サイトがそれぞれ独自のキーを持っていながら、開発者は、1つのS3のアカウントを共有することがあります。
伝統的なアプローチは、それらの変数を記述したファイルをソースコードに含まることです。これは、不都合が発生しやすく、特に頻繁に別ブランチにおけるアプリケーションの環境を構築する必要があるようなオープンソースのアプリケーションにおいては顕著です。
より良い解決策は、環境変数を使用して、コードのキーを維持することです。従来のホスト上、またはローカルの作業、あなたのbashrcファイルの
環境のvarsを設定することができます。 Herokuでは、環境変数の内容を使用しています。
deployしたアプリケーションに環境変数を設定する方法を示します。
Herokuコマンドラインでconfig
, config:add
, and config:remove
といったコマンドを利用することで、環境変数を管理することが出来ます。
:::term
$ heroku config:add GITHUB_USERNAME=joesmith
Adding config vars and restarting app... done, v12
GITHUB_USERNAME => joesmith
$ heroku config
GITHUB_USERNAME => joesmith
OTHER_VAR => production
Herokuは、アプリケーションに環境変数としてこれらを設定します。これらの環境変数は永続的です - それらはdeployし、アプリを再起動しても残ります - よって、値を変更する必要がない限り、それらは一度だけ設定すれば良いです。
S3アカウントのキーを環境変数へ追加する例:
:::term
$ cd myapp
$ heroku config:add S3_KEY=8N029N81 S3_SECRET=9s83109d3+583493190
Adding config vars:
S3_KEY => 8N029N81
S3_SECRET => 9s83109d3+583493190
Restarting app...done.
実行時に環境変数の内容を読み取るようにコードを設定します。たとえば、Rubyでは、 ENV['KEY']
といったように環境変数へアクセスします。 - 以下のように初期化の処理を書くことができます。
:::ruby
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
)
Javaでは、System.getenv('key')
でアクセス可能です。:
:::java
S3Handler = new S3Handler(System.getenv("S3_KEY"), System.getenv("S3_SECRET"))
Python:
:::python
s3 = S3Client(os.environ['S3_KEY'], os.environ['S3_SECRET'])
Herokuにdeployしたアプリケーションは環境変数に設定されたキーを使うようになります。
上記の例では、アプリケーションは開発者の環境またはHerokuの以外の他のホスト上ではS3のキーはnil
に設定されます。典型的には局所的に異なるサービスを使用することができます。
たとえば、deployされたアプリケーションではDATABASE_URL
はHeroku上のPostgreSQLを参照しますが、ローカルアプリケーションでは、ローカルのストレージを参照することでしょう。
Foreman has the advantage of letting you select a different environment file, or even multiple files, at launch: foreman -e alternate_env start
Where should you put your local environment variables? One solution is to place them in a .env
file and use Foreman(installed with the Heroku Toolbelt) to start your application. Foreman will then ensure the variables are set, before starting up the processes specified in your Procfile
. Here's a .env
:
S3_KEY=mykey
S3_SECRET=mysecret
Now start the application:
:::term
foreman start
When using this approach, add your environment files to .gitignore
.
heroku-config is a plugin for the Heroku CLI that makes it easy to grab your application's config vars, and place them in your local .env
, and vice versa.
Install it:
:::term
$ heroku plugins:install git://github.com/ddollar/heroku-config.git
heroku-config installed
Interactively pull your config vars, prompting for each value to be overwritten in your local file:
:::term
$ heroku config:pull --overwrite --interactive
Push a local environment file to Heroku:
Pushing a local environment will overwrite the environment in your Heroku application. Take care when performing this command.
:::term
$ heroku config:push
A less useful alternative to using Foreman's .env
file is to set these values in the ~/.bashrc
for the user:
export S3_KEY=mykey
export S3_SECRET=mysecret
Or, specify them when running the application (or any other command) by prepending the shell command:
:::term
$ S3_KEY=mykey S3_SECRET=mysecret application
Many languages and frameworks support a development mode. This typically enables more debugging, as well as dynamic reloading or recompilation of changed source files.
For example, in a Ruby environment you could set a RACK_ENV
config var to development
to enable such a mode.
It's important to understand and keep track of these config vars on a production Heroku app. While a development mode is typically great for development, it's not so great for production - leading to a degradation of performance.