fireproofsocks

fireproofsocks

Gigalixir ENV variables not accessable

I’ve been deploying an umbrella app to a Gigalixir instance and mostly it’s been working beautifully. I’ve probably got something misconfigured, but some of the ENV vars are not available at runtime – I did use gigalixir config:set IMPORTANT_URL=foo and I do see the expected values when I run gigalixir config.

However, I’m seeing stuff like "Querying ${IMPORTANT_URL}" in the logs. I did restart the service, but I’m still getting this errors because of this. I confirmed that once I hard-coded the values in my config file (with the values visible from gigalixir config, the errors stopped (!?!?!)

Is there something else we need to be doing when adding ENV vars to our Gigalixir deployments?

Many thanks!

First 8 of 8 Posts Switch mode

hauleth

hauleth

Where you get that string? There is nothing in Elixir that will “magically” replace all occurrences of ${VAR} with environment variable VAR.

jola

jola

EDIT: Originally gave a short answer but I see it was easily misunderstood, so giving a longer answer instead.

If you’re deploying with mix you don’t have access to those magic ${VAR} string replacements. Instead, you’ll want to grab stuff from env by using System.get_env. Since config is evaluated at runtime it will read the system env when you run your app, so you have the same behavior between calling System.get_env in some function and in your config.

If you’re doing Distillery releases things are a bit different. The config is evaluated at compile time and so if you put a System.get_env in your config it will run when you build, not when you start the app. Distillery solves this with config providers or this special built in syntax. If you set REPLACE_OS_VARS in the environment you run your release, any ${VAR}s in your config will be replace at run time. You can still run System.get_env in any code that is evaluated at run time, it will still work as you expect.

If you’re deploying with mix, you can get the env variables with System.get_env but if youre deploying with Distillery you have to set REPLACE_OS_VARS and then it will automatically expand any ${VAR} s you set in your config. This is a feature of Distillery. It wont go through all your code and replace any occurrence of ${VAR} though.

NobbZ

NobbZ

This is only half of the truth.

It is perfectly fine to use System.get_env in your code, whether you use distillery or mix based deployments.

You have to be careful though when you use environment variables in the config/*.exs files, they will only be evaluated at “mix-time”.

Similar caution has to be taken when reading environment variables in module attributes, but REPLACE_OS_VARS wouldn’t help there either, as distillery only hooks into the application environment at the applications boot IIRC.

fireproofsocks

fireproofsocks OP

I think I need to try the System.get_env() calls.

In my various config files, I have calls like

config :my_app, :important_url, "${IMPORTANT_URL}"

I think I’m doing Distillery releases. The ${DATABASE_URL} is getting set and read by my repo, and when I connect to the remote mix console, the configs all show the proper values, but it’s only when I’m actually running something using the other configs that this fails.

I’m not sure what I’m missing, and I’m more confused because the behavior seems inconsistent (why does the DATABASE_URL variable seem to work by my custom IMPORTANT_URL does not? I’ve re-deployed the app several times after setting those variables, doesn’t that mean that the app re-compiled? And why does the remote iex session have access to the variables?

jola

jola

Maybe if you show us some more code. Could you show the code where you read the config? Note that if you would eg get the config value in a module attribute that would happen at compile time and not reflect the run time value.

@important_url Application.get_env(:my_app, :important_url) would not correctly get the run time value of the environment, it would capture what the config value was at compile time (which is "${IMPORTANT_URL}".

fireproofsocks

fireproofsocks OP

Jola, I think you hit the nail on the head! That seems to be exactly what’s going on. Here is some more code (forgive me for not sharing more earlier):

In my config I’m really specifying an API base url and API keys which change between testing/dev/production environments:

In config:

config :my_app, :some_api, %{base_url: "${IMPORTANT_URL}", api_key: "${API_KEY}"}

In my module I am referencing it via a module attribute:

@api_config Application.get_env(:my_app, :some_api)

So can you help me understand how this is being compiled and how I should refactor it? Is that correct that module attributes get resolved at compile time and so that forces the Application.get_env() to get evaluated? Whereas if I were to use the Application.get_env() inside a regular function, it wouldn’t execute until run-time?

jola

jola

That’s correct. Credit to @NobbZ who pointed it out before me.

fireproofsocks

fireproofsocks OP

@NobbZ thank you too for your guidance! I don’t think I would have figured that out for a very long time.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement