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

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

Yohei Kawahara
April 27, 2018
4.4k

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

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

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. ΪϣʔϜͰ ! AWS SDK ΍ AWS CLI Λར༻ͯ͠, AWS ϦιʔεΛૢ࡞͢Δε

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

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

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

    • ΍ͬͺΓ, ϢχοτςετΛॻ͖͍ͨʂ PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 8
  8. moto ͱ͸ • https://github.com/spulec/moto • Boto3 ͷಈ࡞Λ mock Խ͢ΔϥΠϒϥϦ •

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

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

    ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 12
  11. 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
  12. 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
  13. 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
  14. moto sample(2) Standalone Server Mode PyFukuoka #4 | moto Λ

    '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 16
  15. 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
  16. 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
  17. 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
  18. ศརʂʂʂ (͔΋) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~

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

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

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

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

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

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

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