Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Frameworkで気軽にAPIを公開してみる?(Python3.6・ヴ...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Serverless Frameworkで気軽にAPIを公開してみる?(Python3.6・ヴィジュアル系編)

ヴィジュアル系で学ぶ、Serverless Framework(Python3.6編)

Avatar for Yutaka Fujisaki

Yutaka Fujisaki

July 13, 2017
Tweet

More Decks by Yutaka Fujisaki

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ౻࡚ɹ༏ • גࣜձࣾΦϧλʔϒʔε ۀ຿ࣥߦ໾һ COO • JAWS-UG෱ԬίΞϝϯόʔ •

    ͘͞ΒΫϥϒIoT෱ԬίΞϝϯόʔ • ϰΟδϡΞϧܥେ޷͖28೥ • AWS Summitߦͬͯͳ͍੎ɾɾɾ
  2. JSON [ [ "date", "2017-8-3" ], [ "artist", "C4" ],

    [ "title", " ULTIMATE BEST ALBUM [-S A G A-] " ], [ "media", "CD" ], [ "price", "¥3,800" ] ],
  3. 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/
  4. ࢖͍ํ(ςϯϓϨʔτσϑΥϧτ࣮ߦ࣌) # ʲςϯϓϨʔτ࡞੒ʳ sls create -t aws-python3 -p vkeicalendar cd

    vkeicalendar/ # ʲσϓϩΠʳ sls deploy # ʲςετ:AWS্Ϧιʔε࣮ߦʳ sls invoke -f hello # ʲ࡟আʳ sls remove
  5. 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 # ઃఆ͠ͳ͍ͱଞ͔Βར༻Ͱ͖ͳ͍
  6. εΫϦϓτΛҠ২͢Δ 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(): ɾɾɾ ɾɾɾ
  7. σϓϩΠ͢Δ 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