I am trying to deploy a simple flask app on ec2 instance but unable to send any http requests , I have tried different combinations of ips and port while creating a security group but it still wont work .
the app works as needed on my localhost.
here is the code
import logging
import sys
import threading
from flask import Flask, request, jsonify
from main import mainfunction
import concurrent.futures
# Configure logging to capture output
app = Flask(__name__)
# Your asynchronous function that will be called
def process_client(client):
# Redirect stdout to the console
sys.stdout = sys.__stdout__
# Create a thread to run mainfunction in the background
thread = threading.Thread(target=mainfunction, args=(client,))
thread.start()
return "Processing client in the background..."
@app.route('/process', methods=['POST'])
def process():
try:
data = request.get_json()
if 'client' in data:
client = data['client']
# Call the asynchronous function using asyncio
result = process_client(client)
return jsonify({'result': result})
else:
return jsonify({'error': 'Missing client data in request'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/', methods=['GET'])
def status():
return 'Server is listening'
if __name__ == '__main__':
app.run(debug=True)
here are the inbound and outbound rules I have setup
it has been more than a week and i dont know how to proceed with this problem
3
Answers
Need to make sure that the webserver is responding. You cud probably check using cmd prompt and localhost url. And doesn’t seem to be SecurityGroup/Firewall issue. To confirm this, you can Temp allow "All Traffic" for both InBound and OutBound http+https+SSH requests.
When deploying a Flask application on an EC2 instance, there are several things to consider for it to be accessible externally. Below are some troubleshooting steps that might help you figure out why you can’t send HTTP requests to your EC2 Flask app.
Security Groups
Make sure that you have opened the port (by default Flask uses port 5000) in the EC2 Security Group.
Go to your AWS Console.
Security" section.
instance.
Source: 0.0.0.0/0 (though you might want to limit it later for
security reasons)
Running the Flask App
In your Flask app code, you have:
Change this to:
Using 0.0.0.0 allows the app to listen to requests from any IP address, not just localhost.
Networking
Check your EC2 instance’s public IP. Use that IP to connect to your Flask app from your local machine, like so:
arduino
Firewall
It’s also possible that you have a local firewall enabled on the EC2 instance itself that is preventing external access. Check that using:
For Ubuntu/Debian:
For Red Hat/CentOS:
Logs and Debugging
Enable Flask logging by adding these lines in your code:
Then check the logs for any errors or issues.
Finally
If none of these steps work, the problem could be more complex, and you might need to look into networking settings, VPC configurations, or something else.
You’re running your app on port
5000
in your local machine you can access this atlocalhost:5000
.But it’s a different scenario with EC2. You need to set up a proxy service here. Example nginx. So what this does is when ever you send a request to your EC2 IP. The request will be forwarded to yourlocalhost:5000
(Of your EC2). Here’s an example on how to setup NGINX on your EC2 link.