Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment Variables on Heroku and Mailgun Problems with Phoenix Framework

I was following this guide on deploying to Heroku and this one for sending email.

Everything works fine in development. My variables are set in Heroku:

heroku config
...
MAILGUN_DOMAIN:  https://api.mailgun.net/v3/xxxxxx.mailgun.org
MAILGUN_KEY:     key-3-xxxxxx
...

And loaded from the config files like so:

config :take_two, Mailer,
    domain: System.get_env("MAILGUN_DOMAIN"),
    key: System.get_env("MAILGUN_KEY")

However when I try to send email on Heroku when the Mailgun config is set from environment variables I get this error:

** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
    (elixir) lib/io.ex:346: IO.chardata_to_string(nil)
    (elixir) lib/path.ex:467: Path.join/2
    (elixir) lib/path.ex:449: Path.join/1
             lib/client.ex:44: Mailgun.Client.send_without_attachments/2

This happens when the domain is not set for the Mailgun Client. But it is supposed to be set from the environment variable. I made a simple module to test:

defmodule TakeTwo.Mailer do
    require Logger

    use Mailgun.Client,
        Application.get_env(:take_two, Mailer) 

    def blank_shot do
        Logger.info Application.get_env(:take_two, Mailer)[:domain]
        Logger.info Application.get_env(:take_two, Mailer)[:key]
        send_email from: "[email protected]", to: "[email protected]", subject: "Hello", text: "This is a blank shot"
    end

When I run TakeTwo.Mailer.blank_shot I see the correct domain/key variables logged followed by the error. I am not sure how to debug the Mailgun client remotely.

Finally, if I recreate the above module in the shell (after running heroku run iex -S mix) it works just fine!?

I feel like when the original module is being loaded perhaps the environment variables have yet to be loaded??

like image 505
speg Avatar asked Feb 10 '16 04:02

speg


People also ask

Are Heroku environment variables secure?

IMO both – environment variables and config files – are secure as long you can trust everyone that has access to your servers and you carefully reviewed the source code of all libraries and gems you have bundled with your app.

How do I access Heroku config vars in Python?

To access your environment variables which you stored in Heroku in Python, you only need to install the os module. Then, the function to access to the environment variables is os. getenv("[key_name]") (replacing [key_name] with the key name). You've done it!


2 Answers

The answer was a little buried in a comment so I wanted to make it easier to find. As the other answer mentions, the environment variables aren't available, but the buildpack lets you configure them to be:

I created a elixir_buildpack.config file and added the following:

config_vars_to_export=(DATABASE_URL MAILGUN_DOMAIN MAILGUN_KEY SECRET_KEY_BASE)
like image 123
Peter Brown Avatar answered Sep 17 '22 15:09

Peter Brown


The environment variables aren't available at build time. I had the same issue and decided to get rid of the macro carrying the configuration. You can use this patch to move on.

like image 20
Ragnar Lodbrok Avatar answered Sep 20 '22 15:09

Ragnar Lodbrok