SlideShare a Scribd company logo
1 of 49
Download to read offline
Building Serverless
applications with Python
Andrii Soldatenko
8 April 2017
Italy, Otto
@a_soldatenko
Andrii Soldatenko
• Senior Python Developer at
• CTO at
• Co-organizer PyCon Belarus 2017
• Speaker at many PyCons and open
source contributor
• blogger at https://asoldatenko.com
@a_soldatenko
…in the next few years we’re going to
see the first billion-dollar startup with a
single employee, the founder, and that
engineer will be using serverless
technology.
@a_soldatenko
James Governor
Analyst & Co-founder at RedMonk
Origins of “Serverless”
@a_soldatenko
Origins of
http://readwrite.com/2012/10/15/why-the-future-of-software-
and-apps-is-serverless/
Travis CI
Later in 2014…
Amazon introduce
AWS Lambda
https://techcrunch.com/2015/11/24/aws-lamda-makes-
serverless-applications-a-reality/
What about conferences?
http://serverlessconf.io/
How Lambda works
@a_soldatenko
λ
@a_soldatenko
How Lambda works
@a_soldatenko
How Lambda works
- function name;
- memory size
- timeout;
- role;
@a_soldatenko
Your first λ function
def lambda_handler(event, context):
message = 'Hello {}!'.format(event['name'])
return {'message': message}
@a_soldatenko
Deploy λ function
#!/usr/bin/env bash
python -m zipfile -c hello_python.zip hello_python.py
aws lambda create-function 
--region us-west-2 
--function-name HelloPython 
--zip-file fileb://hello_python.zip 
--role arn:aws:iam::278117350010:role/lambda-s3-
execution-role 
--handler hello_python.my_handler 
--runtime python2.7 
--timeout 15 
--memory-size 512
@a_soldatenko
Invoke λ function
aws lambda invoke 
--function-name HelloPython 
--payload '{"name":"PyCon
Italy"}' output.txt
cat output.txt
{"message": "Hello PyCon Italy!"}%
@a_soldatenko
Amazon API Gateway
Resource HTTP verb
AWS
Lambda
/books GET get_books
/book POST create_book
/books/{ID} PUT chage_book
/books/{ID} DELETE delete_book
Chalice python
micro framework
https://github.com/awslabs/chalice
@a_soldatenko
Chalice python
micro framework
$ cat app.py

from chalice import Chalice
app = Chalice(app_name='hellopyconit')
@app.route('/books', methods=['GET'])
def get_books():
return {'hello': 'from python library'}

...

...
...
https://github.com/awslabs/chalice
...

...

...

@app.route('/books/{book_id}', methods=['POST', 'PUT', 'DELETE'])
def process_book(book_id):
request = app.current_request
if request.method == 'PUT':
return {'msg': 'Book {} changed'.format(book_id)}
elif request.method == 'DELETE':
return {'msg': 'Book {} deleted'.format(book_id)}
elif request.method == 'POST':
return {'msg': 'Book {} created'.format(book_id)}
Chalice python
micro framework
@a_soldatenko
Chalice deploy
chalice deploy
Updating IAM policy.
Updating lambda function...
Regen deployment package...
Sending changes to lambda.
API Gateway rest API already
found.
Deploying to: dev
https://8rxbsnge8d.execute-api.us-
west-2.amazonaws.com/dev/
@a_soldatenko
Try our books API
curl -XGET https://8rxbsnge8d.execute-
api.us-west-2.amazonaws.com/dev/books
{"hello": "from python library"}%
curl -XGET https://8rxbsnge8d.execute-
api.us-west-2.amazonaws.com/dev/books/15
{"message": "Book 15"}%
curl -XDELETE https://8rxbsnge8d.execute-
api.us-west-2.amazonaws.com/dev/books/15
{"message": "Book 15 has been deleted"}%
@a_soldatenko
Chalice under the hood
@a_soldatenko
- botocore;
- typing;
Chalice under the hood
@a_soldatenko
AWS Serverless
Application Model
@a_soldatenko
AWS SAM
@a_soldatenko
AWS lambda Limits
@a_soldatenko
AWS Lambda
without any costs
@a_soldatenko
- The first 400,000 seconds of
execution time with 1 GB of
memory
What if you want to run
your Django app?
@a_soldatenko
@a_soldatenko
@a_soldatenko
@a_soldatenko
And again what do you
mean “serveless”?
@a_soldatenko
➜ pip install zappa
➜ zappa init
███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
Welcome to Zappa!
...
➜ zappa deploy
https://github.com/Miserlou/Zappa
@a_soldatenkohttps://github.com/Miserlou/Zappa
- deploy;
- tailing logs;
- run django manage.py

