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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement