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

Flask + Google App Engine(GAE)でWeb APIをデプロイするまで...

Flask + Google App Engine(GAE)でWeb APIをデプロイするまで
- Github Actionsを添えて / Flask + App Engine + Github Actions

PyLadies Tokyo 7周年パーティーのLT

Shinichi Nakagawa

October 03, 2021
Tweet

More Decks by Shinichi Nakagawa

Other Decks in Programming

Transcript

  1. Flask + Google App EngineʢGAEʣͰWeb APIΛσϓϩΠ͢Δ·Ͱ 
 - Github ActionsΛఴ͑ͯ

    - PyLadies Tokyo - 7प೥ه೦ΦϯϥΠϯύʔςΟ 2021/10/3 Shinichi Nakagawa@shinyorke
  2. Who am I ?ʢ͓લ୭Αʣ • Shinichi Nakagawaʢத઒ ৳Ұʣ • େ఍ͷSNSͰʮshinyorkeʢ͠ΜΑʔ͘ʣʯͱ໊৐͍ͬͯ·͢

    • Software Engineer & Data ScientistʢؚΉ⽁ʣ • ๭֎ࢿܥاۀͷΤϯδχΞɾϚωʔδϟʔʢࠓ݄͔Βʣ • #Python #DataScience #Baseball⚾ #SABRmetrics
  3. ࠓ೔ͷ͓౔࢈ • WebαΠτ΋͘͠͸APIΛFlaskͰͭ͘Δͱܾ·͍ͬͯΔલఏͰ • ʮͻͱ·ͣશੈքʹެ։͠ͳ͍ͱ͔͋Μͷ΍ʔʯͬͯ࣌ʹ • Google App EngineʢGAEʣ +

    Github Actions 
 ͰςετˠσϓϩΠΛ͍͍ײ͡ʹ΍Δํ๏ΛαΫοͱ࿩͠·͢ • ͳ͓, ͜ͷ࿩͸PyCon JP 2021Ͱ࿩͢༧ఆͷτʔΫ…ͷҰ෦Ͱ͢
  4. FlaskͰͻͱ·ͣ࡞ͬͯGAEͰಈ͔͢·Ͱ • ͸͡ΊͯGAEΛ৮Δʢorٱ͠ͿΓʹ৮Δʣํ͸Google CloudͷΫΠοΫελʔτΛࢀߟʹ΍Δͱ ྑ͍Ͱ͠ΐ͏, ࣸܦͯ͠ਅࣅΔͷ͕ͪΐ͏ͲΑ͍͔΋. • https://cloud.google.com/appengine/docs/standard/python3/quickstart?hl=ja • ্هΛ௨͠Ͱ΍ΔͱFlask੡ͷhello

    world͕ര஀, ࢓૊Έ΋ཧղͰ͖Δ͸ͣ. • ͋ͱ͸࡞Γ͍ͨϞϊͷ࢓༷ʹ߹ΘͤͯFlaskͷίʔυΛ͍͡Ε͹OK • ྫ͑͹RESTfulʹ͢ΔͳΒResponseΛJSONʹม͑Δͱ͔ • FastAPIͳͲ, ଞͷFWΛ࢖͏࣌͸ͦ͜ʹ߹Θͤͯ࡞Γ௚ͤ͹
  5. ʲࡶஊʳGAE ελϯμʔυVSϑϨΩγϒϧ • Python࢖͍తʹ͸, GAE࢖͏࣌͸ݪଇελϯμʔυҰ୒Ͱྑ͍ͱࢥͬͯ·͢ • ελϯμʔυ؀ڥ, Pythonͷόʔδϣϯ΋৽͍͠ʢݱࡏ3.9ʣ 
 ͷͱ,

    scikit-learnͱ͔pandas΋໰୊ͳ͘ಈ͘ͷͰେ఍ͷࣄ͸Ͱ͖Δ 
 →AIΦΦλχαϯຊྥଧ༧ଌ͸sklearn + pandasͰಈ͍ͯ·͢, GAEͰ΋໰୊ͳ͘ • σϓϩΠָ͕, ߴෛՙରࡦ΋ख͍ؒΒͣͱελϯμʔυͦͷ΋ͷͷڧΈଟ͍ • ଞͷϓϩάϥϛϯάݴޠ΍, ෳࡶͳ͜ͱΛ͢Δ࣌ʹϑϨΩγϒϧͰྑ͍ͱࢥ͏ 
 ʢ͕, ࠓ͸Cloud Run΋͋Δ͠…ʣ
  6. ߴෛՙରࡦʢapp.yamlهड़ྫʣ runtime: python3 9 automatic_scaling : max_instances: 33 # ࠷େͷΠϯελϯε਺ʢ͜ΕҎ্૿͑ͳ͍ʣ

    min_instances: 4 # ࠷খͷΠϯελϯε਺ʢ͜ΕҎ্ݮΒͳ͍ʣ • app.yamlͷΦϓγϣϯΛ͍ͬͯ͡deployͨ͠Β͍͍ײ͡ʹ͍͚·͢ʢ্هͷྫ͸ελϯμʔυ؀ڥʣ • TVʹग़Δ, SNSͰόζΔ౳͕༧૝͞ΕΔ࣌͸ࣄલʹύϥϝʔλΛ͍ͬͯ͡σϓϩΠͯ͠ղܾ 
 ͪͳΈʹ૿͑ͨΠϯελϯε͸app.yamlΛ௚ͯ͠࠶σϓϩΠ or GCPͷίϯιʔϧͰखಈͰফ͢౳ͰରԠͰ͖·͢ • min_instancesΛ0ʹͨ͠ΒʮϦΫΤετ͕͖ͨΒ্ཱͪ͛ΔʯΈ͍ͨͳײ͡ʹ 
 ʢϨεϙϯε͕དྷΔ·Ͱ࣌ؒ͸͔͔Δ͕ྉۚ͸અ໿Ͱ͖Δʣ • max_instancesΛ0ʹͨ͠Βύϥϝʔλ͕ແޮʹͳΓ·͢…͕͓͢͢Ί͸͠·ͤΜ 
 ࣄ্࣮࠷େ஋Λఆٛ͢Δ͜ͱʹͳΔͨΊʢ㲈ͳʹ͔ͷΞΫγσϯτͰΠϯελϯε͕૿͑Δʣ
  7. ʲྫʳActionsͷهड़ name: deplo y on : push : branches :

    - mai n jobs : deploy-gae : name: App Engine Deplo y runs-on: ubuntu-lates t steps : - uses: actions/checkout@v 2 - name: Deploy an App Engine ap p id: deploy-ap p uses: google-github-actions/[email protected]. 1 with : project_id: ${{ secrets.GCP_PROJECT_ID } } deliverables: app.yam l credentials: ${{ secrets.GCP_SA_KEY } } version: v1
  8. ςετ͸Ͳ͏͢Δ🤔 # AIΦΦλχαϯຊྥଧ༧ଌͷύοέʔδߏ੒ʢΠϝʔδʣ app / !"" entities / !"" interfaces/

    # FlaskҎ֎ͷαʔυύʔςΟʔɾϥΠϒϥϦΛimportͯ͠OKͳॴ !"" tests / # !"" dataset/ # tests಺Ͱ࢖͏σʔληοτʢ͍ΘΏΔfixtureʣ # !"" entities/ # entitiesͷςετ # !"" interfaces/ # interfacesͷςετ # $"" usecase/ # usecaseͷςετ !"" usecase / main.py # Flaskʹґଘͯ͠OKͳͱ͜Ζ • ͋Δఔ౓࡞ͬͨΒ༻ҙ͠·͠ΐ͏, σΟϨΫτϦɾύοέʔδ୯ҐͰ෼͚Δͱϕετ • ࠷ॳ͸ಈ͘΋ͷ͋Γ͖, ঃʑʹTDDʢςετۦಈ։ൃʣͬΆ͘Ͱ͖Δͱ඼࣭΋εϐʔυ΋͍͍ײ͡ʹ • ͋Δఔ౓Ͱ͖ͨΒ͜Ε΋Github ActionsʹՃ͑Δʢςετͷલʹ࣮ߦͯ͠Τϥʔग़ͨΒσϓϩΠ͠ͳ͍ʣ
  9. ʲྫʳActionsͷهड़ʢςετ௥Ճ൛ʣ name: C I on: pus h jobs : test

    : name: tes t runs-on: ubuntu-lates t steps : - uses: actions/checkout@maste r - name: Instal l uses: abatilo/[email protected]. 0 with : python_version: 3.9. 7 poetry_version: 1.1. 7 args: instal l - name: Run myp y uses: abatilo/[email protected]. 0 with : python_version: 3.9. 7 poetry_version: 1.1. 7 args: run python -m mypy . - name: Run pytes t uses: abatilo/[email protected]. 0 with : python_version: 3.9. 7 poetry_version: 1.1. 7 args: run python -m pytest . deploy-gae : name: App Engine Deplo y needs: tes t if: github.ref == 'refs/heads/main ' runs-on: ubuntu-lates t steps : - uses: actions/checkout@v 2 - name: Deploy an App Engine ap p id: deploy-ap p uses: google-github-actions/[email protected]. 1 with : project_id: ${{ secrets.GCP_PROJECT_ID } } deliverables: app.yam l credentials: ${{ secrets.GCP_SA_KEY } } version: v1
  10. ݁ͼ • ࠷ॳʹখ͍͞ΞϓϦέʔγϣϯΛ࡞ͬͯσϓϩΠ͢Δ·ͰΛ 
 ΍Δͷ͕࠷΋खͬऔΓૣ͘ੈͷதʹग़ͤΔ 
 &ਫ਼ਆతෛՙ΋গͳ͍ͱࢥ͏ͷͰڧ͓͘͢͢Ί͍ͨ͠ • Github Actions΋͍ͩͿ৭ʑἧ͖ͬͯͨͷͰྑͦ͞͏

    
 ʢࠓճॳΊͯͪΌΜͱ࢖͕ͬͨࢥͬͨΑΓָͩͬͨʣ • Ҏલͷ࢓ࣄʢAIϫΫνϯ઀छ༧ଌʣ΋ࠓճͷαϯϓϧ΋ 
 ͜ͷख๏Ͱര଎։ൃͰ͖·ͨ͠👍