Build a RESTfull API with the
Serverless framework
Plone Conference WebDay 2018 Nov 7th 1
Introduction
1. Introduce myself
2. perpose of this presentation
Plone Conference WebDay 2018 Nov 7th 2
Introduce myself
• Masato Nakamura
• twitter: masahito
• github: masahitojp
• work at Nulab inc. Typetalk team
• use Scala and JavaScript and Python
• Python ❤ and
☕
❤
Plone Conference WebDay 2018 Nov 7th 3
Perpose of this presentation
• You can understand buid a REST API with the
Serverless Framework
• I will talk especially for the AWS services
Plone Conference WebDay 2018 Nov 7th 4
Not covering today
• Technical Details
• AWS knowledge(IAM etc)
Plone Conference WebDay 2018 Nov 7th 5
Today I will talk about following
things
• How to create an RESTful API with the Amazon API
Gateway
• How to use the Serverless Framework
• How to make deployment easier
Plone Conference WebDay 2018 Nov 7th 6
How to create RESTful API
Plone Conference WebDay 2018 Nov 7th 7
How to create RESTful API
1. Where to use RESTful API?
2. What is the Amazon API Gateway
3. What is AWS Lambda
Plone Conference WebDay 2018 Nov 7th 8
Where to use RESTful API?
• Upload File to the Amazon S3
• Send Message to a service
Plone Conference WebDay 2018 Nov 7th 9
Target
• You use CMS
• You want to make API more easily
• I want you to introduce the Amazon API Gateway
Plone Conference WebDay 2018 Nov 7th 10
What is the Amazon API Gateway
API creation, application communication gateway.
Create an endpoint.
It is created from the management console of AWS.
We do not write actual code.
It also has an authentication function, it separates
the environment, deploys and scales.
Plone Conference WebDay 2018 Nov 7th 11
What is the AWS Lambda
A service that executes code.
Create a function called Lambda function and write
code.
You can write it directly in the AWS management
console, but basically upload the code.
You can write even code, AWS Lamvs provide
manage servers, scaling, logging.
Plone Conference WebDay 2018 Nov 7th 12
What you can do with combining
We can create RESTful API ❤
Plone Conference WebDay 2018 Nov 7th 13
Flow of API creation
1. create the Amazon API Gateway
2. create AWS Lambda funtion
3. connect AWS GateWay & AWS Lambda funtion
Plone Conference WebDay 2018 Nov 7th 14
1). create the Amazon API Gateway
Plone Conference WebDay 2018 Nov 7th 15
2). create AWS Lambda funtion
Plone Conference WebDay 2018 Nov 7th 16
3). connect the Amazon API Gateway & AWS Lambda
funtion
Plone Conference WebDay 2018 Nov 7th 17
Next
I want to talk about the AWS Lambda
Plone Conference WebDay 2018 Nov 7th 18
How to use the Serverless
framework
Plone Conference WebDay 2018 Nov 7th 19
How to use the Serverless
framework
1. How to deploy AWS Lambda function.
2. Introduce the Serverless Framework
Plone Conference WebDay 2018 Nov 7th 20
Target
• Python newbie
• use or used the AWS Lambda
Plone Conference WebDay 2018 Nov 7th 21
Goal
•
!
Easy to use AWS Lambda for Python
• When you write Code, You can update easily
Plone Conference WebDay 2018 Nov 7th 22
can register at AWS console
• Write Code
• Run Test
Plone Conference WebDay 2018 Nov 7th 23
When used only once This is enough
Plone Conference WebDay 2018 Nov 7th 24
• example: POST JSON -> send message to Chat
Plone Conference WebDay 2018 Nov 7th 25
• example: POST JSON -> send message to Chat
•
!
We want to use a 3rd Party lib
• requests
• add requirements.txt
requests
• run
$ pip install -r requirements.txt -t vendor/
$ edit python-file
$ zip /path/to/service-dir
$ aws lambda ~~
Plone Conference WebDay 2018 Nov 7th 26
  It's troublesome to do with the
