Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
moto を 'もっと' 使おう/PyFukuoka#4
Search
Yohei Kawahara
April 27, 2018
4.9k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
moto を 'もっと' 使おう/PyFukuoka#4
2018/04/27 PyFukuoka#4 でお話する予定の資料です.
Yohei Kawahara
April 27, 2018
More Decks by Yohei Kawahara
See All by Yohei Kawahara
20191002_CircleCIMeetupFukuoka#2
inokappa
1
950
kix_009_20190429
inokappa
0
950
2019-04-18 CircleCI Meetup Fukuoka #1
inokappa
2
9.5k
開発で使える AWS の API 操作を模倣する (OSS) ツールを探してみた/JAWS-UG Kagoshima Vol.8
inokappa
1
1.4k
隙間時間を使って exercism.io を始め隊 | リモートで勉強会 #2
inokappa
0
570
Congratulations fukuokarb#100
inokappa
0
4.2k
github-enterprise-user-kai-desuyone
inokappa
0
160
Elixir and AWS Tutorial
inokappa
0
650
AWS Step Functions で検討する crontabless architecture/20170824_serverless_meetup_fukuoka#1
inokappa
1
820
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
420
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
500
Marketing to machines
jonoalderson
1
5.8k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
570
Heart Work Chapter 1 - Part 1
lfama
PRO
10
36k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
360
Designing Experiences People Love
moore
143
24k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
550
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
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