仅通过SSH端口(22)测试AWS EC2部署的API的解决方案咨询
Hey there! Since your EC2 security group only allows SSH traffic (port 22) and you need to test the API running at http://ec2xxxxxx:5000/test, here are 3 practical methods you can use right away:
1. Local Port Forwarding (Most Common & Convenient)
This method creates a secure tunnel between your local machine and the EC2 instance, forwarding traffic from a local port to the EC2 instance's port 5000.
Run this command on your local terminal:
ssh -L 8080:localhost:5000 ec2-user@ec2xxxxxxNote: Replace
ec2-userwith your instance's default username (e.g.,ubuntufor Ubuntu AMIs,centosfor CentOS AMIs).Once connected, open your local browser or use
curlto access:curl http://localhost:8080/testYou’ll get the exact response from your EC2-hosted API.
To run the tunnel in the background (so you can keep using your terminal), add the
-fNflags:ssh -fN -L 8080:localhost:5000 ec2-user@ec2xxxxxx
2. Test Directly on the EC2 Instance
If you just need to verify the API is running correctly on the instance itself, you can SSH into the EC2 machine and call the API locally.
First, SSH into your instance:
ssh ec2-user@ec2xxxxxxOnce logged in, use
curlorwgetto hit the API endpoint:curl http://localhost:5000/testThis will show you the API’s response directly from the server, which is great for troubleshooting if the API isn’t responding as expected.
3. Remote Port Forwarding (For Specific Use Cases)
If your local machine has a public IP address, you can forward traffic from the EC2 instance’s port 5000 to a port on your local machine. This is less common but useful if you need others to access the API temporarily without opening security group rules.
Run this command on your local terminal:
ssh -R 5000:localhost:5000 ec2-user@ec2xxxxxxNow you can access the API from your local machine at:
curl http://localhost:5000/testNote: This requires your local machine to be reachable from the EC2 instance, which may involve configuring your home/router firewall to allow incoming traffic on the specified port.
内容的提问来源于stack exchange,提问作者sevenfold

