SlideShare a Scribd company logo
1 of 61
Download to read offline
Building Serverless
applications with Python3
Andrii Soldatenko
12 June 2017
@a_soldatenko
Andrii
Soldatenko
• Senior Python Developer at Toptal
• Speaker at many PyCons and open
source contributor
• blogger at https://asoldatenko.com
@a_soldatenko
cat /etc/passwd
…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 announced
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
@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
@a_soldatenko
From Apr 18, 2017 AWS Lambda
Supports Python 3.6
Face detection
Example:
Using binaries with your
function
yum update -y
yum install -y git cmake gcc-c++ gcc python-
devel chrpath
mkdir -p lambda-package/cv2 build/numpy
...
pip install opencv-python -t .
...
# Copy template function and zip package
cp template.py lambda-package/lambda_function.py
cd lambda-package
zip -r ../lambda-package.zip *
Prepare wheels
import cv2
def lambda_handler(event, context):
print(“OpenCV installed version:",
cv2.__version__)
return "It works!"
lambda function
START RequestId: 19e6a8f9-4eea-11e7-a662-299188c47179
Version: $LATEST
OpenCV installed version: 3.2.0
END RequestId: 19e6a8f9-4eea-11e7-a662-299188c47179
REPORT RequestId: 19e6a8f9-4eea-11e7-
a662-299188c47179 Duration: 0.46 ms Billed Duration: 100
ms Memory Size: 512 MB Max Memory Used: 35 MB
import importlib
import sys
# Import your lambda python module
mod = importlib.import_module(sys.argv[1])
function_handler = sys.argv[2]
lambda_function = getattr(mod, function_handler )
event = {'name': 'Andrii'}
context = {'conference_name': 'PyCon Israel'}
try:
data = function_handler(event, context)
print(data)
except Exception as error:
print(error)
How to run lambda
locally
$ python local_lambda.py lambda_function lambda_handler
{'message': 'Hello Andrii!'}
How to run lambda
locally
What about
websockets?
A: No, API Gateway doesn’t
support
https://forums.aws.amazon.com/thread.jspa?threadID=205761
AWS IoT Now Supports
WebSockets
http://stesie.github.io/2016/04/aws-iot-pubsub
Thank You
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

Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having Users
Dave Cross
 

What's hot (20)

3h à l'assaut d'une application réactive - Devoxx 2015
3h à l'assaut d'une application réactive - Devoxx 20153h à l'assaut d'une application réactive - Devoxx 2015
3h à l'assaut d'une application réactive - Devoxx 2015
 
Webhooks
WebhooksWebhooks
Webhooks
 
WebHooks in 10 Minutes
WebHooks in 10 MinutesWebHooks in 10 Minutes
WebHooks in 10 Minutes
 
Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having Users
 
Re invent 2018 - The Evolution of AircraftML
Re invent 2018  - The Evolution of AircraftMLRe invent 2018  - The Evolution of AircraftML
Re invent 2018 - The Evolution of AircraftML
 
Yan Cui - How to build observability into a serverless application - Codemoti...
Yan Cui - How to build observability into a serverless application - Codemoti...Yan Cui - How to build observability into a serverless application - Codemoti...
Yan Cui - How to build observability into a serverless application - Codemoti...
 
Monitoring using Sensu
Monitoring using SensuMonitoring using Sensu
Monitoring using Sensu
 
gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!
 
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
 
"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 edition"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 edition
 
"gRPC vs REST: let the battle begin!" DevoxxUK 2018 edition
"gRPC vs REST: let the battle begin!" DevoxxUK 2018 edition"gRPC vs REST: let the battle begin!" DevoxxUK 2018 edition
"gRPC vs REST: let the battle begin!" DevoxxUK 2018 edition
 
Connecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter PlatformConnecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter Platform
 
gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!
 
How WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All ProgrammersHow WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All Programmers
 
Spacebrew Server Workshop @ ITP
Spacebrew Server Workshop @ ITPSpacebrew Server Workshop @ ITP
Spacebrew Server Workshop @ ITP
 
"gRPC vs REST: let the battle begin!" GeeCON Krakow 2018 edition
"gRPC vs REST: let the battle begin!" GeeCON Krakow 2018 edition"gRPC vs REST: let the battle begin!" GeeCON Krakow 2018 edition
"gRPC vs REST: let the battle begin!" GeeCON Krakow 2018 edition
 
APIs That Make Things Happen
APIs That Make Things HappenAPIs That Make Things Happen
APIs That Make Things Happen
 
