Port Forward From Local Machine To Remote AWS EC2 Instance

0

Hi,

Scenario: I have a web application running on port 8080 in my local machine (laptop). I want to access this application from a remote EC2 instance in a private Amazon Virtual Private Cloud (VPC) subnet. The security group associated with the EC2 instance allows all traffic within the VPC's CIDR block. I use AWS Session Manager to connect to the remote EC2 instance.

My Goal:

Establish a secure tunnel to forward traffic from port 8080 on your local machine to the same port (8080) on the private EC2 instance. By running curl localhost:8080 on the EC2 instance, you should be able to access the web application hosted on your local machine.

Please advice on how to configure a tunnel to achieve this local-to-remote port forwarding for accessing your local web application from the private EC2 instance?

1 Risposta
1

Install AWS CLI and Session Manager Plugin:

Make sure you have the AWS CLI and Session Manager plugin installed on your local machine.

AWS CLI Installation:

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

session Manager Plugin Installation:

curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"
unzip sessionmanager-bundle.zip
sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin

Start a Session Manager Session with Port Forwarding:

Use the AWS CLI to start a Session Manager session with port forwarding. This command forwards traffic from your local port 8080 to port 8080 on the EC2 instance.

aws ssm start-session \
  --target i-0123456789abcdef0 \
  --document-name AWS-StartPortForwardingSession \
  --parameters '{"portNumber":["8080"],"localPortNumber":["8080"]}'

Verify the Tunnel:

On your EC2 instance, you should now be able to access your local web application by running:

curl http://localhost:8080

Detailed Explanation

AWS CLI and Session Manager Plugin:

These tools are necessary to interact with AWS services and establish the Session Manager session for port forwarding.

Start a Session Manager Session:

The aws ssm start-session command starts a session with port forwarding. The --document-name AWS-StartPortForwardingSession specifies the type of session, and the --parameters option specifies the local and remote ports to forward.

Accessing the Application:

Once the session is established, any traffic to localhost:8080 on the EC2 instance is forwarded through the tunnel to localhost:8080 on your local machine, allowing the EC2 instance to access the web application running on your local machine.

ESPERTO
con risposta 2 mesi fa
  • Hi Thanniru, Thanks for your response. I have tried to do port forwarding from my local machine. It says Cannot perform start session: listen tcp 127.0.0.1:8080: bind: address already in use

    Because there is a service already running in 8080 in my local machine. I want to do a reverse port forwarding from local to remote

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande