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

Python × AWS × Serverless 初学者が次の一歩を踏み出すためのテクニック...

Python × AWS × Serverless 初学者が次の一歩を踏み出すためのテクニック/Python & AWS & Serverless - Step to the next stage from a beginner

PyCon JP 2020 ( https://pycon.jp/2020/en/ ) の登壇資料

https://pycon.jp/2020/en/timetable/?id=203588

(2020/08/30 追記) Qiita 補足ブログ書きました https://qiita.com/hassaku_63/items/875ba4694538c6484526

hassaku63

August 28, 2020
Tweet

More Decks by hassaku63

Other Decks in Programming

Transcript

  1. Python × AWS × Serverless ॳֶऀ͕࣍ͷҰาΛ౿Έग़ͨ͢ΊͷςΫχοΫ Python & AWS &

    Serverless - Step to the next stage from a beginner PyCon JP 2020 #pyconjp_2 @hassaku
  2. ൃදͷલʹ - ͓͜ͱΘΓ - • ࠂ஌ࡁΈͷΞδΣϯμ͔Βएׯͷมߋ͕͋Γ·͢ʢݟղͷมԽʹ൐͏΋ͷʣ • ૝ఆಡऀ • AWS

    ʹ͍ͭͯɺଟগͷར༻ܦݧ͕͋Δ • ʮαʔόʔϨεʯͷ֓೦΍ɺྑ͞͸ͳΜͱͳ͘஌͍ͬͯΔ • Lambda Function ΛσϓϩΠͨ͜͠ͱ͕͋Δʗ֓೦Λཧղ͍ͯ͠Δ • ʮαʔόʔϨεʯೖ໳هࣄͷ࣍ʹԿΛͨ͠Βྑ͍ͷ͔Θ͔Βͳ͍ • Զͨͪ͸งғؾͰαʔόʔϨεΛ΍͍ͬͯΔ
  3. See also • [PyCon JP 2017] The theory of Serverless

    development by Python (ཧ࿦ ͔ΒֶͿPythonʹΑΔαʔόϨε։ൃ) @Masashi Terui
  4. Serverless Framework • OSSͷσϓϩΠπʔϧ • Infrastructure as Code (IaC) •

    ϚϧνΫϥ΢υରԠ • Serverless Frameworkͷ࢖͍ํ·ͱΊ https://www.serverless.com/
  5. Serverless Framework 1. ϓϩδΣΫτ࡞੒ 2. ίʔυΛॻ͘ (yaml, python) 3. σϓϩΠύοέʔδΛ࡞੒

    4. σϓϩΠ … sls create … sls deploy (or sls package) … sls deploy
  6. Serverless Framework ͷಠࣗ֓೦
 ˞σϓϩΠઌ؀ڥΛ۠ผ͢ΔͨΊͷ༻్ > sls deploy —stage dev >

    sls deploy —stage prod → “dev” ༻ͷ CloudFormation ελοΫ͕࡞੒͞ΕΔ → “prod” ༻ͷ CloudFormation ελοΫ͕࡞੒͞ΕΔ Note: εςʔδ
  7. ྫʣఆظδϣϒͷ࣮ߦ service dir lambda functions scheduled task step functions task

    utility libs jinja2 tempalte files tests ࣮૷ྫ: Qiita هࣄࣗಈߋ৽ https://github.com/hassaku63/qiita-autogenaration
  8. εςʔδ͝ͱʹ஋͕มΘΔ΋ͷ • Ϧιʔε໊ͷࢀরɹ • AWS Ϧιʔεͷ Resource Name, ARN •

    Endpoint (API Gateway, SQS Queue URL) • ΞϓϦέʔγϣϯݻ༗ͷઃఆ৘ใɹ • ϩάϨϕϧ • ར༻͢ΔAWSϦιʔεͷAPIࢀর༻ͷ໊લʢARN ΍ Ϧιʔε໊ɺURL ͳͲʣ • ઀ଓ͢Δ֎෦αʔϏεͷઃఆ (e.g Slack ͷ௨஌ઌνϟϯωϧʣ
  9. εςʔδ͝ͱʹ஋͕มΘΔ΋ͷ • Ϧιʔε໊ͷࢀর → ελοΫͷఆٛͰରॲ • AWS Ϧιʔεͷ Resource Name,

    ARN • Endpoint (API Gateway, SQS Queue URL) • ΞϓϦέʔγϣϯݻ༗ͷઃఆ৘ใ → ʢLambda ͷʣίʔυϕʔεͰରॲ • ϩάϨϕϧ • ར༻͢ΔAWSϦιʔεͷAPIࢀর༻ͷ໊લʢARN ΍ Ϧιʔε໊ɺURL ͳͲʣ • ઀ଓ͢Δ֎෦αʔϏεͷઃఆ (e.g Slack ͷ௨஌ઌνϟϯωϧʣ
  10. ؀ڥม਺ʹΑΔεςʔδݻ༗ม਺ͷ؅ཧ • python-dotenv • .env ϑΝΠϧΛѻ͏ύοέʔδ • serverless-dotenv-plugin • Serverless

    Framework ϓϥάΠϯ • .env.<stage> ϑΝΠϧͷએݴΛελοΫఆٛ಺ͰࢀরͰ͖Δ • ϦϦʔε໊ʢͷҰ෦͋Δ͍͸શ෦ʣͷએݴ • Lambda ؀ڥม਺ʹ౉͢ → ${provider.environment.XXX}
  11. جຊ • લड़ͷ͜ͱ͕Ͱ͖͍ͯΕ͹ɺ͋ͱ͸΄΅ʮςετ༰қੑʯͷ࿩ • Lambda handler ͷؔ਺ʹϕλॻ͖͗͢͠ͳ͍ • ϏδωεϩδοΫ΍֎෦αʔϏεݺͼग़͠͸ผϞδϡʔϧʹ෼཭ •

    ϋϯυϥ಺ͷ࢓ࣄ͸ event ͷύʔεͳͲɺ࠷௿ݶʹཹΊΔ • ϩʔΧϧίϯςφϥϯλΠϜͷ࢖༻΋ࣗવͱ΍Γ΍͘͢ͳΔ • e.g) SAM Local, DynamoDB Local, localstack
  12. JSONͰߏ଄Խ CloudWatch Logs Insights ͷݕࡧ͕ḿΔ
 (υοτ۠੾ΓͰωετͨ͠ϑΟʔϧυʹΞΫηεՄೳ) Amazon CloudWatch Logs -

    JSON ϩάͷϑΟʔϧυ https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html#CWL_AnalyzeLogData-discoverable-JSON-logs
  13. JSONͰߏ଄Խ ࣮૷ͷબ୒ࢶ 1. ϩΪϯά࣌ͷ msg Ҿ਺ʹ͸ඞͣ json.dump() ͔ͯ͠Β஋Λ౉͢ 2. JSON

    ϩΨʔΛαϙʔτ͢ΔύοέʔδΛ࢖͏ 3. ࣗલͰॻ͘ 1. Logger, Formatter Λܧঝ 2. Logging Adapter ͷར༻ʢݕ౼தʣ
  14. τϨʔε༻IDͷ෇༩ • جຊ͸ AWS X-Ray Λ࢖͓͚ͬͯ͹OK • τϨʔε༻IDʹ x-amzn-trace-id Λར༻Մೳ

    • Serverless Framework ͳΒ༗ޮԽ͸؆୯ • Step Functions Λ࢖͏ߏ੒ͩͱҰ೧Γඞཁ • Step Functions ͸ X-Ray ʹඇରԠʢ2020೥8݄ݱࡏʣ
  15. ʢࢀߟʣ৘ใݯ • AWS • AWS Ϋϥ΢υαʔϏε׆༻ࢿྉू: https://aws.amazon.com/jp/aws-jp-introduction/ • Webinar: https://aws.amazon.com/jp/about-aws/events/webinars/

    • Connpass: https://aws-serverless.connpass.com/ • Serverless community • Connpass: https://serverless.connpass.com/ • Serverless Operations • Blog archives: https://serverless.co.jp/blog-archives • Qiita: https://qiita.com/organizations/serverless-operations • Twitter • Yan Cui (@theburningmonk / AWS Serverless Hero): https://twitter.com/theburningmonk • Kensuke Shimokawa (@kensh /AWS Japan Serverless Specialist SA): https://twitter.com/_kensh • Keisuke Nishitani (@Keisuke69 / AWS Japan): https://twitter.com/Keisuke69 • Takahiro Horike (@horike37 / Serverless Operations): https://twitter.com/horike37