slogger

slogger

Understanding environment variables and Elixir

Hello Community!

My team has recently started using Elixir and Phoenix.
Im responsible for deploying our applications.
My main experience so far is with Javascript and Golang. I only have started working with Elixir in the last few weeks.

Im struggling to understand how environment variables are supposed to work in Elixir.
Im using Distillery to create my releases and am deploying them with Docker.

After much research (Googling!) it seems that config/runtime.exs is the place to put my environment variables.
Here is my runtime.exs

import Config

config :wb_iot,
  ecto_repos: [WB.IOT.Persistence.Repo]

config :wb_iot, WB.IOT.Persistence.Repo,
  database: System.get_env("DATABASE_NAME", "wb_iot"),
  username: System.get_env("DATABASE_USERNAME", "postgres"),
  password: System.get_env("DATABASE_PASSWORD", "postgres"),
  hostname: System.get_env("DATABASE_HOST_NAME", "localhost"),
  port: String.to_integer(System.get_env("DATABASE_PORT", "5432")),
  pool_size: 10,
  migration_timestamps: [type: :utc_datetime_usec]

config :broadway_kafka,
  hosts: System.get_env("KAFKA_HOSTS", "localhost:9092"),
  group_id: System.get_env("KAFKA_GROUP_ID", "wb_iot"),
  topics: [System.get_env("KAFKA_TOPICS", "message")]

config :redix,
  host: System.get_env("REDIS_HOST", "localhost"),
  port: String.to_integer(System.get_env("REDIS_PORT", "6379"))

I am accessing them in the modules as follows:

Application.get_env(:broadway_kafka, :hosts)

I then build my Docker image which completes successfully.
It doesnt seem like my environment variables are being picked up - even the defaults! - as the app crashes with the values being nil.

application_start_failure,wb_iot,{{shutdown,{failed_to_start_child,‘Elixir.Redix’,{‘EXIT’,{#{‘exception’ => true,‘struct’ => ‘Elixir.ArgumentError’,message => <<"expected an integer as the value of the :port option, got: nil">>}

Ive tried multiple variations, but I cant seem to get this to work.
Does Elixir manage this “per environment” config differently?

Thank you!

Marked As Solved

LostKobrakai

LostKobrakai

I’m not sure Distillery even supports runtime.exs. At least the error you showed doesn’t match the code. String.to_integer can never return nil. I’d suggest using mix release unless you actually need hot code updates, which is imo the only reason one would be using distillery for today.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement