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.

Last Post!

slogger

slogger

I will look at using mix release
I dont need hot code updates.
Thank you!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement