Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

ࢲ͸ • ઒ݪ ༸ฏ(͔ͬͺ) • iret גࣜձࣾ cloudpack ࣄۀ෦ • twitter : @inokara • Facebook : inokappa • Github : inokappa ࢓ࣄͰ͸ AWS ؀ڥͷߏங, ӡ༻΍ AWS CLI, AWS SDK Λ࢖ͬͨπʔϧΛ࡞ͬͨ Γ͍ͯ͠·͢. PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 2

Slide 3

Slide 3 text

ຊࢿྉͰར༻ͨ͠؀ڥ $ 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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

ΪϣʔϜͰ ! AWS SDK ΍ AWS CLI Λར༻ͯ͠, AWS ϦιʔεΛૢ࡞͢Δε ΫϦϓτ΍, Lambda ϑΝϯΫγϣϯΛॻ͘͜ͱ͕͋Γ·͕͢... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 5

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

moto ͱ͸ • https://github.com/spulec/moto • Boto3 ͷಈ࡞Λ mock Խ͢ΔϥΠϒϥϦ • Github ˒ : 1653 (2018/04/24 ࣌఺) • AWS ͷओཁͳαʔϏε͸໢ཏ͞Ε͍ͯΔ (ͱࢥ͏) PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

moto sample(2) Standalone Server Mode PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 16

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

࣮͸ • localstack ͱ͍͏ AWS ؀ڥΛϩʔΧϧ؀ڥʹల։ग़དྷΔπʔ ϧ͕ OSS Ͱఏڙ͞Ε͍ͯΔ • https://github.com/localstack/localstack • ࢼͨ͜͠ͱ͸ແ͍͕͚Ͳ, μογϡϘʔυ΋උΘ͍ͬͯͯ, ৭ʑͱॆ࣮͍ͯͦ͠͏ͳงғؾ • ελʔ਺͚ͩΛൺֱ͢Δͱ, ѹ౗తʹ localstack ͚ͩͲ... PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 27

Slide 28

Slide 28 text

࢖ͬͨ͜ͱ͸͋Γ·ͤΜ moto ͱൺֱ͢Δͱ, ϔϏʔͳؾ͕͍ͯ͠·͢. ࢖ͬͨࣄ͕͋Δํ, ࢖༻ײ౳Λڭ͑ͯ௖͚Δͱخ͍͠Ͱ͢ ! PyFukuoka #4 | moto Λ '΋ͬͱ' ࢖͓͏ ~ Πϯϑϥ୲౰Ͱ΋ςετΛ. ͜ͷࢥ͍, ఱਆͷӉ஦ (ͦΒ) ʹڹ͚ ~ 28

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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