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
860
messaging apiとLine Payで広がるLINE Botの世界
yfujisaki
1
650
孤独に耐えるための自宅スマートホーム化のすゝめ(JAWS-UG 鹿児島 Vol.8 LT)
yfujisaki
0
590
AWS(クラウド)活用思考と機能概要
yfujisaki
0
210
起業から3年間を支えてくれたintoneとChatOps〜面倒くさいを減らして運用を楽にしよう〜
yfujisaki
0
770
マイソースファクトリーでのStripeの導入を検討してみた
yfujisaki
1
430
コミュニティで会社ができた話とクラウドでのビジネスの話
yfujisaki
0
170
Other Decks in Technology
See All in Technology
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
220
5分でわかるDuckDB
chanyou0311
9
3.1k
Amazon Bedrock Knowledge BasesがGraphRAGに対応!! ・・・それってつまりどういうコト!? をチョット深堀ってみる
tokushun
0
200
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
240
Oracle Database Release and Support Timelines 2024/12/11
wmo6hash
0
300
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
170
私なりのAIのご紹介 [2024年版]
qt_luigi
1
100
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
280
実務につなげる数理最適化
recruitengineers
PRO
6
600
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
2
150
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
310
『GRANBLUE FANTASY: Relink』続・最高の「没入感」を実現するカットシーン制作手法とそれを支える技術
cygames
0
130
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
270
A better future with KSS
kneath
238
17k
Facilitating Awesome Meetings
lara
50
6.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
800
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Fireside Chat
paigeccino
34
3.1k
The Language of Interfaces
destraynor
154
24k
It's Worth the Effort
3n
183
28k
Done Done
chrislema
181
16k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Embracing the Ebb and Flow
colly
84
4.5k
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Λ͓͏ʂ