AWS console
!
use 3rd party libraries
!
Especially when using C-API
!
Code version control (git etc)
Plone Conference WebDay 2018 Nov 7th 27
Deploy to the AWSLambda
• Deployment using ZipFile is possible
!
Code version control (git etc)
Plone Conference WebDay 2018 Nov 7th 28
• Deployment using ZipFile is possible
$ aws lambda create-function 
--function-name AccessMemCache 
--region us-east-1 
--zip-file fileb://path-to/app.zip 
--role execution-role-arn 
--handler app.handler 
--runtime python3.6 
--timeout 30 
--vpc-config SubnetIds=comma-separated-vpc-subnet-ids,SecurityGroupIds=default-security-group-id 
--memory-size 1024
https://docs.aws.amazon.com/ja_jp/lambda/latest/
dg/vpc-ec-upload-deployment-pkg.html
Plone Conference WebDay 2018 Nov 7th 29
I want to make it easier
!
Create a zipfile each time you change -> deploy
!
Run test locally
• What we need now
$ edit python-file
$ (pip install ~~)
$ zip /path/to/service-dir
$ aws lambda ~~
Plone Conference WebDay 2018 Nov 7th 30
Introduce the Serverless Framework
Plone Conference WebDay 2018 Nov 7th 31
What is the Serverless Framework?
• http://www.serverless.com
Serverless Framework – Build web, mobile and IoT
applications with serverless architectures using AWS
Lambda, Azure Functions, Google CloudFunctions &
more!
• AWS Lambda, Azure Functions, Google
CloudFunctions を使ってサーバレスアーキテク
チャで web, Mobile IoT アプリケーションを作ろう
Plone Conference WebDay 2018 Nov 7th 32
Description of the Serverless Framework
• pros
!!
Code version control(git / svn / Hg)
!
zip -> deploy is possible by executing Only 1
command
!
manage CloudWatch/ AWS IAM
Plone Conference WebDay 2018 Nov 7th 33
Use the Serverless Framework
How to create environments
$ npm instal -g serverless
$ sls -v
1.32.0
Plone Conference WebDay 2018 Nov 7th 34
create AWS Lambda with the Serverless Framework
$ sls create -t aws-python3 -p py3-hello
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/masahito-nulab/src/jobs/typetalk-serverless-internal/py3-hello"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| ___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v1.27.3
-------'
Serverless: Successfully generated boilerplate for template: "aws-python3"
$ tree py3-hello/
py3-hello/
!"" handler.py
#"" serverless.yml
Plone Conference WebDay 2018 Nov 7th 35
• handler.py
import json
def hello(event, context):
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
• serverless.yml
service: py3-hello
provider:
name: aws
runtime: python3.6
functions:
hello:
handler: handler.hello
Plone Conference WebDay 2018 Nov 7th 36
Run Test locally
$ cd py3-hello
$ sls invoke local -f hello
{
"statusCode": 200,
"body": "{"message": "Go Serverless v1.0! Your function executed successfully!", "input": {}}"
}
Plone Conference WebDay 2018 Nov 7th 37
Deploy to AWS Lambda
!
Deployment will be simplified after setting the
IAM/Role
$ sls deploy
Plone Conference WebDay 2018 Nov 7th 38
How to make deployment
easier
Plone Conference WebDay 2018 Nov 7th 39
How to make deployment easier
1. reduce troublesome for library installation
2. Non-Pure Python module
Plone Conference WebDay 2018 Nov 7th 40
Goal
• Easy to use AWS Lambda for Python
•
!
When you write Code, You can update easily
Plone Conference WebDay 2018 Nov 7th 41
Plone Conference WebDay 2018 Nov 7th 42
reduce troublesome for library installation
!
required 3rd-party library installation
#
!
I often forget this
$ pip install -r requirement.txt -t vendor/
$ sls deploy
Plone Conference WebDay 2018 Nov 7th 43
reduce troublesome for library installation
Use serverless-framework plugin
• UnitedIncome/serverless-python-requirements
• serverless >= v1.12
Plone Conference WebDay 2018 Nov 7th 44
How to install this plugin
$ sls plugin install -n serverless-python-requirements
service: py3-hello
provider:
name: aws
runtime: python3.6
cfLogs: true
plugins:
- serverless-python-requirements
functions:
hello:
handler: handler.hello
Plone Conference WebDay 2018 Nov 7th 45
Deploy
($ sls requirements install)
$ sls deploy
Plone Conference WebDay 2018 Nov 7th 46
It is now possible to execute with a only 1 command !
Plone Conference WebDay 2018 Nov 7th 47
Non-Pure Python module
• Developers often use Windows / OSX for
development environment
• Python has lots of libraries written with C-API
• numpy , TensorFlow , Image Libraries: pillow etc
• Creating a zipfile locally does not work at AWS
Lambda
!
• The environment of AWS Lambda is Linux
Plone Conference WebDay 2018 Nov 7th 48
dockerize pip
Cross-compile in docker environment
• It uses docker image ”lambci / docker-lambda"
service: py3-hello
provider:
name: aws
runtime: python3.6
cfLogs: true
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
functions:
hello:
handler: handler.hello
Plone Conference WebDay 2018 Nov 7th 49
Today I talked about following things
• How to create an RESTful API with the Amazon API
Gateway
• How to use the Serverless Framework
• How to make deployment easier
Plone Conference WebDay 2018 Nov 7th 50
I hope you can write and
deploy RESTfull API ❤
Plone Conference WebDay 2018 Nov 7th 51
Thank you
@masahito
Plone Conference WebDay 2018 Nov 7th 52
Question?
Plone Conference WebDay 2018 Nov 7th 53