…
███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
AWS lambda Limits
@a_soldatenko
Python 2.7
@a_soldatenko
Python 2.7 will retire
in...
@a_soldatenkohttps://pythonclock.org/
AWS lambda and
Python 3
@a_soldatenko
import os
def lambda_handler(event, context):
txt = open('/etc/issue')
print txt.read()
return {'message': txt.read()}
Amazon Linux AMI release 2016.03
Kernel r on an m
AWS lambda and
Python 3
@a_soldatenko
Linux ip-10-11-15-179 4.4.51-40.60.amzn1.x86_64 #1 SMP
Wed Mar 29 19:17:24 UTC 2017 x86_64 x86_64 x86_64 GNU/
Linux
import subprocess
def lambda_handler(event, context):
args = ('uname', '-a')
popen = subprocess.Popen(args,
stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print(output)
AWS lambda and
Python 3
@a_soldatenko
import subprocess
def lambda_handler(event, context):
args = ('which', 'python3')
popen = subprocess.Popen(args,
stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print(output)
python3: /usr/bin/python3 /usr/bin/python3.4m /usr/bin/
python3.4 /usr/lib/python3.4 /usr/lib64/python3.4 /usr/local/lib/
python3.4 /usr/include/python3.4m /usr/share/man/man1/
python3.1.gz
YAHOOO!!:
Run python 3 from
python 2
@a_soldatenko
Prepare Python 3 for
AWS Lambda
@a_soldatenko
virtualenv venv -p `which python3.4`
pip install requests
zip -r lambda_python3.zip venv
python3_program.py lambda.py
Run python 3 from
python 2
@a_soldatenko
cat lambda.py
import subprocess
def lambda_handler(event, context):
args = ('venv/bin/python3.4',
'python3_program.py')
popen = subprocess.Popen(args,
stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print(output)
Run python 3 from
python 2
@a_soldatenko
START RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1
Version: $LATEST
Python 3.4.0
END RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1
REPORT RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1
Thanks Lyndon Swan
for ideas about hacking
python 3
@a_soldatenko
Future of serverless
@a_soldatenko
The biggest problem with Serverless FaaS right now is tooling.
Deployment / application bundling, configuration, monitoring /
logging, and debugging all need serious work.
https://github.com/awslabs/serverless-
application-model/blob/master/HOWTO.md
Thank You
andrii.soldatenko@toptal.com
https://asoldatenko.com
@a_soldatenko
Questions
? @a_soldatenko
We are hiring
https://www.toptal.com/#connect-
fantastic-computer-engineers

More Related Content

What's hot

What's hot (20)

Helpful Automation Techniques - Selenium Camp 2014
Helpful Automation Techniques - Selenium Camp 2014Helpful Automation Techniques - Selenium Camp 2014
Helpful Automation Techniques - Selenium Camp 2014
 
Selenium conference, 2016
Selenium conference, 2016Selenium conference, 2016
Selenium conference, 2016
 
SeConf_Nov2016_London
SeConf_Nov2016_LondonSeConf_Nov2016_London
SeConf_Nov2016_London
 
Cypress workshop for JSFoo 2019
Cypress  workshop for JSFoo 2019Cypress  workshop for JSFoo 2019
Cypress workshop for JSFoo 2019
 
Engineering Tools at Netflix: Enabling Continuous Delivery
Engineering Tools at Netflix: Enabling Continuous DeliveryEngineering Tools at Netflix: Enabling Continuous Delivery
Engineering Tools at Netflix: Enabling Continuous Delivery
 
Closer To the Metal - Why and How We Use XCTest and Espresso by Mario Negro P...
Closer To the Metal - Why and How We Use XCTest and Espresso by Mario Negro P...Closer To the Metal - Why and How We Use XCTest and Espresso by Mario Negro P...
Closer To the Metal - Why and How We Use XCTest and Espresso by Mario Negro P...
 
Introduction to cypress in Angular (Chinese)
Introduction to cypress in Angular (Chinese)Introduction to cypress in Angular (Chinese)
Introduction to cypress in Angular (Chinese)
 
Selenium Testing on Chrome - Google DevFest Armenia 2015
Selenium Testing on Chrome - Google DevFest Armenia 2015Selenium Testing on Chrome - Google DevFest Armenia 2015
Selenium Testing on Chrome - Google DevFest Armenia 2015
 
Selenium Conference 2015 - Mobile Selenium Grid Setup
Selenium Conference 2015 - Mobile Selenium Grid SetupSelenium Conference 2015 - Mobile Selenium Grid Setup
Selenium Conference 2015 - Mobile Selenium Grid Setup
 
Selenium Camp 2016 - Kiev, Ukraine
Selenium Camp 2016 -  Kiev, UkraineSelenium Camp 2016 -  Kiev, Ukraine
Selenium Camp 2016 - Kiev, Ukraine
 
Automation Best Practices
Automation Best PracticesAutomation Best Practices
Automation Best Practices
 
Best Practices in Mobile CI (webinar)
Best Practices in Mobile CI (webinar)Best Practices in Mobile CI (webinar)
Best Practices in Mobile CI (webinar)
 
Build Automation in Android
Build Automation in AndroidBuild Automation in Android
Build Automation in Android
 
Midwest PHP 2017 DevOps For Small team
Midwest PHP 2017 DevOps For Small teamMidwest PHP 2017 DevOps For Small team
Midwest PHP 2017 DevOps For Small team
 
Moving From a Selenium Grid to the Cloud - A Real Life Story
Moving From a Selenium Grid to the Cloud - A Real Life StoryMoving From a Selenium Grid to the Cloud - A Real Life Story
Moving From a Selenium Grid to the Cloud - A Real Life Story
 
Continuous Testing in the Cloud
Continuous Testing in the CloudContinuous Testing in the Cloud
Continuous Testing in the Cloud
 
Testing desktop apps with selenium
Testing desktop apps with seleniumTesting desktop apps with selenium
Testing desktop apps with selenium
 
An Introduction to Appium Desktop
An Introduction to Appium DesktopAn Introduction to Appium Desktop
An Introduction to Appium Desktop
 
Belfast Selenium Meetup
Belfast Selenium MeetupBelfast Selenium Meetup
Belfast Selenium Meetup
 
Selenium and Sauce Labs
Selenium and Sauce LabsSelenium and Sauce Labs
Selenium and Sauce Labs
 

Similar to Building serverless-applications

Building an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learnedBuilding an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learned
Wojciech Koszek
 

Similar to Building serverless-applications (20)

Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 
Building Serverless applications with Python
Building Serverless applications with PythonBuilding Serverless applications with Python
Building Serverless applications with Python
 
Try! Swift Tokyo2017
Try! Swift Tokyo2017Try! Swift Tokyo2017
Try! Swift Tokyo2017
 
Serverless in production, an experience report
Serverless in production, an experience reportServerless in production, an experience report
Serverless in production, an experience report
 
Serverless in Production, an experience report (AWS UG South Wales)
Serverless in Production, an experience report (AWS UG South Wales)Serverless in Production, an experience report (AWS UG South Wales)
Serverless in Production, an experience report (AWS UG South Wales)
 
DevOps + MongoDB Realm Serverless Functions = 🤩
DevOps + MongoDB Realm Serverless Functions = 🤩DevOps + MongoDB Realm Serverless Functions = 🤩
DevOps + MongoDB Realm Serverless Functions = 🤩
 
Serverless in production, an experience report (CoDe-Conf)
Serverless in production, an experience report (CoDe-Conf)Serverless in production, an experience report (CoDe-Conf)
Serverless in production, an experience report (CoDe-Conf)
 
Serverless in production, an experience report (FullStack 2018)
Serverless in production, an experience report (FullStack 2018)Serverless in production, an experience report (FullStack 2018)
Serverless in production, an experience report (FullStack 2018)
 
DevOps + MongoDB Serverless = 
DevOps + MongoDB Serverless = DevOps + MongoDB Serverless = 
DevOps + MongoDB Serverless = 
 
Introduce Django
Introduce DjangoIntroduce Django
Introduce Django
 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TS
 
SoundCloud API Do:s and Don't:s
SoundCloud API Do:s and Don't:sSoundCloud API Do:s and Don't:s
SoundCloud API Do:s and Don't:s
 
API-First Design and Django
API-First Design and DjangoAPI-First Design and Django
API-First Design and Django
 
Overboard.js - where are we going with with jsconfasia / devfestasia
Overboard.js - where are we going with with jsconfasia / devfestasiaOverboard.js - where are we going with with jsconfasia / devfestasia
Overboard.js - where are we going with with jsconfasia / devfestasia
 
Kiwipycon command line
Kiwipycon command lineKiwipycon command line
Kiwipycon command line
 
Building an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learnedBuilding an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learned
 
SaaS Boilerplate.pptx
SaaS Boilerplate.pptxSaaS Boilerplate.pptx
SaaS Boilerplate.pptx
 
Jets: The Ruby Serverless Framework Ruby Kaigi Japan 2019 April
Jets: The Ruby Serverless Framework Ruby Kaigi Japan 2019 AprilJets: The Ruby Serverless Framework Ruby Kaigi Japan 2019 April
Jets: The Ruby Serverless Framework Ruby Kaigi Japan 2019 April
 
Understanding and building Your Own Docker
Understanding and building Your Own DockerUnderstanding and building Your Own Docker
Understanding and building Your Own Docker
 
Metasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCUMetasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCU
 

More from Andrii Soldatenko

More from Andrii Soldatenko (8)

Debugging concurrency programs in go
Debugging concurrency programs in goDebugging concurrency programs in go
Debugging concurrency programs in go
 
Building robust and friendly command line applications in go
Building robust and friendly command line applications in goBuilding robust and friendly command line applications in go
Building robust and friendly command line applications in go
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
 
Building social network with Neo4j and Python
Building social network with Neo4j and PythonBuilding social network with Neo4j and Python
Building social network with Neo4j and Python
 
What is the best full text search engine for Python?
What is the best full text search engine for Python?What is the best full text search engine for Python?
What is the best full text search engine for Python?
 
Kyiv.py #16 october 2015
Kyiv.py #16 october 2015Kyiv.py #16 october 2015
Kyiv.py #16 october 2015
 
PyCon Russian 2015 - Dive into full text search with python.
PyCon Russian 2015 - Dive into full text search with python.PyCon Russian 2015 - Dive into full text search with python.
PyCon Russian 2015 - Dive into full text search with python.
 
PyCon 2015 Belarus Andrii Soldatenko
PyCon 2015 Belarus Andrii SoldatenkoPyCon 2015 Belarus Andrii Soldatenko
PyCon 2015 Belarus Andrii Soldatenko
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Building serverless-applications

  • 1. Building Serverless applications with Python Andrii Soldatenko 8 April 2017 Italy, Otto @a_soldatenko
  • 2. Andrii Soldatenko • Senior Python Developer at • CTO at • Co-organizer PyCon Belarus 2017 • Speaker at many PyCons and open source contributor • blogger at https://asoldatenko.com @a_soldatenko
  • 3. …in the next few years we’re going to see the first billion-dollar startup with a single employee, the founder, and that engineer will be using serverless technology. @a_soldatenko James Governor Analyst & Co-founder at RedMonk
  • 7.
  • 8. Later in 2014… Amazon introduce AWS Lambda https://techcrunch.com/2015/11/24/aws-lamda-makes- serverless-applications-a-reality/
  • 13. How Lambda works - function name; - memory size - timeout; - role; @a_soldatenko
  • 14. Your first λ function def lambda_handler(event, context): message = 'Hello {}!'.format(event['name']) return {'message': message} @a_soldatenko
  • 15. Deploy λ function #!/usr/bin/env bash python -m zipfile -c hello_python.zip hello_python.py aws lambda create-function --region us-west-2 --function-name HelloPython --zip-file fileb://hello_python.zip --role arn:aws:iam::278117350010:role/lambda-s3- execution-role --handler hello_python.my_handler --runtime python2.7 --timeout 15 --memory-size 512 @a_soldatenko
  • 16. Invoke λ function aws lambda invoke --function-name HelloPython --payload '{"name":"PyCon Italy"}' output.txt cat output.txt {"message": "Hello PyCon Italy!"}% @a_soldatenko
  • 17. Amazon API Gateway Resource HTTP verb AWS Lambda /books GET get_books /book POST create_book /books/{ID} PUT chage_book /books/{ID} DELETE delete_book
  • 19. Chalice python micro framework $ cat app.py
 from chalice import Chalice app = Chalice(app_name='hellopyconit') @app.route('/books', methods=['GET']) def get_books(): return {'hello': 'from python library'}
 ...
 ... ... https://github.com/awslabs/chalice
  • 20. ...
 ...
 ...
 @app.route('/books/{book_id}', methods=['POST', 'PUT', 'DELETE']) def process_book(book_id): request = app.current_request if request.method == 'PUT': return {'msg': 'Book {} changed'.format(book_id)} elif request.method == 'DELETE': return {'msg': 'Book {} deleted'.format(book_id)} elif request.method == 'POST': return {'msg': 'Book {} created'.format(book_id)} Chalice python micro framework @a_soldatenko
  • 21. Chalice deploy chalice deploy Updating IAM policy. Updating lambda function... Regen deployment package... Sending changes to lambda. API Gateway rest API already found. Deploying to: dev https://8rxbsnge8d.execute-api.us- west-2.amazonaws.com/dev/ @a_soldatenko
  • 22. Try our books API curl -XGET https://8rxbsnge8d.execute- api.us-west-2.amazonaws.com/dev/books {"hello": "from python library"}% curl -XGET https://8rxbsnge8d.execute- api.us-west-2.amazonaws.com/dev/books/15 {"message": "Book 15"}% curl -XDELETE https://8rxbsnge8d.execute- api.us-west-2.amazonaws.com/dev/books/15 {"message": "Book 15 has been deleted"}% @a_soldatenko
  • 23. Chalice under the hood @a_soldatenko - botocore; - typing;
  • 24. Chalice under the hood @a_soldatenko
  • 28. AWS Lambda without any costs @a_soldatenko - The first 400,000 seconds of execution time with 1 GB of memory
  • 29. What if you want to run your Django app? @a_soldatenko
  • 32. @a_soldatenko And again what do you mean “serveless”?
  • 33. @a_soldatenko ➜ pip install zappa ➜ zappa init ███████╗ █████╗ ██████╗ ██████╗ █████╗ ╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║ ███████╗██║ ██║██║ ██║ ██║ ██║ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ Welcome to Zappa! ... ➜ zappa deploy https://github.com/Miserlou/Zappa
  • 34. @a_soldatenkohttps://github.com/Miserlou/Zappa - deploy; - tailing logs; - run django manage.py
 … ███████╗ █████╗ ██████╗ ██████╗ █████╗ ╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║ ███████╗██║ ██║██║ ██║ ██║ ██║ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
  • 37. Python 2.7 will retire in... @a_soldatenkohttps://pythonclock.org/
  • 38. AWS lambda and Python 3 @a_soldatenko import os def lambda_handler(event, context): txt = open('/etc/issue') print txt.read() return {'message': txt.read()} Amazon Linux AMI release 2016.03 Kernel r on an m
  • 39. AWS lambda and Python 3 @a_soldatenko Linux ip-10-11-15-179 4.4.51-40.60.amzn1.x86_64 #1 SMP Wed Mar 29 19:17:24 UTC 2017 x86_64 x86_64 x86_64 GNU/ Linux import subprocess def lambda_handler(event, context): args = ('uname', '-a') popen = subprocess.Popen(args, stdout=subprocess.PIPE) popen.wait() output = popen.stdout.read() print(output)
  • 40. AWS lambda and Python 3 @a_soldatenko import subprocess def lambda_handler(event, context): args = ('which', 'python3') popen = subprocess.Popen(args, stdout=subprocess.PIPE) popen.wait() output = popen.stdout.read() print(output) python3: /usr/bin/python3 /usr/bin/python3.4m /usr/bin/ python3.4 /usr/lib/python3.4 /usr/lib64/python3.4 /usr/local/lib/ python3.4 /usr/include/python3.4m /usr/share/man/man1/ python3.1.gz YAHOOO!!:
  • 41. Run python 3 from python 2 @a_soldatenko
  • 42. Prepare Python 3 for AWS Lambda @a_soldatenko virtualenv venv -p `which python3.4` pip install requests zip -r lambda_python3.zip venv python3_program.py lambda.py
  • 43. Run python 3 from python 2 @a_soldatenko cat lambda.py import subprocess def lambda_handler(event, context): args = ('venv/bin/python3.4', 'python3_program.py') popen = subprocess.Popen(args, stdout=subprocess.PIPE) popen.wait() output = popen.stdout.read() print(output)
  • 44. Run python 3 from python 2 @a_soldatenko START RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1 Version: $LATEST Python 3.4.0 END RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1 REPORT RequestId: 44c89efb-1bd2-11e7-bf8c-83d444ed46f1
  • 45. Thanks Lyndon Swan for ideas about hacking python 3 @a_soldatenko
  • 46. Future of serverless @a_soldatenko The biggest problem with Serverless FaaS right now is tooling. Deployment / application bundling, configuration, monitoring / logging, and debugging all need serious work. https://github.com/awslabs/serverless- application-model/blob/master/HOWTO.md