aws re:invent 2016: chalice: a serverless microframework for python (dev308)

71
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved. James Saryerwinnie / AWS December 2, 2016 DEV308 Chalice A Serverless Microframework for Python

Upload: amazon-web-services

Post on 06-Jan-2017

165 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

James Saryerwinnie / AWS

December 2, 2016

DEV308

ChaliceA Serverless Microframework for Python

Page 2: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Agenda

Serverless REST APIs

Getting started with chalice

Chalice sample applications with demos

Page 3: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Assumptions

{…} REST API

Page 4: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST APIs

Page 5: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

Client

Mobile client

REST APIs

Page 6: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

REST APIs

GET /users HTTP/1.1

{"users": […]}

Page 7: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

REST APIs

Amazon

DynamoDB

Memcached

Amazon

CloudWatch

Page 8: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Auto Scaling group

Security group

Elastic Load

Balancing

Instance

REST API

REST APIs

Amazon

DynamoDB

Memcached

Amazon

CloudWatch

Page 9: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Serverless REST APIs

Page 10: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Auto Scaling group

Security group

Instance

Elastic Load

Balancing

REST API

Page 11: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

Amazon API Gateway

AWS Lambda

Page 12: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Amazon API Gateway

Create, maintain, and secure APIs at any scale

Handles authorization, access control, and monitoring

Acts as a “front door” for applications

Page 13: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

AWS Lambda

Run code without thinking about servers

Pay for only the compute time you consume

Lambda takes care of everything required to run and

scale your code with high availability

Page 14: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

First serverless API

Page 15: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

First serverless API

GET / HTTP/1.1

{"hello": "world"}

Page 16: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Demo

Page 17: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

REST API

First serverless API

GET / HTTP/1.1

{"hello": "world"}

Page 18: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

AWS Management Console

Page 19: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

What about?

Page 20: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Amazon API Gateway AWS Lambda

Page 21: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

SDK

