Search code examples
pythonamazon-web-servicesflaskcloud9-ideaws-cloud9

Can't preview 'Hello World' application in aws cloud9 c9 python flask


I can't preview this appliation using AWS Cloud9 (c9) python flask:

from flask import Flask
import os
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

host = 'localhost' # '0.0.0.0' #"127.0.0.1" # I tried all of these ip's

if __name__ == '__main__':
    app.debug = True
    port = int(os.environ.get("PORT", 8080)) # I also tried port was 5000
    app.run(host=host, port=port)

# This is the error I got 52.15.73.96 The connection has timed out
#The server at 52.15.73.96 is taking too long to respond.

This is similar to AWS cloud9 timeout when running flask application


Solution

  • Here is the answer: You have to get past the AWS firewall. You have to

    1. Go into EC2 (from the list of all AWS services)
    2. Click security groups
    3. Click your Cloud9 instance
    4. Click Inbound
    5. Click Edit
    6. Click Add Rule
    7. Add this rule:

      • For Type, choose Custom TCP Rule. - All Traffic also worked.
      • For Port Range, type 8080, 8081, or 8082. - If you did 'All Traffic' this will default to all ports.
      • For Source, choose Anywhere, which looks like 0.0.0.0/0

      Here is a screen shot link: https://i.sstatic.net/xpW5F.jpg

    AWS does have this, buried, in their C9 documentation. https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html#app-preview-share-security-group In Share a Running Application over the Internet, Step 2: Set Up the Security Group for the Instance