johanvanzyl

johanvanzyl

Reading a list of strings and a number as config values from environment variable

I’m deploying my Phoenix app with edeliver and distillery and reading config values from environment variables. I can read normal strings just fine, but how do I handle 1.) a list of strings and 2.) numbers?

For 1, the list of strings:
In my config/prod.exs i have allowed_users: ["${ALLOWED_USERS}"],

Here is my sys.config file with the problematic variable:
allowed_users,[<<"${ALLOWED_USERS}">>]},

My environment variable being exported is (i’ve escaped the strings, but it’s not really helping):
export ALLOWED_USERS="\"john\", \"alice\""

When I don’t use an environment variable in my config file and just enter the values directly, then the compiled sys.config looks as follows, so that’s what I need:
allowed_users,[<<"john">>,<<"alice">>],

I can export the following export ALLOWED_USERS="john\">>,<<"alice" as the environment variable and that works, but it feels clunky.

Also, question 2. How can I get a number for a config value that is exported as an environment variable?
My config/prod.exs has this variable enable_salt_workers: "${ENABLE_SALT_WORKERS}", which
i export as export ENABLE_SALT_WORKERS=true, but it ends up as a string in sys.config as follows {enable_salt_workers,<<"true">>}, instead of {enable_salt_workers,true},

Any help would be appreciated.

Marked As Solved

sribe

sribe

Environment variables are always simple strings, nothing more. If you want to encode something more complex into them, then you’ll have to parse them after reading them.

If you have a lot of more complex “stuff” that you need to communicate via environment variables, then you might want to consider a string of JSON as the value…

Also Liked

OvermindDL1

OvermindDL1

This isn’t an Elixir thing, this just just how Environment Variables work regardless of any operating system I’ve seen, they are just a single string, not lists or numbers or anything of the form, hence why parsing is required regardless of the language.

sribe

sribe

Two things to keep in mind:

  1. mix.exs is not some simple template, it’s full-blown Elixir, so you can use the full language for parsing, including defp of some some helper functions, so enable_salt_workers: my_parse_env_list("${ENABLE_SALT_WORKERS}")

  2. All .exs files are run at compile time; so when you pull in env vars in prod.exs, you’re getting the ones on your dev system, not the ones from your production system. To actually use env vars loaded at launch instead of build, you have to read them in elsewhere, when your application is starting up–sounds like you may have already found some of the posts on how to do that.

johanvanzyl

johanvanzyl

Thanks @OvermindDL1, good to know.

dimitarvp

dimitarvp

Sounds like you have outgrown environment variables and you might need to use etcd. Environment variables are awful to store data in. I would not recommend the character escaping hell they will put you through even for basic JSON or a few key-value pairs.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
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
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement