How to make an HTTP request between two machines of an app?

I’m trying to make two machines communicate between each other.

I created a very simple application with a minimal REST api. A machine can answer with its own machine id.

Then I tried to make one machine send a request to the other machine’s api. I read about private networking, and I thought this isd possible with the id of the machine and the request <machine_id>.vm.<appname>.internal.

So basically, I’m doing a fetch request like this from machineA:

const response = await fetch(`${machineBId}.vm.test-machines.internal/api/machineId`)
const shouldBeMachineBId = await response.text()

But the fetch request fails with an error message “Cannot reach the given URL”.

I also tried with the private IP address of machineB, but same result.

Is there something I’m missing?

The tests have been done in a Bun environment.

Hey @GinQuin

Make sure you are using network port that your app is listening on when making a request (corresponds to internal_port in fly.toml). .internal domain names resolve to machine IP addresses, so the requests go directly to a machine and not via Fly Proxy.

2 Likes

That was it, thank you :smile: I was indeed using a non-conventional port.

I added the internal port to the request and it worked:

await fetch(`${machineBId}.vm.test-machines.internal:${internalPort}/api/machineId`)

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