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.

DJUGL - Django and AWS Lambda

563 views

Published on

Presentation from Django User Group London, June 2017

How to run Django on AWS Lambda - with demo of getting a hello world deployed in less than 10 minutes

Published in: Technology
  • Be the first to comment

  • Be the first to like this

DJUGL - Django and AWS Lambda

  1. 1. Django and AWS Lambda Malcolm Box malcolm.box@gmail.com Sometimes: @malcolmbox Very occasionally: https://attentionshard.wordpress.com
  2. 2. About me • Working with Django since 2009 (Django 1.1) • CTO of Tellybug where we made apps for TV shows and a very scalable platform using Django & AWS • Recently started building a new project using serverless • Consulting CTO working with startups
  3. 3. “AWS Lambda is a compute service that lets you run code without provisioning or managing servers”
  4. 4. Data Centre Server Linux nginx gunicorn Django def view(request): return render(request, "demo.html") *Not to scale
  5. 5. https://github.com/Miserlou/Zappa
  6. 6. Demo! • You will need: • AWS Credentials • A python virtualenv • Instructions and example code: https://github.com/mbox/djugl-demo
  7. 7. Other Zappa goodness $ zappa tail Calling tail for stage demo.. [1497350426182] [DEBUG] 2017-06-13T10:40:26.182Z b55a7b0b-5024-11e7-8e46-47e2239fb3ce Zappa Event: {u'body': None, u'resource': u'/', u'requestContext': {u'resourceId': u'gm6q48zq93', u'apiId': u'lkvzkzlzd6', u'resourcePath': u'/', u'httpMethod': u'GET', u'requestId': u'b54d0e51-5024-11e7-b059-6324c1a6f402', u'path': u'/demo', u'accountId': u'956653133436', u'identity': {u'apiKey': u'', u'userArn': None, u'cognitoAuthenticationType': None, u'accessKey': None, u'caller': None, u'userAgent': u'python-requests/2.17.3', u'user': None, u'cognitoIdentityPoolId': None, u'cognitoIdentityId': None, u'cognitoAuthenticationProvider': None, u'sourceIp': u'82.3.163.138', u'accountId': None}, u'stage': u'demo'}, u'queryStringParameters': None, u'httpMethod': u'GET', u'pathParameters': None, u'headers': {u'Via': u'1.1 2e578cf3c3154a7d5d4f253d74c2b940.cloudfront.net (CloudFront)', u'Accept-Encoding': u'gzip, deflate', u'CloudFront-Is-SmartTV-Viewer': u'false', u'CloudFront-Forwarded-Proto': u'https', u'X-Forwarded-For': u'82.3.163.138, 54.182.244.97', u'CloudFront-Viewer-Country': u'GB', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.17.3', u'X-Amzn-Trace-Id': u'Root=1-593fc118-7e2199041b9cd95f3abfe753', u'Host': u'lkvzkzlzd6.execute-api.eu-west-1.amazonaws.com', u'X-Forwarded-Proto': u'https', u'X-Amz-Cf-Id': u'1Cn8Ygt_ftJmfKnGiNqCTOO2SNT- gcNigAY0DXARyP2O500N8oEZGQ==', u'CloudFront-Is-Tablet-Viewer': u'false', u'X-Forwarded-Port': u'443', u'CloudFront-Is-Mobile-Viewer': u'false', u'CloudFront-Is-Desktop-Viewer': u'true'}, u'stageVariables': None, u'path': u'/', u'isBase64Encoded': False} [1497350426184] Invalid HTTP_HOST header: 'lkvzkzlzd6.execute-api.eu-west-1.amazonaws.com'. You may need to add u'lkvzkzlzd6.execute- api.eu-west-1.amazonaws.com' to ALLOWED_HOSTS. [1497350426184] [ERROR] 2017-06-13T10:40:26.183Z b55a7b0b-5024-11e7-8e46-47e2239fb3ce Invalid HTTP_HOST header: 'lkvzkzlzd6.execute-api.eu-west-1.amazonaws.com'. You may need to add u'lkvzkzlzd6.execute-api.eu-west-1.amazonaws.com' to ALLOWED_HOSTS. [1497350426341] [INFO] 2017-06-13T10:40:26.341Z b55a7b0b-5024-11e7-8e46-47e2239fb3ce 82.3.163.138 - - [13/Jun/2017:10:40:26 +0000] "GET / HTTP/1.1" 400 37968 "" "python-requests/2.17.3" 0/158.477
  8. 8. zappa manage $ zappa manage demo migrate START RequestId: da3e8e84-5039-11e7-9c38-2913a464f8e0 Version: $LATEST Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: No migrations to apply. END RequestId: da3e8e84-5039-11e7-9c38-2913a464f8e0 REPORT RequestId: da3e8e84-5039-11e7-9c38-2913a464f8e0Duration: 386.42 msBilled Duration: 400 ms Memory Size: 512 MB Max Memory Used: 52 MB
  9. 9. zappa schedule
  10. 10. Asynchronous Task Execution from flask import Flask from zappa.async import task app = Flask(__name__) @task def make_pie(): """ This takes a long time! """ ingredients = get_ingredients() pie = bake(ingredients) deliver(pie) @app.route('/api/order/pie') def order_pie(): """ This returns immediately! """ make_pie() return "Your pie is being made!"
  11. 11. Going further • Database - can use RDS, Dynamo, Redshift etc • GeoDjango & binary libraries - issues if they get too big • Python 3 - now supported • Static files - use Whitenoise http://whitenoise.evans.io
  12. 12. Thank you!

×