Build a RESTful API with the Serverless Framework

  • 1.
    Build a RESTfullAPI with the Serverless framework Plone Conference WebDay 2018 Nov 7th 1
  • 2.
    Introduction 1. Introduce myself 2.perpose of this presentation Plone Conference WebDay 2018 Nov 7th 2
  • 3.
    Introduce myself • MasatoNakamura • twitter: masahito • github: masahitojp • work at Nulab inc. Typetalk team • use Scala and JavaScript and Python • Python ❤ and ☕ ❤ Plone Conference WebDay 2018 Nov 7th 3
  • 4.
    Perpose of thispresentation • You can understand buid a REST API with the Serverless Framework • I will talk especially for the AWS services Plone Conference WebDay 2018 Nov 7th 4
  • 5.
    Not covering today •Technical Details • AWS knowledge(IAM etc) Plone Conference WebDay 2018 Nov 7th 5
  • 6.
    Today I willtalk about following things • How to create an RESTful API with the Amazon API Gateway • How to use the Serverless Framework • How to make deployment easier Plone Conference WebDay 2018 Nov 7th 6
  • 7.
    How to createRESTful API Plone Conference WebDay 2018 Nov 7th 7
  • 8.
    How to createRESTful API 1. Where to use RESTful API? 2. What is the Amazon API Gateway 3. What is AWS Lambda Plone Conference WebDay 2018 Nov 7th 8
  • 9.
    Where to useRESTful API? • Upload File to the Amazon S3 • Send Message to a service Plone Conference WebDay 2018 Nov 7th 9
  • 10.
    Target • You useCMS • You want to make API more easily • I want you to introduce the Amazon API Gateway Plone Conference WebDay 2018 Nov 7th 10
  • 11.
    What is theAmazon API Gateway API creation, application communication gateway. Create an endpoint. It is created from the management console of AWS. We do not write actual code. It also has an authentication function, it separates the environment, deploys and scales. Plone Conference WebDay 2018 Nov 7th 11
  • 12.
    What is theAWS Lambda A service that executes code. Create a function called Lambda function and write code. You can write it directly in the AWS management console, but basically upload the code. You can write even code, AWS Lamvs provide manage servers, scaling, logging. Plone Conference WebDay 2018 Nov 7th 12
  • 13.
    What you cando with combining We can create RESTful API ❤ Plone Conference WebDay 2018 Nov 7th 13
  • 14.
    Flow of APIcreation 1. create the Amazon API Gateway 2. create AWS Lambda funtion 3. connect AWS GateWay & AWS Lambda funtion Plone Conference WebDay 2018 Nov 7th 14
  • 15.
    1). create theAmazon API Gateway Plone Conference WebDay 2018 Nov 7th 15
  • 16.
    2). create AWSLambda funtion Plone Conference WebDay 2018 Nov 7th 16
  • 17.
    3). connect theAmazon API Gateway & AWS Lambda funtion Plone Conference WebDay 2018 Nov 7th 17
  • 18.
    Next I want totalk about the AWS Lambda Plone Conference WebDay 2018 Nov 7th 18
  • 19.
    How to usethe Serverless framework Plone Conference WebDay 2018 Nov 7th 19
  • 20.
    How to usethe Serverless framework 1. How to deploy AWS Lambda function. 2. Introduce the Serverless Framework Plone Conference WebDay 2018 Nov 7th 20
  • 21.
    Target • Python newbie •use or used the AWS Lambda Plone Conference WebDay 2018 Nov 7th 21
  • 22.
    Goal • ! Easy to useAWS Lambda for Python • When you write Code, You can update easily Plone Conference WebDay 2018 Nov 7th 22
  • 23.
    can register atAWS console • Write Code • Run Test Plone Conference WebDay 2018 Nov 7th 23
  • 24.
    When used onlyonce This is enough Plone Conference WebDay 2018 Nov 7th 24
  • 25.
    • example: POSTJSON -> send message to Chat Plone Conference WebDay 2018 Nov 7th 25
  • 26.
    • example: POSTJSON -> send message to Chat • ! We want to use a 3rd Party lib • requests • add requirements.txt requests • run $ pip install -r requirements.txt -t vendor/ $ edit python-file $ zip /path/to/service-dir $ aws lambda ~~ Plone Conference WebDay 2018 Nov 7th 26
  • 27.
      It's troublesometo do with the AWS console ! use 3rd party libraries ! Especially when using C-API ! Code version control (git etc) Plone Conference WebDay 2018 Nov 7th 27
  • 28.
    Deploy to theAWSLambda • Deployment using ZipFile is possible ! Code version control (git etc) Plone Conference WebDay 2018 Nov 7th 28
  • 29.
    • Deployment usingZipFile is possible $ aws lambda create-function --function-name AccessMemCache --region us-east-1 --zip-file fileb://path-to/app.zip --role execution-role-arn --handler app.handler --runtime python3.6 --timeout 30 --vpc-config SubnetIds=comma-separated-vpc-subnet-ids,SecurityGroupIds=default-security-group-id --memory-size 1024 https://docs.aws.amazon.com/ja_jp/lambda/latest/ dg/vpc-ec-upload-deployment-pkg.html Plone Conference WebDay 2018 Nov 7th 29
  • 30.
    I want tomake it easier ! Create a zipfile each time you change -> deploy ! Run test locally • What we need now $ edit python-file $ (pip install ~~) $ zip /path/to/service-dir $ aws lambda ~~ Plone Conference WebDay 2018 Nov 7th 30
  • 31.
    Introduce the ServerlessFramework Plone Conference WebDay 2018 Nov 7th 31
  • 32.
    What is theServerless Framework? • http://www.serverless.com Serverless Framework – Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more! • AWS Lambda, Azure Functions, Google CloudFunctions を使ってサーバレスアーキテク チャで web, Mobile IoT アプリケーションを作ろう Plone Conference WebDay 2018 Nov 7th 32
  • 33.
    Description of theServerless Framework • pros !! Code version control(git / svn / Hg) ! zip -> deploy is possible by executing Only 1 command ! manage CloudWatch/ AWS IAM Plone Conference WebDay 2018 Nov 7th 33
  • 34.
    Use the ServerlessFramework How to create environments $ npm instal -g serverless $ sls -v 1.32.0 Plone Conference WebDay 2018 Nov 7th 34
  • 35.
    create AWS Lambdawith the Serverless Framework $ sls create -t aws-python3 -p py3-hello Serverless: Generating boilerplate... Serverless: Generating boilerplate in "/Users/masahito-nulab/src/jobs/typetalk-serverless-internal/py3-hello" _______ __ | _ .-----.----.--.--.-----.----| .-----.-----.-----. | |___| -__| _| | | -__| _| | -__|__ --|__ --| |____ |_____|__| ___/|_____|__| |__|_____|_____|_____| | | | The Serverless Application Framework | | serverless.com, v1.27.3 -------' Serverless: Successfully generated boilerplate for template: "aws-python3" $ tree py3-hello/ py3-hello/ !"" handler.py #"" serverless.yml Plone Conference WebDay 2018 Nov 7th 35
  • 36.
    • handler.py import json defhello(event, context): body = { "message": "Go Serverless v1.0! Your function executed successfully!", "input": event } response = { "statusCode": 200, "body": json.dumps(body) } return response • serverless.yml service: py3-hello provider: name: aws runtime: python3.6 functions: hello: handler: handler.hello Plone Conference WebDay 2018 Nov 7th 36
  • 37.
    Run Test locally $cd py3-hello $ sls invoke local -f hello { "statusCode": 200, "body": "{"message": "Go Serverless v1.0! Your function executed successfully!", "input": {}}" } Plone Conference WebDay 2018 Nov 7th 37
  • 38.
    Deploy to AWSLambda ! Deployment will be simplified after setting the IAM/Role $ sls deploy Plone Conference WebDay 2018 Nov 7th 38
  • 39.
    How to makedeployment easier Plone Conference WebDay 2018 Nov 7th 39
  • 40.
    How to makedeployment easier 1. reduce troublesome for library installation 2. Non-Pure Python module Plone Conference WebDay 2018 Nov 7th 40
  • 41.
    Goal • Easy touse AWS Lambda for Python • ! When you write Code, You can update easily Plone Conference WebDay 2018 Nov 7th 41
  • 42.
    Plone Conference WebDay2018 Nov 7th 42
  • 43.
    reduce troublesome forlibrary installation ! required 3rd-party library installation # ! I often forget this $ pip install -r requirement.txt -t vendor/ $ sls deploy Plone Conference WebDay 2018 Nov 7th 43
  • 44.
    reduce troublesome forlibrary installation Use serverless-framework plugin • UnitedIncome/serverless-python-requirements • serverless >= v1.12 Plone Conference WebDay 2018 Nov 7th 44
  • 45.
    How to installthis plugin $ sls plugin install -n serverless-python-requirements service: py3-hello provider: name: aws runtime: python3.6 cfLogs: true plugins: - serverless-python-requirements functions: hello: handler: handler.hello Plone Conference WebDay 2018 Nov 7th 45
  • 46.
    Deploy ($ sls requirementsinstall) $ sls deploy Plone Conference WebDay 2018 Nov 7th 46
  • 47.
    It is nowpossible to execute with a only 1 command ! Plone Conference WebDay 2018 Nov 7th 47
  • 48.
    Non-Pure Python module •Developers often use Windows / OSX for development environment • Python has lots of libraries written with C-API • numpy , TensorFlow , Image Libraries: pillow etc • Creating a zipfile locally does not work at AWS Lambda ! • The environment of AWS Lambda is Linux Plone Conference WebDay 2018 Nov 7th 48
  • 49.
    dockerize pip Cross-compile indocker environment • It uses docker image ”lambci / docker-lambda" service: py3-hello provider: name: aws runtime: python3.6 cfLogs: true plugins: - serverless-python-requirements custom: pythonRequirements: dockerizePip: true functions: hello: handler: handler.hello Plone Conference WebDay 2018 Nov 7th 49
  • 50.
    Today I talkedabout following things • How to create an RESTful API with the Amazon API Gateway • How to use the Serverless Framework • How to make deployment easier Plone Conference WebDay 2018 Nov 7th 50
  • 51.
    I hope youcan write and deploy RESTfull API ❤ Plone Conference WebDay 2018 Nov 7th 51
  • 52.
  • 53.