+ All Categories
Home > Software > Origins of Serverless

Origins of Serverless

Date post: 21-Jan-2018
Category:
Upload: andrii-soldatenko
View: 85 times
Download: 0 times
Share this document with a friend
77
Origins of Serverless Andrii Soldatenko 10-11 November 2017 @a_soldatenko
Transcript
Page 1: Origins of Serverless

Origins of Serverless

Andrii Soldatenko 10-11 November 2017

@a_soldatenko

Page 2: Origins of Serverless

• Gopher by night and Python dev by day

• Speaker at many conferences and open source contributor github.com/andriisoldatenko

• blogger at https://asoldatenko.com

grep andrii /etc/passwd

Page 3: Origins of Serverless

@a_soldatenkohttps://asoldatenko.com/

Page 4: Origins of Serverless
Page 5: Origins of Serverless

@a_soldatenkohttps://asoldatenko.com/

Page 6: Origins of Serverless

Server

@a_soldatenkohttps://asoldatenko.com/

Page 7: Origins of Serverless

ЛЕС

Page 8: Origins of Serverless

SERVERLESS

Page 9: Origins of Serverless

…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.

James Governor Analyst & Co-founder at RedMonk

@a_soldatenkohttps://asoldatenko.com/

Page 10: Origins of Serverless

Origins of “Serverless”

@a_soldatenkohttps://asoldatenko.com/

Page 11: Origins of Serverless

Origins of serverless

http://readwrite.com/2012/10/15/why-the-future-of-software-and-apps-is-serverless/

Page 12: Origins of Serverless

https://asoldatenko.com/building-serverless-applications-with-python-3.html

Page 13: Origins of Serverless

Origins of serverless

Page 14: Origins of Serverless

Origins of serverless

Page 15: Origins of Serverless

Origins of serverless

Page 16: Origins of Serverless

Origins of serverless

Page 17: Origins of Serverless

Travis CI

@a_soldatenkohttps://asoldatenko.com/

Page 18: Origins of Serverless

Later in 2014…

Page 19: Origins of Serverless
Page 20: Origins of Serverless

Amazon announced AWS Lambda

https://techcrunch.com/2015/11/24/aws-lamda-makes-serverless-applications-a-reality/

Page 21: Origins of Serverless

AWS Lambda is one of the most popular implementations of FaaS

(Functions as a service / FaaS)

https://martinfowler.com/articles/serverless.html

Page 22: Origins of Serverless

How Lambda (λ) works

@a_soldatenko

Page 23: Origins of Serverless

Traditionally the architecture

Page 24: Origins of Serverless

Serverless architecture

Page 25: Origins of Serverless

How Lambda works

@a_soldatenko

Page 26: Origins of Serverless

How Lambda works

@a_soldatenko

Page 27: Origins of Serverless

Runtimes

@a_soldatenko

Page 28: Origins of Serverless

How Lambda works

- function name; - memory size; - timeout; - role;

@a_soldatenko

Page 29: Origins of Serverless

Your first λ function

def lambda_handler(event, context): message = 'Hello {}!'.format(event['name']) return {'message': message}

@a_soldatenko

Page 30: Origins of Serverless

Deploy λ function#!/usr/bin/env bashpython -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

Page 31: Origins of Serverless

Invoke λ function

