Upload
amazon-web-services
View
165
Download
8
Embed Size (px)
Citation preview
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
James Saryerwinnie / AWS
December 2, 2016
DEV308
ChaliceA Serverless Microframework for Python
Agenda
Serverless REST APIs
Getting started with chalice
Chalice sample applications with demos
Assumptions
{…} REST API
REST APIs
REST API
Client
Mobile client
REST APIs
REST API
REST APIs
GET /users HTTP/1.1
{"users": […]}
REST API
REST APIs
Amazon
DynamoDB
Memcached
Amazon
CloudWatch
Auto Scaling group
Security group
Elastic Load
Balancing
Instance
REST API
REST APIs
Amazon
DynamoDB
Memcached
Amazon
CloudWatch
Serverless REST APIs
Auto Scaling group
Security group
Instance
Elastic Load
Balancing
REST API
REST API
Amazon API Gateway
AWS Lambda
Amazon API Gateway
Create, maintain, and secure APIs at any scale
Handles authorization, access control, and monitoring
Acts as a “front door” for applications
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
First serverless API
REST API
First serverless API
GET / HTTP/1.1
{"hello": "world"}
Demo
REST API
First serverless API
GET / HTTP/1.1
{"hello": "world"}
AWS Management Console
What about?
Amazon API Gateway AWS Lambda
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
Introducing chalice
from chalice import Chalice
app = Chalice(app_name='helloworld')
@app.route('/')def index():
return {'hello': 'world'}
app.pyHello World app
from chalice import Chalice
app = Chalice(app_name='helloworld')
@app.route('/')def index():
return {'hello': 'world'}
app.pyHello World app
1. App object
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
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
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/
Demo
Chalice API
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
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
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
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"
}
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
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
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
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
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 {}
@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
Chalice architecture
Lambda runtime and APIs
Command line interface
Configuration, packaging, and deployment
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"}
Client
Lambda runtime and API
Amazon API Gateway AWS Lambda
Chalice
Client
Lambda runtime and API
Amazon API Gateway AWS Lambda
Chalice
Lambda runtime and API
AWS Lambda
Lambda runtime and API
Lambda
handler.py
def lambda_handler(event, context):# Called by lambda runtime.
return 'Hello from Lambda'AWS Lambda
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
Chalice architecture
Lambda runtime and APIs
Command line interface
Configuration, packaging, and deployment
Chalice architecture
CLI
.├── app.py└── requirements.txt
Chalice architecture
CLI
.├── app.py└── requirements.txt
$ chalice deploy
Chalice architecture
CLI
.├── app.py└── requirements.txt
deploy.zip
Chalice architecture
CLI
.├── app.py└── requirements.txt
deploy.zipdeploy.zip
Chalice runtime
Chalice architecture
CLI
.├── app.py└── requirements.txt
deploy.zipdeploy.zip
Chalice runtime
Chalice architecture
CLI
.├── app.py└── requirements.txt
deploy.zip
Chalice architecture
CLI
.├── app.py└── requirements.txt Amazon API Gateway AWS Lambda
What we learned
Setting up chalice
Create a new chalice project
Deploying our app
Chalice routing API
Github bot
Sample application
Github bot flow
Github bot flow
Github bot flow
Github bot flow
Github bot flow
Github bot flow
Github bot flow
Github bot flow
Demo
What we learned
Managing secrets and config
Multifile chalice applications
Configuring debug logs
Viewing Amazon CloudWatch Logs
Single page app
What we learned
Local mode
Javascript SDK generation
Integrating with Amazon Cognito
Wrapping up
Serverless REST APIs
Chalice API
Github bot
Single page app
User pool integration
Remember to complete
your evaluations!
Thank you!
• https://github.com/awslabs/chalice
• http://chalice.readthedocs.io/