AWS cloud9 timeout when running flask application

≯℡__Kan透↙ 提交于 2019-12-22 14:06:07

问题


Hi all I'm trying to setup a AWS cloud9 environment with flask to develop a web app. I'm new to AWS/ flask, and stuck on an issue. There seems to be an issue between the IDE environment and previewing the application in my browser (I'm using chrome, but have also tried in IE).

From app.py:

import os
from flask import Flask

app = Flask(__name__)

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


app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)))

if __name__ == '__main__':
    app.run()
    app.debug(True)

When I run this in the terminal (as root):

[root@ip-172-31-11-201 environment]# python ./app.py
Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

When I right click on the on http://0.0.0.0:8080/ it will open a yab and redirect me to a Public IP x.x.x.x:8080 and will eventually timeout and give me:

err_connection_timeout

When I attempt to run the application using the IDE run option it will take me to:

Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)

At which point it will just timeout as well. So this has me really confused, when I'm running this outside of the cloud9 IDE I don't have this issue. I know in the documentation you're supposed to point to 0.0.0.0 over port 8080. So I'm not quite sure why running that with run would change the IP I specified.

I've also tried manually putting my project and username in manually:

https://projectname-c9-username.c9.io/

At which point it redirects me to a page where it tells me it "can't find my username". I then tried to setup a cloud9.io account which completed, I confirmed my account but can't login and still have the "cannot find username" page.

After which I tested my app.py file from cloud9 locally with sublime substituted 0.0.0.0 for 127.0.0.1 and it worked locally.

Does is there anything I'm missing in my config? Has anything changed in the setup since AWS acquired cloud9, I've been following online tutorials and videos but just can't see to solve this issue.

From the IDE environment:

# python --version
Python 2.7.12

# pip freeze flask
astroid==1.5.3
aws-cfn-bootstrap==1.4
awscli==1.11.132
Babel==0.9.4
backports.functools-lru-cache==1.4
backports.ssl-match-hostname==3.4.0.2
boto==2.42.0
botocore==1.5.95
chardet==2.0.1
click==6.7
cloud-init==0.7.6
CodeIntel==0.9.3
colorama==0.2.5
configobj==4.7.2
configparser==3.5.0
docutils==0.11
ecdsa==0.11
enum34==1.1.6
Flask==0.12.2
futures==3.0.3
gyp==0.1
ikpdb==1.1.2
Inflector==2.0.11
iniparse==0.3.1
isort==4.2.15
itsdangerous==0.24
jedi==0.11.0
Jinja2==2.7.2
jmespath==0.9.2
jsonpatch==1.2
jsonpointer==1.0
kitchen==1.1.1
lazy-object-proxy==1.3.1
lockfile==0.8
MarkupSafe==0.11
mccabe==0.6.1
paramiko==1.15.1
parso==0.1.0
PIL==1.1.6
ply==3.4
pyasn1==0.1.7
pycrypto==2.6.1
pycurl==7.19.0
pygpgme==0.3
pyliblzma==0.5.3
pylint==1.7.4
pylint-django==0.7.2
pylint-flask==0.5
pylint-plugin-utils==0.2.6
pystache==0.5.3
python-daemon==1.5.2
python-dateutil==2.1
pyxattr==0.5.0
PyYAML==3.10
requests==1.2.3
rsa==3.4.1
simplejson==3.6.5
singledispatch==3.4.0.3
six==1.11.0
subprocess32==3.2.7
urlgrabber==3.10
urllib3==1.8.2
virtualenv==15.1.0
Werkzeug==0.13
wrapt==1.10.11
yum-metadata-parser==1.1.4
zope.cachedescriptors==4.3.0 

Thanks for any help!


回答1:


I figured out by searching EC2 questions:

You have to get past the AWS firewall / security settings. 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://imgur.com/a/zhQbA

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




回答2:


For me, I wasn't running/previewing the app correctly. I was either trying to use the Green Run button or flask run in the terminal.

Here's what worked for me.

Running the App

To run the app, go to the terminal and type python [NAMEOFPYTHONFILE] so python app.py

Previewing the App

To view the app, go to Preview in C9 toolbar and click Preview Running App. You'll be directed to a crazy url. Mine was https://a823459f29b04402a3793bec16fbXXXX.vfs.cloud9.us-east-1.amazonaws.com/ (edited a little)

My Code (app.py)

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Let's double check"

if __name__ == '__main__':
    app.debug = True
    app.run(host=os.environ['IP'], port=os.environ['PORT']) 

The current AWS C9 os environment variables are below

  • os.environ['IP'] is 127.0.0.1
  • os.environ['PORT'] is 8080

Hardcoding the port/host also works for both localhost and 127.0.0.01

  • app.run(host='localhost', port='8080')
  • app.run(host='127.0.0.1', port='8080')

I didn't have to change the security settings as hlidwin mentioned.



来源:https://stackoverflow.com/questions/47849926/aws-cloud9-timeout-when-running-flask-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!