Lambda.GetFunction(params: {'body': '', 'url': u'httpsIAM.GetRole(params: {'body': {'Action': u'GetRole', 'IAM.CreateRole(params: {'body': {'Action': u'CreateRoleIAM.PutRolePolicy(params: {'body': {'Action': u'PutRolePolicyLambda.CreateFunction(params: (... omitted from logs due to size ...)APIGateway.GetRestApis(params: {'body': '', 'url': APIGateway.CreateRestApi(params: {'body': '{"name": "helloworld7"}', 'APIGateway.GetResources(params: {'body': '', 'url': APIGateway.PutMethod(params: {'body': '{"authorizationTypeAPIGateway.PutIntegration(params: {'body': '{"httpMethod

Page 22: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Introducing chalice

Page 23: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.pyHello World app

Page 24: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.pyHello World app

1. App object

Page 25: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.pyHello World app

1.

2.

App object

Routes

Page 26: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.pyHello World app

1.

2.

App object

Routes

3. File

Page 27: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.pyHello World app

$ chalice deploy

https://dfut7pnl47/dev/

Page 28: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Demo

Page 29: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice API

Page 30: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import BadRequestError

@app.route('/cities/{city}')def state_of_city(city):

try:return {'state': CITIES_TO_STATE[city]}

except KeyError:raise BadRequestError(

"Unknown city '%s', valid choices are: %s" %( city, ', '.join(CITIES_TO_STATE.keys())))

app.pyError handling

Page 31: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import BadRequestError

@app.route('/cities/{city}')def state_of_city(city):

try:return {'state': CITIES_TO_STATE[city]}

except KeyError:raise BadRequestError(

"Unknown city '%s', valid choices are: %s" %( city, ', '.join(CITIES_TO_STATE.keys())))

app.pyError handling

Page 32: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import BadRequestError

@app.route('/cities/{city}')def state_of_city(city):

try:return {'state': CITIES_TO_STATE[city]}

except KeyError:raise BadRequestError(

"Unknown city '%s', valid choices are: %s" %( city, ', '.join(CITIES_TO_STATE.keys())))

app.pyError handling

Page 33: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import BadRequestError

@app.route('/cities/{city}')def state_of_city(city):

try:return {'state': CITIES_TO_STATE[city]}

except KeyError:raise BadRequestError(

"Unknown city '%s', valid choices are: %s" %( city, ', '.join(CITIES_TO_STATE.keys())))

app.pyError handling

HTTP/1.1 400 Bad Request

{"Code": "BadRequestError","Message": "BadRequestError: Unknown city 'vancouver', valid choices are: portland, seattle"

}

Page 34: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import NotFoundError

OBJECTS = {}

@app.route('/objects/{key}', methods=['GET', 'PUT'])def myobject(key):

request = app.current_requestif request.method == 'PUT':

OBJECTS[key] = request.json_bodyelif request.method == 'GET':

try:return {key: OBJECTS[key]}

except KeyError:raise NotFoundError(key)

app.pyRequest metadata

Page 35: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import NotFoundError

OBJECTS = {}

@app.route('/objects/{key}', methods=['GET', 'PUT'])def myobject(key):

request = app.current_requestif request.method == 'PUT':

OBJECTS[key] = request.json_bodyelif request.method == 'GET':

try:return {key: OBJECTS[key]}

except KeyError:raise NotFoundError(key)

app.pyRequest metadata

Page 36: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import NotFoundError

OBJECTS = {}

@app.route('/objects/{key}', methods=['GET', 'PUT'])def myobject(key):

request = app.current_requestif request.method == 'PUT':

OBJECTS[key] = request.json_bodyelif request.method == 'GET':

try:return {key: OBJECTS[key]}

except KeyError:raise NotFoundError(key)

app.pyRequest metadata

Updated every request

Page 37: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

from chalice import NotFoundError

OBJECTS = {}

@app.route('/objects/{key}', methods=['GET', 'PUT'])def myobject(key):

request = app.current_requestif request.method == 'PUT':

OBJECTS[key] = request.json_bodyelif request.method == 'GET':

try:return {key: OBJECTS[key]}

except KeyError:raise NotFoundError(key)

app.pyRequest metadata

Page 38: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

app.pyPet store

@app.route('/')def index():

return {}

@app.route('/login', methods=['POST'])def login():

return {}

@app.route('/pets', methods=['POST', 'GET'])def pets():

return {}

@app.route('/pets/{pet_id}', methods=['GET'])def pet(pet_id):

return {}

@app.route('/users', methods=['POST'])def users():

return {}

Page 39: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

@app.route('/')def index():

return {}

@app.route('/login', methods=['POST'])def login():

return {}

@app.route('/pets', methods=['POST', 'GET'])def pets():

return {}

@app.route('/pets/{pet_id}', methods=['GET'])def pet(pet_id):

return {}

@app.route('/users', methods=['POST'])def users():

return {}

app.pyPet store

Page 40: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

Lambda runtime and APIs

Command line interface

Configuration, packaging, and deployment

Page 41: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Client

Lambda runtime and API

Amazon API Gateway AWS Lambda

GET /users HTTP/1.1 API call

{"hello": "world"}HTTP/1.1 200 OKDate: …

{"hello": "world"}

Page 42: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Client

Lambda runtime and API

Amazon API Gateway AWS Lambda

Chalice

Page 43: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Client

Lambda runtime and API

Amazon API Gateway AWS Lambda

Chalice

Page 44: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Lambda runtime and API

AWS Lambda

Page 45: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Lambda runtime and API

Lambda

handler.py

def lambda_handler(event, context):# Called by lambda runtime.

return 'Hello from Lambda'AWS Lambda

Page 46: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Lambda runtime and API

Lambda

chalice

from chalice import Chalice

app = Chalice(app_name='helloworld')

@app.route('/')def index():

return {'hello': 'world'}

app.py

AWS Lambda

Page 47: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

Lambda runtime and APIs

Command line interface

Configuration, packaging, and deployment

Page 48: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

Page 49: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

$ chalice deploy

Page 50: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

deploy.zip

Page 51: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

deploy.zipdeploy.zip

Chalice runtime

Page 52: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

deploy.zipdeploy.zip

Chalice runtime

Page 53: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt

deploy.zip

Page 54: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Chalice architecture

CLI

.├── app.py└── requirements.txt Amazon API Gateway AWS Lambda

Page 55: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

What we learned

Setting up chalice

Create a new chalice project

Deploying our app

Chalice routing API

Page 56: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot

Sample application

Page 57: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 58: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 59: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 60: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 61: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 62: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 63: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 64: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Github bot flow

Page 65: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Demo

Page 66: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

What we learned

Managing secrets and config

Multifile chalice applications

Configuring debug logs

Viewing Amazon CloudWatch Logs

Page 67: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Single page app

Page 68: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

What we learned

Local mode

Javascript SDK generation

Integrating with Amazon Cognito

Page 69: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Wrapping up

Serverless REST APIs

Chalice API

Github bot

Single page app

User pool integration

Page 70: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Remember to complete

your evaluations!

Page 71: AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)

Thank you!

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

• http://chalice.readthedocs.io/