- Newest
- Most votes
- Most comments
I am not running on a Ubuntu machine as I said in the post but on a Amazon Linux 2023. Tanvir's answer solved my issue: https://repost.aws/questions/QUD9AaDnbvRhK8EQ9tT2w-YA/cannot-connect-to-tcp-port-with-curl-command#COXoS9nXM0TzuA5-2c5ifong
Answer: You don’t need to restart your EC2 instance for security group or network ACL changes to take effect—they apply immediately. However, since you're using Amazon Linux 2023, here’s how you can check the firewall and allow traffic on ports 5000 and 8000:
Check and Modify Firewall Rules (Amazon Linux 2023) Amazon Linux 2023 uses nftables instead of ufw or iptables. Run the following command to check if any firewall rules are blocking the traffic: sudo nft list ruleset If necessary, allow traffic on the required ports:
sudo firewall-cmd --permanent --add-port=5000/tcp sudo firewall-cmd --permanent --add-port=8000/tcp sudo firewall-cmd --reload
Check if the rules were applied:
sudo firewall-cmd --list-all
Verify Flask is Running and Listening on 0.0.0.0 After making these changes, confirm that your Flask application is correctly listening on all interfaces: sudo ss -tulnp | grep 5000
You should see something like:
LISTEN 0 128 0.0.0.0:5000 0.0.0.0:* users:(("python",pid,fd)) If Flask is not listening on 0.0.0.0, restart your Flask app.
Retest Access Try accessing your Flask API externally: curl http://<your_aws_public_ip>:5000
If it still doesn’t work, try stopping firewalld temporarily to see if it's causing the issue:
sudo systemctl stop firewalld
If stopping firewalld fixes the issue, you’ll need to permanently configure it to allow Flask traffic instead
Troubleshooting Flask API Accessibility in AWS
If your Flask API is not accessible from outside your AWS instance, but it works locally (e.g., with curl on localhost), there are a few things you can check. Below are steps to troubleshoot and fix the issue:
1. Check Flask Binding
By default, Flask binds to 127.0.0.1, which means it only accepts requests from localhost. To allow external access, you need to bind it to 0.0.0.0.
In your Flask app, modify the run method:
app.run(host='0.0.0.0', port=5000)
This will allow the app to accept connections from any IP address.
2. Check Security Group
Ensure that your AWS EC2 security group allows inbound traffic on port 5000.
- Go to your EC2 console.
- Select your instance.
- Check the Inbound rules of your security group.
- Ensure there is an inbound rule for
TCP on port 5000from anyIP (0.0.0.0/0), or specify the IP range you need to allow.
3. Check Network ACLs
Verify that the network ACLs associated with your subnet are not blocking **inbound or outbound **traffic on port 5000. Ensure that both inbound and outbound rules allow traffic on port 5000.
4. Check EC2 Instance Firewall
If your EC2 instance is running a firewall like ufw (Uncomplicated Firewall), ensure that it's configured to allow traffic on port 5000. Run the following command to allow traffic:
sudo ufw allow 5000/tcp
5. Check CloudWatch Logs
Review your CloudWatch logs to check for any errors related to network connectivity or your Flask app. This can provide insights into whether your app is running properly or if there are issues preventing access.
6. Test with Curl from Outside AWS
After making the above changes, test the Flask API from an external machine by running the following command:
curl http://<your_aws_public_ip>:5000
If everything is set up correctly, you should get a response from your Flask API.
Sources:
answered 2 years ago
Thank you for providing details about your issue, Carlos. It seems you're experiencing connectivity problems with your Flask API running on an AWS EC2 instance. Let's go through some potential causes and solutions:
-
Security Group Configuration: While you've mentioned checking the inbound rules, it's worth double-checking that the security group associated with your EC2 instance explicitly allows inbound traffic on port 5000 (or 8000) from your IP address or 0.0.0.0/0 for testing purposes.
-
Network ACLs: Ensure that the Network ACL for the subnet where your EC2 instance is located allows inbound and outbound traffic on the required ports.
-
Firewall on the EC2 Instance: The Ubuntu instance might have a firewall (like UFW) enabled. SSH into your instance and check if the firewall is blocking the port:
- Run
sudo ufw statusto check the firewall status - If it's active, ensure the port is allowed:
sudo ufw allow 5000/tcp
- Run
-
Application Binding: Ensure your Flask application is binding to all interfaces, not just localhost. In your Flask app, use:
app.run(host='0.0.0.0', port=5000) -
Check if the Application is Running: SSH into your EC2 instance and verify that the Flask application is actually running and listening on the correct port:
sudo netstat -tuln | grep 5000 -
Public IP Address: Confirm that you're using the correct public IP address. EC2 instances can get a new public IP when stopped and started (unless using an Elastic IP).
-
Instance Health: Check the EC2 instance's health status in the AWS Console to ensure there are no underlying issues with the instance itself.
-
VPC Configuration: Ensure your EC2 instance is in a public subnet with an Internet Gateway attached to the VPC.
If none of these solve the issue, you may want to enable verbose logging in your Flask application to see if there are any application-level errors preventing it from properly handling incoming requests.
Remember, for security reasons, it's generally recommended to use HTTPS and restrict access to specific IP ranges rather than opening your API to the entire internet (0.0.0.0/0) in a production environment.
Sources
Unable to connect to port despite changing setting in security group and firewall | AWS re:Post
EC2 Connection issue | AWS re:Post
Hi Riku, Thank you for your swift reply. the command "sudo ufw status" is not recognised. I must say that I just found out my machine is not an ubuntu but an Amazon Linux 2023. My FlaskAPI is running on:
- app.run(host='0.0.0.0', port=5000), also tried with app.run(host='0.0.0.0', port=8000)
if i do "sudo netstat -tuln | grep 8000" i get: tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
So I assume this is correct. Regarding ip address, i can ping the AWS machine, so I guess the IP address is correct.
Relevant content
asked 2 years ago
- AWS OFFICIALUpdated 3 years ago

Hi Tanvir, thank you for your help. Flask Binding is the same as you suggested. I just added inbound/outboud rules to the network ACL associated with my subnet on port 5000 and 8000. Do I need to restart the machine to apply the changes? I am not able to check the instance firewall. Command is not recognised since im in a Amazon Linux 2023 machine (my mistake above, saying it was an ubuntu machine)
You don’t need to restart your EC2 instance for security group or network ACL changes to take effect—they apply immediately. However, since you're using Amazon Linux 2023, here’s how you can check the firewall and allow traffic on ports 5000 and 8000:
sudo nft list ruleset If necessary, allow traffic on the required ports:
sudo firewall-cmd --permanent --add-port=5000/tcp sudo firewall-cmd --permanent --add-port=8000/tcp sudo firewall-cmd --reload
Check if the rules were applied:
sudo firewall-cmd --list-all
sudo ss -tulnp | grep 5000
You should see something like:
LISTEN 0 128 0.0.0.0:5000 0.0.0.0:* users:(("python",pid,fd)) If Flask is not listening on 0.0.0.0, restart your Flask app.
curl http://<your_aws_public_ip>:5000
If it still doesn’t work, try stopping firewalld temporarily to see if it's causing the issue:
sudo systemctl stop firewalld
If stopping firewalld fixes the issue, you’ll need to permanently configure it to allow Flask traffic instead o