Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

0

Share

Download to read offline

Build a RESTful API with the Serverless Framework

Download to read offline

Plone conf2018 webday

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Build a RESTful API with the Serverless Framework

  1. 1. Build a RESTfull API with the Serverless framework Plone Conference WebDay 2018 Nov 7th 1
  2. 2. Introduction 1. Introduce myself 2. perpose of this presentation Plone Conference WebDay 2018 Nov 7th 2
  3. 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. 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. 5. Not covering today • Technical Details • AWS knowledge(IAM etc) Plone Conference WebDay 2018 Nov 7th 5
  6. 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. 7. How to create RESTful API Plone Conference WebDay 2018 Nov 7th 7
  8. 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. 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. 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. 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. 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. 13. What you can do with combining We can create RESTful API ❤ Plone Conference WebDay 2018 Nov 7th 13
  14. 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. 15. 1). create the Amazon API Gateway Plone Conference WebDay 2018 Nov 7th 15
  16. 16. 2). create AWS Lambda funtion Plone Conference WebDay 2018 Nov 7th 16
  17. 17. 3). connect the Amazon API Gateway & AWS Lambda funtion Plone Conference WebDay 2018 Nov 7th 17
  18. 18. Next I want to talk about the AWS Lambda Plone Conference WebDay 2018 Nov 7th 18
  19. 19. How to use the Serverless framework Plone Conference WebDay 2018 Nov 7th 19
  20. 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
  21. 21. Target • Python newbie • use or used the AWS Lambda Plone Conference WebDay 2018 Nov 7th 21
  22. 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. 23. can register at AWS console • Write Code • Run Test Plone Conference WebDay 2018 Nov 7th 23
  24. 24. When used only once This is enough Plone Conference WebDay 2018 Nov 7th 24
  25. 25. • example: POST JSON -> send message to Chat Plone Conference WebDay 2018 Nov 7th 25
  26. 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. 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. 28. Deploy to the AWSLambda • Deployment using ZipFile is possible ! Code version control (git etc) Plone Conference WebDay 2018 Nov 7th 28
  29. 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. 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
  31. 31. Introduce the Serverless Framework Plone Conference WebDay 2018 Nov 7th 31
  32. 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. 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. 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
  35. 35. 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
  36. 36. • 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
  37. 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. 38. Deploy to AWS Lambda ! Deployment will be simplified after setting the IAM/Role $ sls deploy Plone Conference WebDay 2018 Nov 7th 38
  39. 39. How to make deployment easier Plone Conference WebDay 2018 Nov 7th 39
  40. 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. 41. Goal • Easy to use AWS Lambda for Python • ! When you write Code, You can update easily Plone Conference WebDay 2018 Nov 7th 41
  42. 42. Plone Conference WebDay 2018 Nov 7th 42
  43. 43. 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
  44. 44. reduce troublesome for library installation Use serverless-framework plugin • UnitedIncome/serverless-python-requirements • serverless >= v1.12 Plone Conference WebDay 2018 Nov 7th 44
  45. 45. 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
  46. 46. Deploy ($ sls requirements install) $ sls deploy Plone Conference WebDay 2018 Nov 7th 46
  47. 47. It is now possible to execute with a only 1 command ! Plone Conference WebDay 2018 Nov 7th 47
  48. 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. 49. 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
  50. 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. 51. I hope you can write and deploy RESTfull API ❤ Plone Conference WebDay 2018 Nov 7th 51
  52. 52. Thank you @masahito Plone Conference WebDay 2018 Nov 7th 52
  53. 53. Question? Plone Conference WebDay 2018 Nov 7th 53

Plone conf2018 webday

Views

Total views

873

On Slideshare

0

From embeds

0

Number of embeds

182

Actions

Downloads

4

Shares

0

Comments

0

Likes

0

×