Connect to running app with iex?

We have an elixir app running in prod. I can SSH onto this box, but is it possible to connect to the app with an iex session? When I run ./bin/foobar start_iex I get an error:

Protocol 'inet_tcp': the name foobar@ip-1-2-3-4.ec2.internal seems to be in use by another Erlang node

I know something about --cookie and --sname to connect to a session, but I do not know how the prod app was started. Is it possible to know the cookie or session from bash command line? And is better to SSH onto box or to connect to remote iex session directly from my local computer?

Thanks you!

1 Like

In this case I guess you can simply bin/foobar remote.

start_iex starts the system and attaches iex shell. Since application is already started it won’t allow you start it again with the same name.

And is better to SSH onto box or to connect to remote iex session directly from my local computer?

While remsh might look like a convenient option that allows to run :observer etc… Please take a look at “Erlang’s remsh is dangerous”.

Personally so far I’m satisfied with bin/release_name remote. Plus phoenix_live_dashboard replaces most of the :observer I use.

6 Likes

Thank you, this worked exactly!

And thank you for the security link!

How can I achieve this when I’m not using releases? Specifically, a phoenix app started with mix phx.server by a systemd service.

1 Like

Hi @dodo! and welcome to the community.
One way you could achieve that is:
Modify systemd service so it starts the application with the name (e.g. prod):
elixir --sname prod -S mix phx.server

Now we can connect to it from the same machine with:
iex --remsh prod --sname dev

I’ve never run this way in production, though… not sure if this is idiomatic =)

6 Likes

On Render, I go to the Shell tab and then type in bin/myapp remote, that gets me into the app and I can run any module/code I want.