OSCON 2019 "Break me if you can: practical guide to building fault-tolerant s...
OSCON 2019 "Break me if you can: practical guide to building fault-tolerant s...OSCON 2019 "Break me if you can: practical guide to building fault-tolerant s...
OSCON 2019 "Break me if you can: practical guide to building fault-tolerant s...
 
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019
 
Cómo construir pipelines para streaming de datos en visualizaciones: Un ejemp...
Cómo construir pipelines para streaming de datos en visualizaciones: Un ejemp...Cómo construir pipelines para streaming de datos en visualizaciones: Un ejemp...
Cómo construir pipelines para streaming de datos en visualizaciones: Un ejemp...
 

Similar to Building Serverless applications with Python

BigDataFest Building Modern Data Streaming Apps
BigDataFest  Building Modern Data Streaming AppsBigDataFest  Building Modern Data Streaming Apps
BigDataFest Building Modern Data Streaming Apps
ssuser73434e
 

Similar to Building Serverless applications with Python (20)

Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 
Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24
Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24
Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24
 
Building serverless-applications
Building serverless-applicationsBuilding serverless-applications
Building serverless-applications
 
Apache StreamPipes – Flexible Industrial IoT Management
Apache StreamPipes – Flexible Industrial IoT ManagementApache StreamPipes – Flexible Industrial IoT Management
Apache StreamPipes – Flexible Industrial IoT Management
 
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
Microservices Application Tracing Standards and Simulators - Adrians at OSCONMicroservices Application Tracing Standards and Simulators - Adrians at OSCON
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
 
Build a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless FrameworkBuild a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless Framework
 
Swift Cloud Workshop - Swift Microservices
Swift Cloud Workshop - Swift MicroservicesSwift Cloud Workshop - Swift Microservices
Swift Cloud Workshop - Swift Microservices
 
PyCon Canada 2015 - Is your python application secure
PyCon Canada 2015 - Is your python application securePyCon Canada 2015 - Is your python application secure
PyCon Canada 2015 - Is your python application secure
 
BigDataFest Building Modern Data Streaming Apps
BigDataFest  Building Modern Data Streaming AppsBigDataFest  Building Modern Data Streaming Apps
BigDataFest Building Modern Data Streaming Apps
 
Serverless, The Middy Way - Workshop
Serverless, The Middy Way - WorkshopServerless, The Middy Way - Workshop
Serverless, The Middy Way - Workshop
 
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Conf42 Python_ ML Enhanced Event Streaming Apps with Python MicroservicesConf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
 
Is your python application secure? - PyCon Canada - 2015-11-07
Is your python application secure? - PyCon Canada - 2015-11-07Is your python application secure? - PyCon Canada - 2015-11-07
Is your python application secure? - PyCon Canada - 2015-11-07
 
2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open Standards2022 APIsecure_Securing APIs with Open Standards
2022 APIsecure_Securing APIs with Open Standards
 
AppSec EU 2009 - HTTP Parameter Pollution by Luca Carettoni and Stefano di P...
AppSec EU 2009 - HTTP Parameter Pollution by Luca Carettoni and  Stefano di P...AppSec EU 2009 - HTTP Parameter Pollution by Luca Carettoni and  Stefano di P...
AppSec EU 2009 - HTTP Parameter Pollution by Luca Carettoni and Stefano di P...
 
API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015API Documentation Workshop tcworld India 2015
API Documentation Workshop tcworld India 2015
 
Distributing UI Libraries: in a post Web-Component world
Distributing UI Libraries: in a post Web-Component worldDistributing UI Libraries: in a post Web-Component world
Distributing UI Libraries: in a post Web-Component world
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
 
What's Rio 〜Standalone〜
What's Rio 〜Standalone〜What's Rio 〜Standalone〜
What's Rio 〜Standalone〜
 
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)
 
All You Need to Know to Deploy Applications on Kubernetes
All You Need to Know to Deploy Applications on KubernetesAll You Need to Know to Deploy Applications on Kubernetes
All You Need to Know to Deploy Applications on Kubernetes
 

More from Andrii Soldatenko

More from Andrii Soldatenko (11)

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?
 
Practical continuous quality gates for development process
Practical continuous quality gates for development processPractical continuous quality gates for development process
Practical continuous quality gates for development process
 
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
 
PyCon Ukraine 2014
PyCon Ukraine 2014PyCon Ukraine 2014
PyCon Ukraine 2014
 
