PyConIT 2018 Writing and deploying serverless python applications
1. Writing and deploying serverless
Python applications
César Cárdenas Desales
PyCon Italy, April 2018
2. About me
Software Architect at Webrepublic AG
Python User since v.2.0
Co-organizer of the Swiss Python Summit (next on ~Feb. 2019)
Co-organizer of the Zurich Python User Group
@ccdesales
3. Agenda
- What is serverless?
- Serverless with AWS Lambda
- Serverless the easy way
- Pitfalls and tips
5. Architecture Archeology - Client / Server
A fat do-it-all Server
Mostly on-premises server
Upfront investment
Capacity planning
Scaling was a nightmare
Server is a pet
6. Cloud based architectures
+ Servers can be disposable, unless
they’re snowflakes
+ Application architecture can make
scaling easier or more difficult
- Capacity planning still required
- Still pay ($$) if your server is idle
Servers are nameless cattle
7. Capacity planning
How many servers?
How powerful?
How much memory?
What do we do with excess resources?
What if we have too little resources?
How do we (dynamically) grow or shrink resources?
Resources become obsolete
What if we suffer the Slashdot effect?
8. Serverless: a very simple yet powerful idea
● You submit units of work for execution
● Pay only for computing time you consume
● Aka. Function as a Service (FaaS)
10. What does Serverless mean for you?
● Less Work
○ No need to run servers in premises
○ No need to manage servers in the cloud (admin, patches)
○ No need to plan capacity or scaling
● Potential money savings
○ Only pay for computing time you use
11. Why does it make me excited
● I’m an aging developer
12. Why does serverless make me excited?
● Potentially unlimited scaling
● Computing resources are
disposable
● Low level maintenance is
abstracted out…
● (it’s somebody else’s problem)
33. Precondition to run the exercises
tree ~/.aws cat ~/.aws/config
[default]
region = eu-west-1
cat ~/.aws/credentials
[default]
aws_access_key_id = SOME_ID
aws_secret_access_key = AKEY
35. What is zappa?
● A python framework for serverless applications
● Used as command line tool
● Manages application lifecycle
○ Creation
○ Deploy
○ Update
○ Undeploy Configure Deploy Test
37. zappa init
● Optional
● Initializes project
● Creates zappa_settings.json file
● YAML format also available
Configure Deploy Test
38. Zappa code looks like Flask
● example.py
● zappa_settings.json
Configure Deploy Test
39. zappa deploy dev
Calling deploy for stage dev..
Downloading and installing dependencies..
Packaging project as zip.
Uploading servless-dev-1506419080.zip (5.9MiB)..
100%|███████████████| 6.16M/6.16M [00:01<00:00, 2.16MB/s]
Scheduling..
Scheduled servless-dev-zappa-keep-warm-handler.keep_warm_callback
with expression rate(4 minutes)!
TestConfigure Deploy Test
40. zappa deploy dev
Uploading servless-dev-template-1506419092.json (1.6KiB)..
100%|███████████████| 1.61K/1.61K [00:00<00:00, 3.71KB/s]
Waiting for stack servless-dev to create (this can take a bit)..
75%|███████████▎ | 3/4 [00:09<00:04, 4.94s/res]
Deploying API Gateway..
Deployment complete!:
https://x0doi1ioid.execute-api.eu-west-1.amazonaws.com/dev
TestConfigure Deploy Test
41. zappa deploy dev
Uploading servless-dev-template-1506419092.json (1.6KiB)..
100%|███████████████| 1.61K/1.61K [00:00<00:00, 3.71KB/s]
Waiting for stack servless-dev to create (this can take a bit)..
75%|███████████▎ | 3/4 [00:09<00:04, 4.94s/res]
Deploying API Gateway..
Deployment complete!:
https://x0doi1ioid.execute-api.eu-west-1.amazonaws.com/dev
TestConfigure Deploy Test
44. Other commands
zappa tail
● Show latest logs
zappa update dev
● Update code and
settings
zappa undeploy dev
● Remove lambda
function
● Remove AWS API
Gateway
48. Why API Gateway?
● Interoperability
○ It’s only JSON over HTTP
○ Other platforms
○ Other AWS technologies
○ Other languages
○ Clients not tied to boto
● RESTful API
● Request Throttling/Limits
● Authorization tokens
50. Pitfalls
● 300 seconds execution time limit
● Calls must be stateless
● Cold start for 1st request -> Keep warm calls
○ An computing instance must be started
○ Can also happen during low traffic periods
● Logging only to CloudWatch
51. Pitfalls
● Fragmentation in a myriad of microservices
● No (DB) connection pooling
● Data potentially leaves your country
● Flame wars
○ 1 (Git) repository per service
○ All services in 1 (Git) repository
● Potential vendor lock-in
● Flow control and error handling gets tricky
60. AWS Lambda pricing
Requests
First 1 million requests
per month are free
$0.20 per 1 million
requests thereafter
($0.0000002 per request)
Duration
Billing rounded up to the
nearest 100ms
Depending on the
amount of allocated
memory
Other
Data transfer (external)
Data storage
AWS API Gateway, $3.50
per million API calls
received
61. Alternatives
Vendor
Google Cloud Functions
(Beta, only JS, no env
vars)
Microsoft Azure
Functions (python only
experimental)
Python libs
Chalice
Apex
Vendor agnostic
The Serverless
framework
https://goo.gl/yYh2M4
62. Shameless plug
● Swiss Python Summit 2019
● February, 2019
● Most likely close to Zurich
● www.python-summit.ch