Introduciendo Serverless en
Proyectos Python
Carlos Hernando
@chernando
Serverless Hype!
https://trends.google.com/trends/explore?date=2014-01-01%202017-08-27&q=serverless
Serverless
Saas / BaaS
Está usted aquí
Nuestra Aplicación
def handler(event, context):
# PONGA SU CÓDIGO AQUÍ
return
AWS Lambda
Serverless
https://aws.amazon.com/serverless/
Arquitectura Serverless
https://medium.com/cloud-academy-inc/serverless-beyond-functions-cd81ee4c6b8d
Escenarios
Scripts de “mantenimiento”
AWS Lambda
def handler(event, context):
# PONGA SU CÓDIGO AQUÍ
return
AWS Lambda
Desarrolladores
Credenciales
Base de datos
Servidores
Empaquetar
$ cd venv/lib/python3.6/site-packages/
$ zip -r9 ../../../../deploy.zip *
$ cd -
$ zip -g deploy.zip *.py
Crear Lambda
$ aws lambda create-function 
--region eu-west-1 
--function-name SimpleScript 
--zip-file fileb://deploy.zip 
--role arn:aws:iam::XXX:role/MyLambdaRole 
--vpc-config SubnetIds=XXX,SecurityGroupIds=XXX 
--handler handler.handler 
--runtime python3.6 
--profile personal 
--timeout 10 
--memory-size 512
Actualizar Lambda
$ aws lambda update-function-code 
--region eu-west-1 
--function-name SimpleScript 
--zip-file fileb://deploy.zip 
--profile personal
Aprendizajes
Tareas periódicas
# Crontab
30 08 10 06 * /root/full-backup
00 11,16 * * * /root/incremental-backup
00 09-18 * * * /root/check-db-status
00 09-18 * * 1-5 /root/send-updates
*/10 * * * * /root/consolidate-reports
@yearly /root/annual-maintenance
@monthly /root/steal-money
Celery workers
Celery Beat
Celery Beat
Celery workers
AWS
Lambda
Cloudwatch
Events
Celery workers
def handler(event, context):
# PONGA SU CÓDIGO AQUÍ
return
>>> from tasks import add
>>> add.delay(4, 4)
>>> from celery import Celery
>>> app = Celery(broker=broker)
>>> app.send_task('tasks.add', args=[4,4])
https://serverless.com
functions:
celeryless:
handler: celeryless.handler
events:
- schedule:
rate: rate(10 minutes)
input:
task: tasks.add
args: [4, 4]
Aprendizajes
APIs a pequeña escala
API Gateway AWS Lambda
SAM: Serverless Application Model
https://github.com/awslabs/serverless-application-model
https://github.com/aws/chalice
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello World!"
from chalice import Chalice
app = Chalice(app_name="helloworld")
@app.route("/")
def index():
return {"hello": "world"}
Aprendizajes
Eventos encadenados
Serverless Image resizing (AWS Lambda and AWS s3)
http://blog.kaliloudiaby.com/index.php/serverless-image-resizing-aws-lambda-and-aws-s3/
De $0,20 a $834
14 horas
$425
25.000.000 GB/s (290 días)
37.000.000 Invocaciones
AWS Lambda
Serverless
Scripts Periódicos API HTTP
https://www.wdl.org/en/item/576/
Documentación de la charla:

https://bit.ly/pymad0917

Carlos Hernando

@chernando
•Photo by Chris Lawton
on Unsplash
•Photo by Maarten van
den Heuvel on Unsplash
•Photo by Markus Spiske
on Unsplash
•Photo by Teresa Kluge
on Unsplash
•Photo by Dan Paul on
Unsplash
•Photo by Mike Alonzo
on Unsplash
Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
https://creativecommons.org/licenses/by-sa/4.0/
Imágenes cortesía de:

Introduciendo Serverless en Proyectos Python