Hooking up into running heroku phoenix application

Viewed 848

Previous night I was tinkering with Elixir running code on my both machines at home, but when I woke up, I asked myself Can I actually do the same using heroku run command?

I think theoretically it should be entirely possible if setup properly. Obviously heroku run iex --sname name executes and gives me access to shell (without functioning backspace which is irritating) but i haven't accessed my app yet.

Each time I executed the command it gave me different machine. I guess it's how Heroku achieve sandbox. I also was trying to find a way to determine address of my app's machine but haven't got any luck yet.

Can I actually connect with the dyno running the code to evaluate expressions on it like you would do iex -S mix phoenix.server locally ?

3 Answers

It has been a while since you've asked the question, but someone might find this answer valuable, though.

As of 2021 Heroku allows forwarding multiple ports, which allows to remsh into a running ErlangVM node. It depends on how you deploy your application, but in general, you will need to:

  1. Give your node a name and a cookie (i.e. --name "myapp@127.0.0.1" --cookie "secret")
  2. Tell exactly which port a node should bind to, so you know which pot to forward (i.e. --erl "-kernel inet_dist_listen_min 9000 -kernel inet_dist_listen_max 9000")
  3. Forward EPMD and Node ports by running heroku ps:forward 9001:4369,9000
  4. Remsh into your node: ERL_EPMD_PORT=9001 iex --cookie "secret" --name console@127.0.0.1 --remsh "myapp@127.0.0.1"

Eventually you should start your server with something like this (if you are still using Mix tool): MIX_ENV=prod elixir --name "myapp@127.0.0.1" --cookie "secret" --erl "-kernel inet_dist_listen_min 9000 -kernel inet_dist_listen_max 9000" -S mix phx.server --no-halt

If you are using Releases, most of the setup has already been done for you by the Elixir team.

To verify that EPMD port has been forwarded correctly, try running epmd -port 9001 -names. The output should be:

epmd: up and running on port 4369 with data:
name myapp@127.0.0.1 at port 9000

You may follow my notes on how I do it for Dockerized releases (there is a bit more hustle): https://paveltyk.medium.com/elixir-remote-shell-to-a-dockerized-release-on-heroku-cc6b1196c6ad

Related