3. 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
4. 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
5. Not covering today
• Technical Details
• AWS knowledge(IAM etc)
Plone Conference WebDay 2018 Nov 7th 5
6. 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
7. How to create RESTful API
Plone Conference WebDay 2018 Nov 7th 7
8. 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
9. Where to use RESTful API?
• Upload File to the Amazon S3
• Send Message to a service
Plone Conference WebDay 2018 Nov 7th 9
10. 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
11. 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
12. 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
13. What you can do with combining
We can create RESTful API ❤
Plone Conference WebDay 2018 Nov 7th 13
14. 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
15. 1). create the Amazon API Gateway
Plone Conference WebDay 2018 Nov 7th 15
17. 3). connect the Amazon API Gateway & AWS Lambda
funtion
Plone Conference WebDay 2018 Nov 7th 17
18. Next
I want to talk about the AWS Lambda
Plone Conference WebDay 2018 Nov 7th 18
19. How to use the Serverless
framework
Plone Conference WebDay 2018 Nov 7th 19
20. 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
22. Goal
•
!
Easy to use AWS Lambda for Python
• When you write Code, You can update easily
Plone Conference WebDay 2018 Nov 7th 22
23. can register at AWS console
• Write Code
• Run Test
Plone Conference WebDay 2018 Nov 7th 23
24. When used only once This is enough
Plone Conference WebDay 2018 Nov 7th 24
25. • example: POST JSON -> send message to Chat
Plone Conference WebDay 2018 Nov 7th 25
26. • 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
27. 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
28. Deploy to the AWSLambda
• Deployment using ZipFile is possible
!
Code version control (git etc)
Plone Conference WebDay 2018 Nov 7th 28
29. • 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
30. 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
32. 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
33. 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
34. Use the Serverless Framework
How to create environments
$ npm instal -g serverless
$ sls -v
1.32.0
Plone Conference WebDay 2018 Nov 7th 34
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 AWS Lambda
!
Deployment will be simplified after setting the
IAM/Role
$ sls deploy
Plone Conference WebDay 2018 Nov 7th 38
39. How to make deployment
easier
Plone Conference WebDay 2018 Nov 7th 39
40. How to make deployment easier
1. reduce troublesome for library installation
2. Non-Pure Python module
Plone Conference WebDay 2018 Nov 7th 40
41. Goal
• Easy to use AWS Lambda for Python
•
!
When you write Code, You can update easily
Plone Conference WebDay 2018 Nov 7th 41
47. It is now possible 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
50. 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
51. I hope you can write and
deploy RESTfull API ❤
Plone Conference WebDay 2018 Nov 7th 51