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

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

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

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

2c0947c6a28e7f771ebd9859ecf54e5c?s=128

Shinichi Nakagawa

October 03, 2021
Tweet

Transcript

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

    - PyLadies Tokyo - 7प೥ه೦ΦϯϥΠϯύʔςΟ 2021/10/3 Shinichi Nakagawa@shinyorke
  2. Happy Birthday🎂 PyLadies Tokyo 7प೥͓ΊͰͱ͏͍͟͝·͢ʂ

  3. ࠓճ࡞ͬͨ΋ͷ - AIΦΦλχαϯຊྥଧ༧ଌAPI ౤͛Δ࿹ʢӈorࠨʣ, ٿ଎ʢmile/hʣ, ٿछʢ௚ٿ, Χʔϒ, εϥΠμʔ, etc…ʣΛύϥϝʔλͱͯ͠POST ΦΦλχ͞Μ͕ຊྥଧΛଧͯΔ͔Ͳ͏͔ࡶʹ઎͏Ψνϟ…΋ͱ͍ਓ޻஌ೳͱࢥ͍ͬͯͩ͘͞.

  4. Who am I ?ʢ͓લ୭Αʣ • Shinichi Nakagawaʢத઒ ৳Ұʣ • େ఍ͷSNSͰʮshinyorkeʢ͠ΜΑʔ͘ʣʯͱ໊৐͍ͬͯ·͢

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

    Github Actions 
 ͰςετˠσϓϩΠΛ͍͍ײ͡ʹ΍Δํ๏ΛαΫοͱ࿩͠·͢ • ͳ͓, ͜ͷ࿩͸PyCon JP 2021Ͱ࿩͢༧ఆͷτʔΫ…ͷҰ෦Ͱ͢
  6. ຊ೔ͷߏ੒ खݩͰͭ͘ΔˠGithub ActionsͰςετ+σϓϩΠˠ༏উ

  7. ཧ૝తͳ։ൃॱং 1.ͬ͞͞ͱಈ͘ΞϓϦΛͭ͘ΔʢݪܕΛͭ͘Δʣ 2.ͻͱ·ͣಈ͘΋ͷΛσϓϩΠ͢Δ 3.CIʢࠓճ͸Github ActionsʣΛಈ͘Α͏ʹ͢Δ 4.ςετˠσϓϩΠͷྲྀΕΛͭ͘Δ ৭ʑͱख๏ɾफ೿͸͋Γͦ͏Ͱ͕͢ແ೉ͳͷ͸͜Μͳײͩ͡ͱࢥ͏

  8. 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Λ࢖͏࣌͸ͦ͜ʹ߹Θͤͯ࡞Γ௚ͤ͹
  9. ʲࡶஊʳGAE ελϯμʔυVSϑϨΩγϒϧ • Python࢖͍తʹ͸, GAE࢖͏࣌͸ݪଇελϯμʔυҰ୒Ͱྑ͍ͱࢥͬͯ·͢ • ελϯμʔυ؀ڥ, Pythonͷόʔδϣϯ΋৽͍͠ʢݱࡏ3.9ʣ 
 ͷͱ,

    scikit-learnͱ͔pandas΋໰୊ͳ͘ಈ͘ͷͰେ఍ͷࣄ͸Ͱ͖Δ 
 →AIΦΦλχαϯຊྥଧ༧ଌ͸sklearn + pandasͰಈ͍ͯ·͢, GAEͰ΋໰୊ͳ͘ • σϓϩΠָ͕, ߴෛՙରࡦ΋ख͍ؒΒͣͱελϯμʔυͦͷ΋ͷͷڧΈଟ͍ • ଞͷϓϩάϥϛϯάݴޠ΍, ෳࡶͳ͜ͱΛ͢Δ࣌ʹϑϨΩγϒϧͰྑ͍ͱࢥ͏ 
 ʢ͕, ࠓ͸Cloud Run΋͋Δ͠…ʣ
  10. ߴෛՙରࡦʢ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ʹͨ͠Βύϥϝʔλ͕ແޮʹͳΓ·͢…͕͓͢͢Ί͸͠·ͤΜ 
 ࣄ্࣮࠷େ஋Λఆٛ͢Δ͜ͱʹͳΔͨΊʢ㲈ͳʹ͔ͷΞΫγσϯτͰΠϯελϯε͕૿͑Δʣ
  11. Github ActionsͰGAEʹσϓϩΠ • https://github.com/google-github-actions/deploy-appengine • ͜ΜͳศརͳϞϊ͕ग़ͨͷͰඇৗʹָʹͳͬͨ • GCPϓϩδΣΫτIDͱ伴Λࢦఆͨ͠Β͍͍ײ͡ʹ΍ͬͯ͘ΕΔ

  12. ʲྫʳ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/deploy-appengine@v0.3. 1 with : project_id: ${{ secrets.GCP_PROJECT_ID } } deliverables: app.yam l credentials: ${{ secrets.GCP_SA_KEY } } version: v1
  13. ςετ͸Ͳ͏͢Δ🤔 # 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ʹՃ͑Δʢςετͷલʹ࣮ߦͯ͠Τϥʔग़ͨΒσϓϩΠ͠ͳ͍ʣ
  14. ʲྫʳ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/actions-poetry@v1.5. 0 with : python_version: 3.9. 7 poetry_version: 1.1. 7 args: instal l - name: Run myp y uses: abatilo/actions-poetry@v1.5. 0 with : python_version: 3.9. 7 poetry_version: 1.1. 7 args: run python -m mypy . - name: Run pytes t uses: abatilo/actions-poetry@v1.5. 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/deploy-appengine@v0.3. 1 with : project_id: ${{ secrets.GCP_PROJECT_ID } } deliverables: app.yam l credentials: ${{ secrets.GCP_SA_KEY } } version: v1
  15. ݁ͼ • ࠷ॳʹখ͍͞ΞϓϦέʔγϣϯΛ࡞ͬͯσϓϩΠ͢Δ·ͰΛ 
 ΍Δͷ͕࠷΋खͬऔΓૣ͘ੈͷதʹग़ͤΔ 
 &ਫ਼ਆతෛՙ΋গͳ͍ͱࢥ͏ͷͰڧ͓͘͢͢Ί͍ͨ͠ • Github Actions΋͍ͩͿ৭ʑἧ͖ͬͯͨͷͰྑͦ͞͏

    
 ʢࠓճॳΊͯͪΌΜͱ࢖͕ͬͨࢥͬͨΑΓָͩͬͨʣ • Ҏલͷ࢓ࣄʢAIϫΫνϯ઀छ༧ଌʣ΋ࠓճͷαϯϓϧ΋ 
 ͜ͷख๏Ͱര଎։ൃͰ͖·ͨ͠👍
  16. ʲ༧ࠂʳ2021/10/16 11:40ϓϨΠϘʔϧ⽁ https://2021.pycon.jp/ PyCon JPຊ൪Ͱ͸ʮStreamlitͰϓϩτλΠϓ࡞ͬͨޙ, ຊαʔϏεͱ͍͍ͯ͠ײ͡ʹ΍Δʯ࿩Λ͠·͢

  17. ήʔϜηοτ⚾ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠. Shinichi Nakagawa(Twitter/Facebook/etc… @shinyorke)