moto を 'もっと' 使おう/PyFukuoka#4

26c17d2f70a57a8fd9a70885682fcd74?s=47 Yohei Kawahara
April 27, 2018
2.3k

moto を 'もっと' 使おう/PyFukuoka#4

2018/04/27 PyFukuoka#4 でお話する予定の資料です.

26c17d2f70a57a8fd9a70885682fcd74?s=128

Yohei Kawahara

April 27, 2018
Tweet

Transcript

  1. moto Λ '΋ͬͱ' ࢖͓͏ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ PyFukuoka

    #4 @inokara PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 1
  2. ࢲ͸ • ઒ݪ ༸ฏ(͔ͬͺ) • iret גࣜձࣾ cloudpack ࣄۀ෦ •

    twitter : @inokara • Facebook : inokappa • Github : inokappa ࢓ࣄͰ͸ AWS ؀ڥͷߏங, ӡ༻΍ AWS CLI, AWS SDK Λ࢖ͬͨπʔϧΛ࡞ͬͨ Γ͍ͯ͠·͢. PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 2
  3. ຊࢿྉͰར༻ͨ͠؀ڥ $ python --version Python 3.6.4 $ pip freeze |

    egrep 'boto|moto' boto==2.48.0 boto3==1.7.7 botocore==1.10.7 moto==1.3.3 PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 3
  4. ϘΫ๊͕͍͑ͯͨ՝୊ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 4
  5. ΪϣʔϜͰ ! AWS SDK ΍ AWS CLI Λར༻ͯ͠, AWS ϦιʔεΛૢ࡞͢Δε

    ΫϦϓτ΍, Lambda ϑΝϯΫγϣϯΛॻ͘͜ͱ͕͋Γ·͕͢... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 5
  6. ՝୊1 ͪΐ͍ͪΐ͍σάϨ ! • ࣮૷΍վमͷաఔͰͪΐͪ͘ΐ͘σάϨ͢Δ • ςετΛॻ͖͍ͨ • AWS Ϧιʔεʹରͯ͠,

    ͲΜͳ෩ʹςετΛॻ͘ͷ? PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 6
  7. ՝୊2 ௚઀ AWS ϦιʔεΛୟ͘ ! • ҙਤ͠ͳ͍ϦιʔεΛ࡞ͬͯ՝͕ۚൃੜ ! • ͏͔ͬΓطଘϦιʔεͷ࡟আ

    (͜Ε͕Ұ൪ා͍) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 7
  8. Կ͔ྑ͍ํ๏͸ͳ͍΋ͷ͔... • SDK ͷϨεϙϯεΛ໛฿͢ΔΑ͏ͳπʔϧͬͯͳ͍ͷ͔ͳ • AWS CLI ͸ --endpoint ͱ͍͏Φϓγϣϯ΋͋Δ͠

    • ΍ͬͺΓ, ϢχοτςετΛॻ͖͍ͨʂ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 8
  9. moto PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 9
  10. moto ͱ͸ • https://github.com/spulec/moto • Boto3 ͷಈ࡞Λ mock Խ͢ΔϥΠϒϥϦ •

    Github ˒ : 1653 (2018/04/24 ࣌఺) • AWS ͷओཁͳαʔϏε͸໢ཏ͞Ε͍ͯΔ (ͱࢥ͏) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 10
  11. ೦ͷҝ, Boto3 ͱ͸ • https://github.com/boto/boto3 • ͳ͘ࢠ΋໧Δ AWS SDK for

    Python • Github ˒ : 3251 (2018/04/26 ࣌఺) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 11
  12. moto sample(1) unittest Ͱ࢖͏৔߹ PyFukuoka #4 | moto Λ '΋ͬͱ'

    ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 12
  13. unittest Ͱ࢖͏৔߹ (1) ςετؔ਺ʹ, ҎԼͷΑ͏ʹσίϨʔλΛ෇͚Δ͚ͩ1. @mock_s3 def test_put_object(self): s3 =

    boto3.resource('s3') s3.create_bucket(Bucket='my_bucket') sample.putObject('foobar', 'foo.txt') body = s3.Object('my_bucket', 'foo.txt').get()['Body'] self.assertDictEqual(body, 'foobar') 1 ͜ͷίʔυ͸͋͘·Ͱ΋αϯϓϧͰ͢. PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 13
  14. unittest Ͱ࢖͏৔߹ (2) ςετؔ਺ʹ, ҎԼͷΑ͏ʹσίϨʔλΛ෇͚Δ͚ͩ2. @mock_lambda def test_invoke_lambda(self): lmd =

    boto3.client('lambda', 'ap-northeast-1') lmd.create_function( FunctionName='sample', Runtime='python3.6', ... self.assertTrue(sample.invoke_lambda(json.dumps(data))) 2 ͜ͷίʔυ͸͋͘·Ͱ΋αϯϓϧͰ͢. PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 14
  15. unittest Ͱ࢖͏৔߹ (3) Boto3 ͷ֤ؔ਺͕ฦ͢ϨεϙϯεΛ moto ͕ݞ୅ΘΓ͢Δ͜ͱ Ͱ, ࣮ࡍͷϦιʔεʹΞΫηε͢Δ͜ͱͳ͘ςετ͕ग़དྷΔ. $

    python -m unittest sample.py -v test_put_object (sample.SampleTest) ... ok test_invoke_lambda (sample.SampleTest) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.201s OK PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 15
  16. moto sample(2) Standalone Server Mode PyFukuoka #4 | moto Λ

    '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 16
  17. Standalone Server Mode AWS ͷ API ϨεϙϯεΛฦ͢αʔόʔ͕ಉࠝ͞Ε͍ͯΔ3. $ moto_server ec2

    * Running on http://127.0.0.1:5000/ ... $ moto_server s3 --port=5001 * Running on http://127.0.0.1:5001/ ... 3 ࣮ࡍʹ͸ pip install moto[server] ͱͯ͠, ผ్ͰΠϯετʔϧ͢Δඞཁ͕͋Γ·͢. PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 17
  18. ͱ͍͏͜ͱ͸... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 18
  19. AWS CLI Ͱ΋ར༻Մೳ --endpoint-url ΦϓγϣϯͰΤϯυϙΠϯτΛࢦఆ͢Δ. $ aws \ > --profile

    mock_profile \ > --region us-west-2 \ > --endpoint-url http://127.0.0.1:5000 \ > ec2 describe-instances PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 19
  20. Ruby Ͱ΋ར༻Մೳ ҎԼ, Rspec ʹͯςετ࣮ߦલʹ moto_server Λىಈ͍ͯ͠Δ ྫ. describe 'MyEc2'

    do before(:all) do @output, @input = PTY.spawn("moto_server ec2") client = Aws::EC2::Client.new(region: 'us-west-2', endpoint: 'http://127.0.0.1:5000', profile: 'mock_profile') res = client.run_instances({image_id: 'ami-20d1c544', min_count: 3, max_count: 3}) ... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 20
  21. ศརʂʂʂ (͔΋) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~

    Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 21
  22. ͕࣌ؒ͋Ε͹ Demo PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~

    Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 22
  23. Ͱ΋ (But), σϞ • AWS CLI Λར༻ͨ͠γΣϧεΫϦϓτΛ shUnit2 Ͱςετ •

    S3 όέοτ͔Βऔಘͨ͠ΦϒδΣΫτͷ಺༰Λςετ͢Δ • moto Standalone Server Mode Λར༻͢Δ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 23
  24. ·ͱΊ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 24
  25. ·ͱΊ • moto ͸ boto3 ͷϨεϙϯεΛ mock Խ͢ΔϥΠϒϥϦ • Standalone

    Server Mode ΋ಉ͍ࠝͯ͠Δ • moto ͷݺͼग़͠͸, ςετؔ਺σίϨʔλΛఆٛ͢Δ͚ͩ • Standalone Server Mode ͸ଞͷݴޠ͔Β΋ར༻ՄೳͳͷͰ, εΫϦϓτͷಈ࡞֬ೝ͕ḿΓͦ͏ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 25
  26. localstack? PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 26
  27. ࣮͸ • localstack ͱ͍͏ AWS ؀ڥΛϩʔΧϧ؀ڥʹల։ग़དྷΔπʔ ϧ͕ OSS Ͱఏڙ͞Ε͍ͯΔ •

    https://github.com/localstack/localstack • ࢼͨ͜͠ͱ͸ແ͍͕͚Ͳ, μογϡϘʔυ΋උΘ͍ͬͯͯ, ৭ʑͱॆ࣮͍ͯͦ͠͏ͳงғؾ • ελʔ਺͚ͩΛൺֱ͢Δͱ, ѹ౗తʹ localstack ͚ͩͲ... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 27
  28. ࢖ͬͨ͜ͱ͸͋Γ·ͤΜ moto ͱൺֱ͢Δͱ, ϔϏʔͳؾ͕͍ͯ͠·͢. ࢖ͬͨࣄ͕͋Δํ, ࢖༻ײ౳Λڭ͑ͯ௖͚Δͱخ͍͠Ͱ͢ ! PyFukuoka #4 |

    moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 28
  29. ࢀߟ • https://github.com/spulec/moto • https://github.com/localstack/localstack • https://github.com/boto/boto3 PyFukuoka #4 |

    moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 29
  30. ͓ΘΓ! PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ.

    ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 30