{"id":5417,"date":"2016-05-17T16:02:05","date_gmt":"2016-05-17T19:02:05","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=5417"},"modified":"2016-10-31T16:57:13","modified_gmt":"2016-10-31T18:57:13","slug":"how-to-config-environment-variables-with-elixir-and-exrm","status":"publish","type":"post","link":"http:\/\/blog.plataformatec.com.br\/2016\/05\/how-to-config-environment-variables-with-elixir-and-exrm\/","title":{"rendered":"How to config environment variables with Elixir and Exrm"},"content":{"rendered":"

It’s very common (and highly recommended) that application keeps its configuration values separated from its version control. A way of doing this is by using ENV vars<\/strong> (environment variables). They’re being used for improvements mostly on maintainability. The 12-factor app manifesto explains it on its Configuration section<\/a>:<\/p>\n

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.<\/em><\/p><\/blockquote>\n

In an Elixir project, the config goes in Mix.Config<\/code> files. Some examples are: config.exs<\/code> and environment config files (dev.exs<\/code>, test.exs<\/code> and prod.exs<\/code>). These files are generally used by frameworks and libraries, but they have already proven useful for using mocks in our tests<\/a>.<\/p>\n

Let’s take an Ecto<\/strong><\/a> config as example:<\/p>\n

# config\/dev.exs\nconfig :myapp, MyApp.Repo,\nadapter: Ecto.Adapters.Postgres,\nusername: \"postgres\",\npassword: \"postgres\",\ndatabase: \"myapp_dev\",\nhostname: \"localhost\",\npool_size: 10\n<\/code><\/pre>\n

A well-known approach is using Environment variables<\/strong><\/a> to hide and scope these values through different environments. To use it, we just need to have a configured variable and get it in our application. In Elixir we do this easily with System.get_env(\"ENV_VAR\")<\/code><\/a>.<\/p>\n

We could configure our last example with this approach:<\/p>\n

# config\/dev.exs\nconfig :myapp, MyApp.Repo,\nadapter: Ecto.Adapters.Postgres,\nusername: System.get_env(\"DB_USER\"),\npassword: System.get_env(\"DB_PASSWORD\"),\ndatabase: System.get_env(\"DB_NAME\"),\nhostname: System.get_env(\"DB_HOST\"),\npool_size: 10\n<\/code><\/pre>\n

This way you won’t expose your database configs and will actually make things more dynamic. In development this is useful because the developers won’t need to make changes on this file, they’ll just need to export these vars.<\/p>\n

So far this isn’t much different from what we do in other languages. However, things start to happen differently when we try to generate an Exrm release to deploy our app in production.<\/p>\n

ENV vars need to be present during compile time<\/h2>\n

We all already know that Elixir is a compiled language. And in order to deploy or generate a release we need to compile our application. So everything is compiled, even our config files! Then, there’s an interesting behavior while compiling our config files.<\/p>\n

Our System.get_env()<\/code> calls will be evaluated during the compilation, so the binaries will be generated with the current value of the ENV var. Because of this, we need all of our environment variables to be exported during compiling. When we don’t have them, their value will be nil<\/code> and we won’t be able to connect to our database, for example. This way, to build a release we’d need all our environment variables where we’re building it (our own machine or a build server).<\/p>\n

If we’re working with Phoenix<\/strong>, there is an exception. Phoenix has a special way of configuring an HTTP port with ENV vars that evaluates it during runtime.<\/p>\n

config :myapp, MyApp.Endpoint,\nhttp: [port: {:system, \"PORT\"}],\n# ...\n<\/code><\/pre>\n

It works great and data won’t be fixed in the release, but it’s specific for this Phoenix config<\/a>. But don’t be sad! There are already some mature discussions<\/a> around this in the Exrm repo, take a look, you may be able to help!<\/p>\n

There’s a way when using Exrm release<\/h2>\n

I was chatting around Elixir Slack channel<\/a><\/a> when our friend Ranelli<\/a> mentioned that there was a simple technique that we could use to solve this when we build an Exrm release. Instead of using System.get_env<\/code> in our configs, we must use \"${ENV_VAR}\"<\/code>. Then, we just need to run our release with RELX_REPLACE_OS_VARS=true<\/code>.<\/p>\n

RELX_REPLACE_OS_VARS=true rel\/myapp\/bin\/myapp start<\/code><\/p>\n

This will make our release to use the values represented by these special strings. I’ll explain.<\/p>\n

An Exrm release has two important files: sys.config<\/code> and vm.args<\/code>. These files are responsible by the data used in production (usually what’s in config.exs<\/code> and prod.exs<\/code>) and specific configs that we can make of the Erlang VM respectively.<\/p>\n

sys.config<\/h3>\n
[{sasl,[{errlog_type,error}]},\n{logger,\n[{console,\n[{format,<<\"$time $metadata[$level] $message\\n\">>},\n{metadata,[request_id]}]},\n{level,info}]},\n{myapp,\n[{'Elixir.MyApp.Endpoint',\n[{root,<<\"\/Users\/igorffs\/src\/myapp\">>},\n{render_errors,[{accepts,[<<\"html\">>,<<\"json\">>]}]},\n{pubsub,\n[{name,'Elixir.MyApp.PubSub'},\n{adapter,'Elixir.Phoenix.PubSub.PG2'}]},\n{http,[{port,<<\"${PORT}\">>}]},\n{url,[{host,<<\"localhost\">>}]},\n{cache_static_manifest,<<\"priv\/static\/manifest.json\">>},\n{server,true},\n{secret_key_base,\n<<\"${SECRET_KEYBASE}\">>}]},\n{'Elixir.MyApp.Repo',\n[{adapter,'Elixir.Ecto.Adapters.Postgres'},\n{username,<<\"${DB_USER}\">>},\n{password,<<\"${DB_PASSWORD}\">>},\n{database,<<\"${DB_NAME}\">>},\n{hostname,<<\"localhost\">>},\n{pool_size,10},\n{port,<<\"15432\">>}]}]},\n{phoenix,[{generators,[{migration,true},{binary_id,false}]}]}].\n<\/code><\/pre>\n

vm.args<\/h3>\n
## Name of the node\n-sname myapp\n\n## Cookie for distributed erlang\n-setcookie myapp\n\n## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive\n## (Disabled by default..use with caution!)\n##-heart\n\n## Enable kernel poll and a few async threads\n##+K true\n##+A 5\n\n## Increase number of concurrent ports\/sockets\n##-env ERL_MAX_PORTS 4096\n\n## Tweak GC to run more often\n##-env ERL_FULLSWEEP_AFTER 10\n<\/code><\/pre>\n

Exrm is using a lib called relx<\/strong><\/a> under the hood to build its releases. When we exported RELX_REPLACE_OS_VARS=true<\/code> relx<\/code> will make a replace of the strings by their correspondent ENV var values in the config files.<\/p>\n

{'Elixir.MyApp.Repo',\n[{adapter,'Elixir.Ecto.Adapters.Postgres'},\n{username,<<\"${DB_USER}\">>},\n{password,<<\"${DB_PASSWORD}\">>},\n{database,<<\"${DB_NAME}\">>},\n{hostname,<<\"localhost\">>},\n{pool_size,10},\n{port,<<\"15432\">>}]}\n<\/code><\/pre>\n

You’ve noticed where our special strings are in the sys.config<\/code>, and if you guessed that this process can be done manually, you got it! But this replace really makes things easier for us. Otherwise, we would have to edit every option in the file. It’s very important to mention, if you change those files, you’ll have to reboot your application.<\/p>\n

Considerations<\/h2>\n

This subject is very important if we’re going on production. It concerned us a bit when we’ve noticed that we couldn’t have more dynamic configs. This replacement solution was a relief. Make sure to keep following the discussion I mentioned before<\/a>, things are probably going to change after it.<\/p>\n

Have you already been in trouble dealing with ENV vars? How did you solve it?<\/p>\n


\n\"What's
\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

It’s very common (and highly recommended) that application keeps its configuration values separated from its version control. A way of doing this is by using ENV vars (environment variables). They’re being used for improvements mostly on maintainability. The 12-factor app manifesto explains it on its Configuration section: The twelve-factor app stores config in environment variables … \u00bb<\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[249,230,143,245],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5417"}],"collection":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=5417"}],"version-history":[{"count":16,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5417\/revisions"}],"predecessor-version":[{"id":5776,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5417\/revisions\/5776"}],"wp:attachment":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=5417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=5417"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=5417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}