You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仅通过SSH端口(22)测试AWS EC2部署的API的解决方案咨询

Testing Your EC2 API via Only SSH Port (22)

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@ec2xxxxxx
    

    Note: Replace ec2-user with your instance's default username (e.g., ubuntu for Ubuntu AMIs, centos for CentOS AMIs).

  • Once connected, open your local browser or use curl to access:

    curl http://localhost:8080/test
    

    You’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 -fN flags:

    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@ec2xxxxxx
    
  • Once logged in, use curl or wget to hit the API endpoint:

    curl http://localhost:5000/test
    

    This 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@ec2xxxxxx
    
  • Now you can access the API from your local machine at:

    curl http://localhost:5000/test
    

    Note: 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:49:29