aws lambda invoke \ --function-name HelloPython \ --payload ‘{"name":"XP Days!”}’ output.txt

cat output.txt{"message": "Hello XP Days!"}%

@a_soldatenko

Page 32: Origins of Serverless

Amazon API GatewayResource HTTP verb AWS Lambda

/books GET get_books

/book POST create_book

/books/{ID} PUT chage_book

/books/{ID} DELETE delete_book

Page 33: Origins of Serverless
Page 34: Origins of Serverless

Chalice python micro framework

https://github.com/awslabs/chalice

@a_soldatenko

Page 35: Origins of Serverless

Chalice python micro framework

$ cat app.py from chalice import Chalice

app = Chalice(app_name='helloxpdays')

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

https://github.com/awslabs/chalice

Page 36: Origins of Serverless

...

...

... @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

Page 37: Origins of Serverless

Chalice deploy

chalice deployUpdating IAM policy.Updating lambda function.........API Gateway rest API already found.Deploying to: devhttps://8rxbsnge8d.execute-api.us-west-2.amazonaws.com/dev/

@a_soldatenko

Page 38: Origins of Serverless

Try our books API

curl -X GET https://8rxbsnge8d.execute-api.us-west-2.amazonaws.com/dev/books{"hello": "from python library"}%

curl -X GET https://8rxbsnge8d.execute-api.us-west-2.amazonaws.com/dev/books/15{"message": "Book 15"}%

curl -X DELETE https://8rxbsnge8d.execute-api.us-west-2.amazonaws.com/dev/books/15{"message": "Book 15 has been deleted"}%

@a_soldatenko

Page 39: Origins of Serverless

Chalice under the hood

@a_soldatenko

Page 40: Origins of Serverless

AWS Serverless Application Model

@a_soldatenko

Page 41: Origins of Serverless

AWS SAM

@a_soldatenko

Page 42: Origins of Serverless

AWS lambda Limits

@a_soldatenko

Page 43: Origins of Serverless

What about conferences?

http://serverlessconf.io/

Page 44: Origins of Serverless

AWS Lambda costs

@a_soldatenko

- The first 400,000 seconds of execution time with 1 GB of memory

Page 45: Origins of Serverless

One missing return; ended up costing me $206.

https://sourcebox.be/blog/2017/08/07/serverless-a-lesson-learned-the-hard-way/

Page 46: Origins of Serverless

@a_soldatenko

And again what do you mean “serveless”?

Page 47: Origins of Serverless

Serverless - это сервер который живет 40 миллисекунд

Page 48: Origins of Serverless

What if you want to run your Django or any WSGI app?

@a_soldatenko

Page 49: Origins of Serverless

@a_soldatenko

Page 50: Origins of Serverless

@a_soldatenko

Page 51: Origins of Serverless

@a_soldatenko

➜ pip install zappa➜ zappa init

███████╗ █████╗ ██████╗ ██████╗ █████╗╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║███████╗██║ ██║██║ ██║ ██║ ██║╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝

Welcome to Zappa!...➜ zappa deploy

https://github.com/Miserlou/Zappa

Page 52: Origins of Serverless

@a_soldatenkohttps://github.com/Miserlou/Zappa

- deploy; - tailing logs; - run django manage.py

███████╗ █████╗ ██████╗ ██████╗ █████╗╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ███╔╝ ███████║██████╔╝██████╔╝███████║ ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║███████╗██║ ██║██║ ██║ ██║ ██║╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝

Page 53: Origins of Serverless

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

Page 54: Origins of Serverless

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)

Page 55: Origins of Serverless

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!!:

Page 56: Origins of Serverless

Run python 3 from python 2

@a_soldatenko

Page 57: Origins of Serverless

Prepare Python 3 for AWS Lambda

virtualenv venv -p `which python3.4`

pip install requests

zip -r lambda_python3.zip venv

python3_program.py lambda.py

@a_soldatenko

Page 58: Origins of Serverless

Run python 3 from python 2

@a_soldatenko

cat lambda.pyimport 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)

Page 59: Origins of Serverless

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

Page 60: Origins of Serverless

Thanks Lyndon Swan for ideas about hacking

python 3

@a_soldatenko

Page 61: Origins of Serverless

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

Page 62: Origins of Serverless

@a_soldatenko

From Apr 18, 2017 AWS Lambda Supports Python 3.6

Page 63: Origins of Serverless

Face detection Example:

Page 64: Origins of Serverless

Using binaries with your function

Page 65: Origins of Serverless

yum update -yyum install -y git cmake gcc-c++ gcc python-devel chrpathmkdir -p lambda-package/cv2 build/numpy...

pip install opencv-python -t ....# Copy template function and zip packagecp template.py lambda-package/lambda_function.pycd lambda-packagezip -r ../lambda-package.zip *

Prepare wheels

Page 66: Origins of Serverless

import cv2

def lambda_handler(event, context):print(“OpenCV installed version:", cv2.__version__)return "It works!"

lambda function

Page 67: Origins of Serverless

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

Page 68: Origins of Serverless
Page 69: Origins of Serverless

Debug lambda locallyimport importlib; sys

# Import your lambda python modulemod = importlib.import_module(sys.argv[1])function_handler = sys.argv[2]

lambda_function = getattr(mod, function_handler )

event = {'name': 'Andrii'}context = {'conference_name': ‘XP Days!’}

try: data = function_handler(event, context) print(data)except Exception as error: print(error) https://goo.gl/2VNPyA

Page 70: Origins of Serverless

How to run lambda locally

$ python local_lambda.py lambda_function \ lambda_handler

{'message': 'Hello Andrii!'}

Page 71: Origins of Serverless

Conclusion

Page 72: Origins of Serverless

Reduce operational cost and complexity

Patrick Brandt Solutions Architect at The Coca-Cola Company

Page 73: Origins of Serverless

What about websockets?

A: 2 years later and nothing yet on this?

https://forums.aws.amazon.com/thread.jspa?threadID=205761

Page 74: Origins of Serverless

AWS IoT Now Supports WebSockets

http://stesie.github.io/2016/04/aws-iot-pubsub

Page 75: Origins of Serverless

Serverless are the new NoSQL 🎉💩

- everybody claims it's the next big thing

- a few people have actually worked with the technology

- and in the end it's just a crippled (one-way) database

Page 76: Origins of Serverless

Thank You

https://asoldatenko.com

@a_soldatenko

Page 77: Origins of Serverless

Questions

?@a_soldatenko


Recommended