Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
moto を 'もっと' 使おう/PyFukuoka#4
Search
Yohei Kawahara
April 27, 2018
0
4.5k
moto を 'もっと' 使おう/PyFukuoka#4
2018/04/27 PyFukuoka#4 でお話する予定の資料です.
Yohei Kawahara
April 27, 2018
Tweet
Share
More Decks by Yohei Kawahara
See All by Yohei Kawahara
20191002_CircleCIMeetupFukuoka#2
inokappa
1
820
kix_009_20190429
inokappa
0
820
2019-04-18 CircleCI Meetup Fukuoka #1
inokappa
2
8k
開発で使える AWS の API 操作を模倣する (OSS) ツールを探してみた/JAWS-UG Kagoshima Vol.8
inokappa
1
1.2k
隙間時間を使って exercism.io を始め隊 | リモートで勉強会 #2
inokappa
0
410
Congratulations fukuokarb#100
inokappa
0
3.9k
github-enterprise-user-kai-desuyone
inokappa
0
100
Elixir and AWS Tutorial
inokappa
0
550
AWS Step Functions で検討する crontabless architecture/20170824_serverless_meetup_fukuoka#1
inokappa
1
750
Featured
See All Featured
Visualization
eitanlees
146
15k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Making Projects Easy
brettharned
116
5.9k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Building an army of robots
kneath
302
44k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Writing Fast Ruby
sferik
628
61k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
What's in a price? How to price your products and services
michaelherold
243
12k
GraphQLとの向き合い方2022年版
quramy
44
13k
The Cult of Friendly URLs
andyhume
78
6.1k
Transcript
moto Λ 'ͬͱ' ͓͏ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ PyFukuoka
#4 @inokara PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 1
ࢲ • ݪ ༸ฏ(͔ͬͺ) • iret גࣜձࣾ cloudpack ࣄۀ෦ •
twitter : @inokara • Facebook : inokappa • Github : inokappa ࣄͰ AWS ڥͷߏங, ӡ༻ AWS CLI, AWS SDK ΛͬͨπʔϧΛ࡞ͬͨ Γ͍ͯ͠·͢. PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 2
ຊࢿྉͰར༻ͨ͠ڥ $ 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
ϘΫ๊͕͍͑ͯͨ՝ PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 4
ΪϣʔϜͰ ! AWS SDK AWS CLI Λར༻ͯ͠, AWS ϦιʔεΛૢ࡞͢Δε
ΫϦϓτ, Lambda ϑΝϯΫγϣϯΛॻ͘͜ͱ͕͋Γ·͕͢... PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 5
՝1 ͪΐ͍ͪΐ͍σάϨ ! • ࣮վमͷաఔͰͪΐͪ͘ΐ͘σάϨ͢Δ • ςετΛॻ͖͍ͨ • AWS Ϧιʔεʹରͯ͠,
ͲΜͳ෩ʹςετΛॻ͘ͷ? PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 6
՝2 AWS ϦιʔεΛୟ͘ ! • ҙਤ͠ͳ͍ϦιʔεΛ࡞ͬͯ՝͕ۚൃੜ ! • ͏͔ͬΓطଘϦιʔεͷআ
(͜Ε͕Ұ൪ා͍) PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 7
Կ͔ྑ͍ํ๏ͳ͍ͷ͔... • SDK ͷϨεϙϯεΛ฿͢ΔΑ͏ͳπʔϧͬͯͳ͍ͷ͔ͳ • AWS CLI --endpoint ͱ͍͏Φϓγϣϯ͋Δ͠
• ͬͺΓ, ϢχοτςετΛॻ͖͍ͨʂ PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 8
moto PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 9
moto ͱ • https://github.com/spulec/moto • Boto3 ͷಈ࡞Λ mock Խ͢ΔϥΠϒϥϦ •
Github ˒ : 1653 (2018/04/24 ࣌) • AWS ͷओཁͳαʔϏεཏ͞Ε͍ͯΔ (ͱࢥ͏) PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 10
೦ͷҝ, Boto3 ͱ • https://github.com/boto/boto3 • ͳ͘ࢠΔ AWS SDK for
Python • Github ˒ : 3251 (2018/04/26 ࣌) PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 11
moto sample(1) unittest Ͱ͏߹ PyFukuoka #4 | moto Λ 'ͬͱ'
͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 12
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
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
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
moto sample(2) Standalone Server Mode PyFukuoka #4 | moto Λ
'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 16
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
ͱ͍͏͜ͱ... PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 18
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
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
ศརʂʂʂ (͔) PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~
Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 21
͕࣌ؒ͋Ε Demo PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~
Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 22
Ͱ (But), σϞ • AWS CLI Λར༻ͨ͠γΣϧεΫϦϓτΛ shUnit2 Ͱςετ •
S3 όέοτ͔Βऔಘͨ͠ΦϒδΣΫτͷ༰Λςετ͢Δ • moto Standalone Server Mode Λར༻͢Δ PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 23
·ͱΊ PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 24
·ͱΊ • moto boto3 ͷϨεϙϯεΛ mock Խ͢ΔϥΠϒϥϦ • Standalone
Server Mode ಉ͍ࠝͯ͠Δ • moto ͷݺͼग़͠, ςετؔσίϨʔλΛఆٛ͢Δ͚ͩ • Standalone Server Mode ଞͷݴޠ͔Βར༻ՄೳͳͷͰ, εΫϦϓτͷಈ࡞֬ೝ͕ḿΓͦ͏ PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 25
localstack? PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 26
࣮ • localstack ͱ͍͏ AWS ڥΛϩʔΧϧڥʹల։ग़དྷΔπʔ ϧ͕ OSS Ͱఏڙ͞Ε͍ͯΔ •
https://github.com/localstack/localstack • ࢼͨ͜͠ͱແ͍͕͚Ͳ, μογϡϘʔυඋΘ͍ͬͯͯ, ৭ʑͱॆ࣮͍ͯͦ͠͏ͳงғؾ • ελʔ͚ͩΛൺֱ͢Δͱ, ѹతʹ localstack ͚ͩͲ... PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 27
ͬͨ͜ͱ͋Γ·ͤΜ moto ͱൺֱ͢Δͱ, ϔϏʔͳؾ͕͍ͯ͠·͢. ͬͨࣄ͕͋Δํ, ༻ײΛڭ͚͑ͯΔͱخ͍͠Ͱ͢ ! PyFukuoka #4 |
moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 28
ࢀߟ • https://github.com/spulec/moto • https://github.com/localstack/localstack • https://github.com/boto/boto3 PyFukuoka #4 |
moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ. ͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 29
͓ΘΓ! PyFukuoka #4 | moto Λ 'ͬͱ' ͓͏ ~ Πϯϑϥ୲ͰςετΛ.
͜ͷࢥ͍, ఱਆͷӉ (ͦΒ) ʹڹ͚ ~ 30