How to expose local server to public internet
Here’s a step-by-step guide to exposing your local server to your EC2 instance using localtunnel
:
Using localtunnel
localtunnel
provides a simple way to expose your local server to the internet with a public URL.
Steps:
-
Install
localtunnel
globally: First, you need to installlocaltunnel
on your local machine.npm install -g localtunnel
-
Expose Your Local Server: Open a terminal on your local machine and run
localtunnel
to expose your local server.lt --port 2000
This command will start
localtunnel
and provide you with a public URL (e.g.,https://randomsubdomain.loca.lt
) that tunnels to your local server running on port2000
. -
Access from EC2 Instance: You can now use this public URL to access your local server from the EC2 instance or any other external environment.
Example with Node.js Script
If you want to automate the process using Node.js, you can use the localtunnel
package programmatically.
-
Install
localtunnel
:npm install localtunnel
-
Create a Script to Start the Tunnel:
const localtunnel = require('localtunnel'); (async () => { const tunnel = await localtunnel({ port: 2000 }); console.log(`Your local server is now exposed at: ${tunnel.url}`); tunnel.on('close', () => { console.log('Tunnel closed'); }); })();
-
Run the Script:
node your-script.js
Steps to Access the Local Server from EC2 Instance
-
Run the
localtunnel
Command: On your local machine, run thelocaltunnel
command to expose your local server.lt --port 2000
-
Note the Public URL: Take note of the public URL provided by
localtunnel
(e.g.,https://randomsubdomain.loca.lt
). -
Access the Local Server from EC2: On your EC2 instance, you can now access the local server using the public URL provided by
localtunnel
.curl https://randomsubdomain.loca.lt