SeleniumCamp 2015 Andrii Soldatenko
SeleniumCamp 2015 Andrii SoldatenkoSeleniumCamp 2015 Andrii Soldatenko
SeleniumCamp 2015 Andrii Soldatenko
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

Building Serverless applications with Python

  • 1. Building Serverless applications with Python3 Andrii Soldatenko 12 June 2017 @a_soldatenko
  • 2. Andrii Soldatenko • Senior Python Developer at Toptal • Speaker at many PyCons and open source contributor • blogger at https://asoldatenko.com @a_soldatenko cat /etc/passwd
  • 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 announced AWS Lambda https://techcrunch.com/2015/11/24/aws-lamda-makes- serverless-applications-a-reality/
  • 14. How Lambda works - function name; - memory size - timeout; - role; @a_soldatenko
  • 15. Your first λ function def lambda_handler(event, context): message = 'Hello {}!'.format(event['name']) return {'message': message} @a_soldatenko
  • 16. 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
  • 17. Invoke λ function aws lambda invoke --function-name HelloPython --payload '{"name":"PyCon Italy"}' output.txt cat output.txt {"message": "Hello PyCon Italy!"}% @a_soldatenko
  • 18. 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
  • 20. 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
  • 21. ...
 ...
 ...
 @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
  • 22. 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
  • 23. 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
  • 24. Chalice under the hood @a_soldatenko - botocore; - typing;
  • 25. Chalice under the hood @a_soldatenko
  • 29. AWS Lambda without any costs @a_soldatenko - The first 400,000 seconds of execution time with 1 GB of memory
  • 30. What if you want to run your Django app? @a_soldatenko
  • 33. @a_soldatenko And again what do you mean “serveless”?
  • 34. @a_soldatenko ➜ pip install zappa ➜ zappa init ███████╗ █████╗ ██████╗ ██████╗ █████╗ ╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║ ███████╗██║ ██║██║ ██║ ██║ ██║ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ Welcome to Zappa! ... ➜ zappa deploy https://github.com/Miserlou/Zappa
  • 35. @a_soldatenkohttps://github.com/Miserlou/Zappa - deploy; - tailing logs; - run django manage.py
 … ███████╗ █████╗ ██████╗ ██████╗ █████╗ ╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║ ███████╗██║ ██║██║ ██║ ██║ ██║ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝
  • 38. Python 2.7 will retire in... @a_soldatenkohttps://pythonclock.org/
  • 39. 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
  • 40. 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)
  • 41. 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!!:
  • 42. Run python 3 from python 2 @a_soldatenko
  • 43. 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
  • 44. 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)
  • 45. 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
  • 46. Thanks Lyndon Swan for ideas about hacking python 3 @a_soldatenko
  • 47. 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
  • 48. @a_soldatenko From Apr 18, 2017 AWS Lambda Supports Python 3.6
  • 50. Using binaries with your function
  • 51. yum update -y yum install -y git cmake gcc-c++ gcc python- devel chrpath mkdir -p lambda-package/cv2 build/numpy ... pip install opencv-python -t . ... # Copy template function and zip package cp template.py lambda-package/lambda_function.py cd lambda-package zip -r ../lambda-package.zip * Prepare wheels
  • 52. import cv2 def lambda_handler(event, context): print(“OpenCV installed version:", cv2.__version__) return "It works!" lambda function
  • 53. START RequestId: 19e6a8f9-4eea-11e7-a662-299188c47179 Version: $LATEST OpenCV installed version: 3.2.0 END RequestId: 19e6a8f9-4eea-11e7-a662-299188c47179 REPORT RequestId: 19e6a8f9-4eea-11e7- a662-299188c47179 Duration: 0.46 ms Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 35 MB
  • 54.
  • 55. import importlib import sys # Import your lambda python module mod = importlib.import_module(sys.argv[1]) function_handler = sys.argv[2] lambda_function = getattr(mod, function_handler ) event = {'name': 'Andrii'} context = {'conference_name': 'PyCon Israel'} try: data = function_handler(event, context) print(data) except Exception as error: print(error) How to run lambda locally
  • 56. $ python local_lambda.py lambda_function lambda_handler {'message': 'Hello Andrii!'} How to run lambda locally
  • 57. What about websockets? A: No, API Gateway doesn’t support https://forums.aws.amazon.com/thread.jspa?threadID=205761
  • 58. AWS IoT Now Supports WebSockets http://stesie.github.io/2016/04/aws-iot-pubsub