Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:Invent 2012

6,189 views

Published on

  • Be the first to comment

TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:Invent 2012

  1. 1. Availability Zone Security GroupAuto scaling Group
  2. 2. Security Group Production Availability Zone Security Group Staging Availability ZoneAuto scaling Group Auto scaling Group Security Group Integration Availability Zone Development Security Group Availability Zone Auto scaling Group Auto scaling Group
  3. 3. Deployment is Hard
  4. 4. AWS Elastic Beanstalk
  5. 5. Amazon DynamoDB Application
  6. 6. $ curl tnee.us/api/v1/shorten?url=http://aws.amazon.com/{ "shorturl": "http://tnee.us/eeGdAO"}$ curl http://tnee.us/eeGdAOHTTP/1.0 301 MOVED PERMANENTLYContent-Type: text/html; charset=utf-8Content-Length: 251Location: http://aws.amazon.com/
  7. 7. pip installs python packagesvirtualenv isolates python packagesgit stores revisions of our appeb manages our deployed application
  8. 8. pip installs python packages$ pip install flask$ python -c "import flask; print flask"<module flask from/usr/local/lib/python/…/flask/__init__.pyc>
  9. 9. pip installs python packages$ pip install flask$ python -c "import flask; print flask"<module flask from/usr/local/lib/python/…/flask/__init__.pyc>
  10. 10. virtualenv isolates python packagesvenv1 venv2./venv1/lib/python ./venv2/lib/python./venv1/bin/python ./venv2/bin/python
  11. 11. virtualenv isolates python packages $ virtualenv venv1 $ . venv1/bin/activate $ which python venv1/bin/python $ pip install flask $ python -c "import flask; print flask” <module flask from ./venv1/lib/python/.../flask/__init__.pyc>
  12. 12. ├── application.py├── requirements.txt├── scripts│ └── createtable.py├── shortflask│ ├── __init__.py│ ├── views.py│ ├── config.py│ ├── db.py│ └── shortener.py└── tests
  13. 13. application.pyimport shortflaskapplication = shortflask.create_app()if __name__ == __main__: application.run(debug=True)
  14. 14. requirements.txtFlask==0.9Jinja2==2.6Werkzeug==0.8.3boto==2.6.0
  15. 15. requirements.txtFlask==0.9 pip install flask botoJinja2==2.6 pip freeze > requirements.txtWerkzeug==0.8.3boto==2.6.0
  16. 16. shortflask/views.py@app.route(/api/v1/shorten, methods=[GET])def create_shortened_url(): url = request.args[url] host = request.headers[Host] try: shortened = _shorten_url(url) except Exception, e: return "Unable to shorten url.", 500 return jsonify({ shorturl: http://%s/%s % (host, shortened)})
  17. 17. What have we NOT done?
  18. 18. CurrentApache Sample Appwsgi.conf
  19. 19. CurrentApache Sample Appwsgi.conf Our Application
  20. 20. CurrentApache Sample App venv pipwsgi.conf Our Application
  21. 21. CurrentApache Sample App venv pip generatewsgi.conf Our Application
  22. 22. Apache Sample App Current venv pipwsgi.conf Our Application
  23. 23. One Hour10 threads 15 urls 10 instances
  24. 24. .elasticbeanstalk/optionsettings[aws:autoscaling:trigger]MeasureName = CPUUtilizationUpperThreshold = 90LowerThreshold = 40Unit = Percent
  25. 25. 515,656135,0911 Instance 4 Instances with Auto Scaling
  26. 26. djangoimg/wsgi.pyimport osos.environ.setdefault( "DJANGO_SETTINGS_MODULE", "djangoimg.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()
  27. 27. djangoimg/settings.pyINSTALLED_APPS = ( ... south, storages, easy_thumbnails, imgshare,)
  28. 28. djangoimg/settings.pySTATIC_ROOT = os.path.join( os.path.dirname( os.path.dirname( os.path.abspath(__file__))), static)STATIC_URL = /static/
  29. 29. djangoimg/settings.pyAWS_ACCESS_KEY_ID = os.environ.get(ACCESS_KEY)AWS_SECRET_ACCESS_KEY = os.environ.get(SECRET_KEY)DEFAULT_FILE_STORAGE = storages.backends.s3boto.S3BotoStorageAWS_STORAGE_BUCKET_NAME = djangoimgMEDIA_URL = (http://%s.s3.amazonaws.com/ % AWS_STORAGE_BUCKET_NAME)
  30. 30. .ebextensions/python.configcommands: 01_syncdb: command: "django-admin.py syncdb --noinput" leader_only: true 02_migrate: command: "django-admin.py migrate" leader_only: true 03_collectstatic: command: "django-admin.py collectstatic --noinput"
  31. 31. .ebextensions/python.configoption_settings: "aws:elasticbeanstalk:container:python:environment": DJANGO_SETTINGS_MODULE: "djangoimg.settings" "aws:elasticbeanstalk:container:python": WSGIPath: "djangoimg/wsgi.py" "aws:elasticbeanstalk:container:python:staticfiles": "/static/": "static/"
  32. 32. ApplicationElastic Beanstalk Envi r onment
  33. 33. RDS Create ApplicationElastic Beanstalk Envi r onment
  34. 34. RDS Authorize Create ApplicationElastic Beanstalk Envi r onment
  35. 35. RDS Authorize Create ApplicationElastic Beanstalk Envi r onment Inject RDS_DB_NAME RDS_USERNAME RDS_PASSWORD RDS_HOSTNAME RDS_PORT
  36. 36. djangoimg/settings.pyif RDS_HOSTNAME in os.environ: DATABASES = { default: { ENGINE: django.db.backends.mysql, NAME: os.environ[RDS_DB_NAME’], USER: os.environ[RDS_USERNAME], PASSWORD: os.environ[RDS_PASSWORD], HOST: os.environ[RDS_HOSTNAME], PORT: os.environ[RDS_PORT], } }
  37. 37. We are sincerely eager to hear your feedback on thispresentation and on re:Invent. Please fill out an evaluation form when you have a chance.

×