How to set environment variables for my app

I have some environment variables that I’m using in my app AUTH0_DOMAIN and AUTH0_AUDIENCE which are consumed in my phoenix app’s config/config.exs file, like so.

config :auth0_jwks, iss: System.get_env("AUTH0_DOMAIN"),
  aud: System.get_env("AUTH0_AUDIENCE")

The question is how can I simply set those environment variables for my app? Heres what I’ve tried so far with zero success.

  • Setting the env vars in the --env argument when deploying the application.
  • Setting the env vars under the [env] config in the fly.toml.
  • Setting the env vars in an .env file which isn’t ignored my the dockerignore file.

I’m possibly missing something fundamental but I can’t see it at the moment, does anyone have any ideas of what I might be missing?

If you ssh into the machine and do ‘env’ in the terminal, can you see those variables?

➜ app git:(main) ✗ fly ssh console -C “printenv”
Connecting to fdaa:2:7f9d:a7b:13e:440a:5618:2… complete
AUTH0_AUDIENCE=https://xxx-sunset-733.fly.dev
AUTH0_DOMAIN=https://xxx.eu.auth0.com/

so they do exist but the app errors as if they are not set.

Can we look into the app errors?

So this is the error which happens when the app is starting (deploying) so this is what happens when I try and deploy using System.get_env/1 to accesss the environment variables in the config - I can only see the env vars with printenv if I statically set those values and then deploy.

This error happens locally for me too when those environments arn’t set for example when running iex -S mix phx.server without those envs set - which is why I’m thinking those envs arn’t set because its the same error.

2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]    ** (EXIT) an exception was raised:
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]        ** (Auth0Jwks.Config.MissingValueError) Auth0 config [:auth0_jwks, :iss] is not set
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (auth0_jwks 0.2.0) lib/config.ex:9: Auth0Jwks.Config.get_config_or_error/2
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (auth0_jwks 0.2.0) lib/config.ex:28: Auth0Jwks.Config.jwks_url/0
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (auth0_jwks 0.2.0) lib/strategy.ex:3: Auth0Jwks.Strategy.init_opts/1
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (auth0_jwks 0.2.0) lib/strategy.ex:2: Auth0Jwks.Strategy.start_link/1
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (stdlib 5.0.2) supervisor.erl:420: :supervisor.do_start_child_i/3
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (stdlib 5.0.2) supervisor.erl:406: :supervisor.do_start_child/2
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (stdlib 5.0.2) supervisor.erl:390: anonymous fn/3 in :supervisor.start_children/2
2023-09-02T16:45:48Z app[2874365a619dd8] lhr [info]            (stdlib 5.0.2) supervisor.erl:1256: :supervisor.children_map/4
2023-09-02T16:45:49Z app[2874365a619dd8] lhr [info] WARN Reaped child process with pid: 314 and signal: SIGUSR1, core dumped? false
2023-09-02T16:45:49Z app[2874365a619dd8] lhr [info] WARN Reaped child process with pid: 316 and signal: SIGUSR1, core dumped? false
2023-09-02T16:45:50Z app[2874365a619dd8] lhr [info]Kernel pid terminated (application_controller) ({application_start_failure,rock_map,{{shutdown,{failed_to_start_child,'Elixir.Auth0Jwks.Strategy',{'EXIT',{#{message => <<"Auth0 config [:auth0_jwks, :iss] is not set">>,'__struct__' => 'Elixir.Auth0Jwks.Config.MissingValueError','__exception__' => true},[{'Elixir.Auth0Jwks.Config',get_config_or_error,2,[{file,"lib/config.ex"},{line,9}]},{'Elixir.Auth0Jwks.Config',jwks_url,0,[{file,"lib/config.ex"},{line,28}]},{'Elixir.Auth0Jwks.Strategy',init_opts,1,[{file,"lib/strategy.ex"},{line,3}]},{'Elixir.Auth0Jwks.Strategy',start_link,1,[{file,"lib/strategy.ex"},{line,2}]},{supervisor,do_start_child_i,3,[{file,"supervisor.erl"},{line,420}]},{supervisor,do_start_child,2,[{file,"supervisor.erl"},{line,406}]},{supervisor,'-start_children/2-fun-0-',3,[{file,"supervisor.erl"},{line,390}]},{supervisor,children_map,4,[{file,"supervisor.erl"},{line,1256}]}]}}}},{'Elixir.RockMap.Application',start,[normal,[]]}}})

Is this exception raised during fly deploy?

Correct, this exception happens when I run fly deploy (which I run also with --remote-only).

Can you try --build-arg instead of --env?

Sadly I still get the same error.

fly deploy --remote-only --build-arg AUTH0_DOMAIN="https://xxx.eu.auth0.com/" --build-arg AUTH0_AUDIENCE="https://xxx-sunset-733.fly.dev"

I will work on a reproduction
and see what I learn until a Fly.io employee gets to this.

1 Like

Okay I’ve fixed the issue by moving the config from config.exs to runtime.exs.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.