Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Serverless Frameworkで気軽にAPIを公開してみる?(Python3.6・ヴ...
Search
Yutaka Fujisaki
July 13, 2017
Technology
0
370
Serverless Frameworkで気軽にAPIを公開してみる?(Python3.6・ヴィジュアル系編)
ヴィジュアル系で学ぶ、Serverless Framework(Python3.6編)
Yutaka Fujisaki
July 13, 2017
Tweet
Share
More Decks by Yutaka Fujisaki
See All by Yutaka Fujisaki
2019.03.06 JBUG福岡@ヌーラボ Backlogでわかる炎上の見分け方、消し方 リビルド
yfujisaki
2
870
messaging apiとLine Payで広がるLINE Botの世界
yfujisaki
1
660
孤独に耐えるための自宅スマートホーム化のすゝめ(JAWS-UG 鹿児島 Vol.8 LT)
yfujisaki
0
600
AWS(クラウド)活用思考と機能概要
yfujisaki
0
220
起業から3年間を支えてくれたintoneとChatOps〜面倒くさいを減らして運用を楽にしよう〜
yfujisaki
0
780
マイソースファクトリーでのStripeの導入を検討してみた
yfujisaki
1
430
コミュニティで会社ができた話とクラウドでのビジネスの話
yfujisaki
0
180
Other Decks in Technology
See All in Technology
AIエージェントに脈アリかどうかを分析させてみた
sonoda_mj
2
140
2025年のARグラスの潮流
kotauchisunsun
0
740
OPENLOGI Company Profile
hr01
0
58k
SpiderPlus & Co. エンジニア向け会社紹介資料
spiderplus_cb
0
680
Oracle Exadata Database Service(Dedicated Infrastructure):サービス概要のご紹介
oracle4engineer
PRO
0
12k
Unsafe.BitCast のすゝめ。
nenonaninu
0
170
Fearsome File Formats
ange
0
580
OCI技術資料 : ファイル・ストレージ 概要
ocise
3
12k
ゼロからわかる!!AWSの構成図を書いてみようワークショップ 問題&解答解説 #デッカイギ #羽田デッカイギおつ
_mossann_t
0
1.3k
「完全に理解したTalk」完全に理解した
segavvy
1
300
comilioとCloudflare、そして未来へと向けて
oliver_diary
5
400
深層学習と3Dキャプチャ・3Dモデル生成(土木学会応用力学委員会 応用数理・AIセミナー)
pfn
PRO
0
430
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Practical Orchestrator
shlominoach
186
10k
Building Your Own Lightsaber
phodgson
104
6.2k
The Cult of Friendly URLs
andyhume
78
6.1k
Music & Morning Musume
bryan
46
6.3k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Language of Interfaces
destraynor
155
24k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Making the Leap to Tech Lead
cromwellryan
133
9k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Transcript
Serverless FrameworkͰ ؾܰʹAPIΛެ։ͯ͠ΈΔʁ (Python3.6ɾϰΟδϡΞϧܥฤ) 201707݄13 JAWS-UGԬ@ूஂࢁݟͤͱAWS Summit TokyoৼΓฦΒNight#5
ࣗݾհ • ౻࡚ɹ༏ • גࣜձࣾΦϧλʔϒʔε ۀࣥߦһ COO • JAWS-UGԬίΞϝϯόʔ •
͘͞ΒΫϥϒIoTԬίΞϝϯόʔ • ϰΟδϡΞϧܥେ͖28 • AWS Summitߦͬͯͳ͍ɾɾɾ
࠷ۙPythonͰϚωʔδϟʔઢͷπʔϧ࡞Γ࢝Ί·ͨ͠ https://github.com/y-fujisaki/BacklogCountToolServerless
ͯ͞ຊͷ͓
ͨͱ͑͜Μͳͪΐͬͱͨ͠ൃ͕͋ͬͨΒʁ
ొਓ() • όϯΪϟϧஉ͞Μ • ϰΟδϡΞϧܥόϯυΛ͔͚ͬͯ28ɻɻCDϑϥή खʹೖΕͳ͍ͱؾ͕͢·ͳ͍ɺےۚೖΓɻதೋප͕࣏ ͠ͳ͍ • ͱ͋ΔCOOͳΤϯδχΞ͞Μ •
AWSͱAzureνϣτͰ͖Δɻ࠷ۙPythonͰπʔϧॻ͘ͷ͕ ͓ؾʹ͍Γ
͋Δͷձ • όϯΪϟϧஉʣϰΟδϡΞϧܥόϯυͷൃചΧϨϯμʔɺ ࣗ༝ʹѻ͑ΔΑ͏ͳσʔλཉ͍͠ͳ͊ • ΤϯδχΞʣϒϥϯυΤοΫεͷαΠτʹൃചΧϨϯμʔ ͕͔͋ͬͨΒεΫϨΠϐϯάͨ͠Β͍͚Δ͔ • όϯΪϟϧஉʣͦΕͩʂʂ
ɿϒϥϯυΤοΫε ାʹ͋ΔϰΟδϡΞϧܥCDઐళɻϝδϟʔόϯυͰຬͰ ͖ͳ͘ͳͬͨਓ͕࣍ʹߦͬͯΈΔ͓ళ http://www.brand-x.jp/
εΫϨΠϐϯάͯ͠Έͨ ݩϖʔδ http://www.brand-x.jp/page/38
εΫϨΠϐϯάͯ͠Έͨ https://github.com/y-fujisaki/vkei • Python • Beautiful Soup(http://kondou.com/BS4/)ͰHTML͔Βରͷ ΧϨϯμʔ෦Λऔಘ • σʔλΛগ͠ܗ
• JSONͰฦ͢Α͏ʹͯ͠Έͨ
JSON [ [ "date", "2017-8-3" ], [ "artist", "C4" ],
[ "title", " ULTIMATE BEST ALBUM [-S A G A-] " ], [ "media", "CD" ], [ "price", "¥3,800" ] ],
͋Δͷձͦͷ̎ • όϯΪϟϧஉʣ͍͍ͪͪεΫϦϓτୟ͘ͷΊΜͲ͍ɺͳΜͱ ͔ͯ͠ • ΤϯδχΞʣ͠Ό͋ͳ͍ɾɾɾLambdaͰ͓͏͔ɾɾɾ ͤʂྲྀߦΓͷServerless FrameworkͰͬͨΖʂ
Serverless Framework • ֤छΫϥυͷSeverlessΞʔΩςΫνϟͰbuild & deploy auto-scaling, event-driven functions.ͳͲΛૢ࡞͢ΔCLIπʔ ϧ
• https://github.com/serverless/serverless • ڥߏஙࢀߟࢿྉ:҆৺҆ఆͷΫϥεϝιου༷ϒϩά ࠓ͔Β࢝ΊΔServerless FrameworkͰ؆୯Lambda։ൃڥͷߏங http://dev.classmethod.jp/cloud/aws/easy-deploy-of-lambda-with-serverless- framework/
͍ํ(ςϯϓϨʔτσϑΥϧτ࣮ߦ࣌) # ʲςϯϓϨʔτ࡞ʳ sls create -t aws-python3 -p vkeicalendar cd
vkeicalendar/ # ʲσϓϩΠʳ sls deploy # ʲςετ:AWS্Ϧιʔε࣮ߦʳ sls invoke -f hello # ʲআʳ sls remove
ςϯϓϨʔτ͕༻ҙ͞Ε͍ͯΔϥϯλΠϜ • aws-python • aws-nodejs • aws-python • aws-java-maven •
aws-java-gradle • aws-scala-sbt • aws-csharp
Serverless FrameworkͰ ࡞ͬͨPythonεΫϦϓτΛ ಈ͔ͯ͠ΈΔ
࡞ۀͷྲྀΕ
ϓϩδΣΫτ࡞ sls create -t aws-python3 -p vkeicalendar cd vkeicalendar/
serverless-python-requirementsΠϯετʔϧ • PGͰඞཁͳPythonύοέʔδϦετ(requirements.txt)Λσ ϓϩΠ࣌ͷύοέʔδʹؚΊΔϓϥάΠϯ npm install --save serverless-python-requirements
PluginͰಡΈࠐΉύοέʔδϦετΛ࡞ (requirements.txt) beautifulsoup4 datetime python-dateutil retry urlopen
serverless.ymlΛ࿔Δ service: slsVkei # ςϯϓϨʔτΛ࡞ͨ͠ͱ͖ʹࢦఆͨ͠ͷ provider: name: aws runtime: python3.6
# aws-python3ͱࢦఆ͢Δͱ3.6ʹɺͳ͍ͱ2.7ʹͳΔ region: ap-northeast-1 # ౦ژϦʔδϣϯʹมߋ plugins: - serverless-python-requirements #ϓϥάΠϯࢦఆ functions: calendar: # Serverless FrameworkͰѻ͏໊ؔʢςετ࣌ʹ༻ʣ handler: vkei.calendar # ϑΝΠϧ໊(vkei.py)ɿLambdaϑΝϯΫγϣϯ໊(def calendar) events: - http: path: vcalendar # api gatewayϦιʔε໊ method: get # ࠓճgetͷΈ cors: true # ઃఆ͠ͳ͍ͱଞ͔Βར༻Ͱ͖ͳ͍
εΫϦϓτΛҠ২͢Δ import json from dateutil.relativedelta import relativedelta from retry import
retry from urllib.request import urlopen from bs4 import BeautifulSoup from datetime import datetime, date, timedelta def calendar(event, context): body = getVcalendar() response = { "statusCode": 200, # SPAͰѻ͏ͨΊɺϨεϙϯεϔομʔʹԼهՃ͑Δ "headers" : {"Access-Control-Allow-Origin" : "*"}, "body": body } return response ## Ճʂ ## ϰΟδϡΞϧܥΧϨϯμʔΛಡΈࠐΜͰJSONԽ def getVcalendar(): ɾɾɾ ɾɾɾ
ग़དྷͨͷ͕ͪ͜Βʢ̏ΫοΩϯάܗࣜʣ • https://github.com/y-fujisaki/vkeiJAWSFUKUOKA20170713
σϓϩΠ͢Δ y-fujisaki-MBP:slsVkei y-fujisaki$ sls deploy Serverless: Installing required Python packages
for runtime python3.6... Serverless: Linking required Python packages... Serverless: Packaging service... Serverless: Unlinking required Python packages... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service .zip file to S3 (4 MB)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... .................................. Serverless: Stack update finished... Service Information service: slsVkei stage: dev region: ap-northeast-1 api keys: None endpoints: GET - https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/vcalendar functions: hello: slsVkei-dev-hello
Ͱ͖ͨʂʂʂʢͣʣ
͋Δͷձͦͷ3 • ΤϯδχΞʣ͜ΕͰऔΕΔΑ͏ʹͳͬͨͰʂʂ • όϯΪϟϧஉʣόϯυ໊ൃചͰߜΓ͜ΊΔΑ͏ʹͯ͠ʂ • ΤϯδχΞʣɾɾɾ
ଓ͘
serverless frameworkͷϝϦοτ • ίϚϯυ̍ൃͰσϓϩΠɾআʂ • ϩʔΧϧσόοάͰ͖Δʢͣɾɾɾࣗͷڥ͍·ίέΔ ͆ʣ • serverlessෳͷαʔϏεͷ߹ͤͱͳΓɺίʔυ Build+Deployͷཧ͕όϥόϥʹͳΓ͕ͪͳͷΛճආͰ͖Δ
• ServerlessΞʔΩςΫνϟͷΊΜͲ͍͘͞ॾʑͷσϝϦοτ ໘ΛݶΓͳܰ͘ݮͰ͖Δ
Serverless FrameworkͰؾܰʹ LambdaΛ͓͏ʂ