I have an EC2 instance that is running a Flask application, this is how my code looks:
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods = ["GET"])
def HOME():
return {"mensaje": "Proyecto OLC2!"}
if __name__ == '__main__':
app.run(debug = True, port=4000)
Since I’m running the app on port 4000,this is how I set the inbound and outbound rules:
It seems that the app is running succesfully
But when I try to connect using the public IP and the port 4000 (publicIp:4000/), it gets me the error "Unable to connect" How can I solve this, am I doing something wrong?
2
Answers
The main issue you have is that your Flask app is, by default, listening on 127.0.0.1 which is the loopback interface. That’s only reachable from within the host itself.
For external clients to be able to reach your app, it needs to listen on 0.0.0.0 which will allow the app to accept traffic from all of the available network interfaces. Or you can bind it to one specific network interface, if you prefer, by supplying the IP address associated with that interface.
Adding onto jarmods answer- I would advise not allowing inbound from 0.0.0.0 as bad actors may try to take advantage of the access. 0.0.0.0 allows anyone from the internet to access your Ec2 on those ports.