$30 off During Our Annual Pro Sale. View details »

サーバーレスのテストを取り巻く環境

 サーバーレスのテストを取り巻く環境

サーバーレスに入門したので、
サーバーレスのテストを取り巻く開発環境についての考察を書いてみました

Yuji Takaesu

July 20, 2017
Tweet

More Decks by Yuji Takaesu

Other Decks in Technology

Transcript

  1. © Serverworks Co., Ltd. All right reserved. αʔόʔϨεʹೖ໳ͨ͠ͷͰɺ αʔόʔϨεͷςετΛऔΓר͘։ൃ؀ڥ 2017/7/14

    ͞͹ΘͷʮΘʯ#1 αʔόʔϨεςετೖ໳ʢԾʣ ٕज़4՝ ߴߐऱ ༞࣏ʢ͔ͨ͑͢ Ώ͏͡ʣ
  2. © Serverworks Co., Ltd. All right reserved. Who am I

    • גࣜձࣾαʔόʔϫʔΫε ٕज़4՝ • େࡕΦϑΟεɺೖࣾ3ϲ݄ͱͪΐͬͱ • ޷͖ͳݴޠ • Ruby • લ৬Ͱ͸RailsͰͷΞϓϦέʔγϣϯ։ൃΛओʹ΍͍ͬͯ·ͨ͠ • ޷͖ͳAWSαʔϏε • S3 • Twitter: @yusabana Facebook: yu.takaesu 2
  3. ຊ೔࿩͢͜ͱ •αʔόʔϨεͰ͸ͳ͍؀ڥͱ͸ʁ •αʔόʔϨεͱ͸ʁ •αʔόʔϨε։ൃͷςελϏϦςΟ •αʔόʔϨεςετ؀ڥͷ՝୊ •·ͱΊ 3

  4. ◆αʔόʔϨεͰ͸ͳ͍؀ڥͱ͸ʁ 4

  5. αʔόʔϨεͰ͸ͳ͍؀ڥྫ 5

  6. ։ൃͰ࢖༻͢Δπʔϧ • OSSͷϛυϧ΢ΣΞ • DB: MySQL, PostgreSQL • Cache: Redis,

    Memcached • Mail: Postfix • OSSͷϓϩάϥϛϯάݴޠɺϑϨʔϜϫʔΫ • PHP,Laravel • Ruby, Rails • nodejs • … etc 6
  7. Development؀ڥ •ϩʔΧϧͷPC؀ڥʹϛυϧ΢ΣΞ΋ؚΊͯߏங͢Δ •Vagrant ΍ Docker Λར༻ͯ͠Ծ૝؀ڥ্ʹߏங͢Δ • ͜͏͍͏هࣄΛॻ͖·ͨ͠ ։ൃ؀ڥ޲͚ϛυϧ΢ΣΞෳ਺όʔδϣϯ؅ཧͱͯ͠ͷDocker -

    Qiita http://qiita.com/yusabana/items/3267775fdf7249e33e1b 7
  8. Production؀ڥ •खॱॻ౳ΛΈͳ͕ΒखಈͰαʔόʔʹϩάΠϯͯ͠؀ڥߏங͢Δ •Chef΍AnsibleͳͲͷϓϩϏδϣχϯάπʔϧΛ༻͍ͯࣗಈߏஙͷ ࢓૊ΈΛ࡞ͬͯ؀ڥߏங͢Δ 8

  9. ؀ڥߏஙʹ͍ͭͯ •OSSϕʔεͷπʔϧͰ͋Ε͹ɺDockerͰ؀ڥΛ࡞͓͚ͬͯ͹ϙʔ λϏϦςΟੑ΋͋ΓɺґଘੑΛগͳ͘Dev΍Production؀ڥΛ༻ҙ Ͱ͖Δ •OSSͰ͸ͳ͍πʔϧͰ΋ϥΠηϯε͑͞क͍ͬͯΕ͹ϩʔΧϧͷ ؀ڥߏங΋Մೳ 9 ؤுΕ͹ී௨ʹ؀ڥߏங͸Ͱ͖Δ

  10. ՝୊ •Production؀ڥͷαʔόӡ༻͕େม •αʔόʔ΍ϛυϧ΢ΣΞͷՄ༻ੑ΍εέʔϥϒϧͳ؀ڥΛߏங ͢Δͷ͸೉͘͠ɺઐ໳ͷ஌͕ࣝඞཁͱͳΔ •σΟεΫͷރׇ •ϝϞϦϦʔΫ 10

  11. ◆αʔόʔϨεͱ͸ʁ 11

  12. AWS Lambdaͷ͜ͱʁʁ 12

  13. αʔόʔϨεఆٛʢ͜͜Ͱͷʣ •αʔόϨε͸৭ʑͳఆ͕ٛ͞Ε͍ͯΔ •֤ࣾͷαʔϏε͔Βݟ͑ΔServerlessͷຊ࣭ͱ͸Կ͔ http://marcy.hatenablog.com/entry/2016/04/01/152751 13 αʔόʔΛ؅ཧͤͣʹϚωʔδυͳαʔϏεΛ ར༻ͯ͠ΞϓϦέʔγϣϯΛ࣮ߦ͢Δ γεςϜͷશମɺ·ͨ͸Ұ෦

  14. 14 ग़య: https://d0.awsstatic.com/events/jp/2017/summit/slide/D2T1-2.pdf

  15. αʔόϨεͷϝϦοτ •࢖ͬͨ෼͚͔͓ͩۚ͠Λ෷Θͳ͍ʢ࣌ؒ՝ۚ͸ͳ͍ʣ •ӡ༻ͷෳࡶ͞Λղফ •εέʔϦϯά͸ϓϥοτϑΥʔϜଆͰ࣮૷ࡁΈ •Մ༻ੑʢΞϕΠϥϏϦςΟʣ΋ϓϥοτϑΥʔϜଆͰ࣮૷ࡁΈ •αʔϏεͷ࢖͍ํΛ֮͑Ε͹͙͢ʹར༻Մೳ 15

  16. αʔόʔϨεͷσϝϦοτ •֤ϚωʔδυαʔϏεʹ͸੍໿͕͋Δ •੍໿Λ௒͑ͨεέʔϥϏϦςΟ͸೉͍͠ ➡େ఍ͷαʔϏε͸ͦ͜·ͰεέʔϥϏϦςΟʹ໰୊͸ͳ͍ •ར༻͢ΔϚωʔδυαʔϏε͕૿Ճͯ͠ෳࡶ͕૿͢Մೳੑ͕͋Δ ʢϚΠΫϩαʔϏεΞʔΩςΫνϟʣ •API Gateway, Lambda, S3,

    DynamoDB, SQS …etc •ૄ݁߹͕Ώ͑ʹ࿈ܞ͢Δ෦෼ͳͲ࣮૷ʗςετ͕೉͍͠ 16
  17. ◆αʔόʔϨε։ൃͷςελϏϦςΟ 17

  18. ςετʹ͍ͭͯ࿩͍ͨ͜͠ͱ͸શͯॻ͔Ε͍ͯ·͢ʢಈը͋Γʣ 18 εϥΠυ: https://speakerdeck.com/twada/testable-lambda-working-effectively-with-legacy-lambda ಈը: https://www.youtube.com/watch?v=C0zNc4bdWhY

  19. ςετͱ͸ •खಈςετ •ΤΫηϧͳͲʹఆٛͨ͠ɺςετ࢓༷ॻΛϕʔεʹਓ͕ؒߦ͏ ςετ •ࣗಈςετ •ςετίʔυΛ΋ͱʹԿ͔͠Βͷػց͕ߦ͏ςετ 19 αʔόϨεʢϚωʔδυαʔϏεʣͰ΋ ࣗಈςετ͕͍ͨ͠!!

  20. ͱ͸͍͑՝୊͕ɺɺɺ DynamoDB ΍ S3 ΍ SQSͳͲͷϚωʔδυαʔϏεΛProductionͰ ར༻͍ͯ͠Δ͕ɺϩʔΧϧ͓Αͼςετ؀ڥͰ͸Ͳ͏͢Δ?? 20 ϚωʔδυαʔϏε͸ϩʔΧϧϚγϯ্Ͱ͸ ࣮ߦͰ͖ͳ͍

  21. ղܾҊ1 - ϚωʔδυαʔϏεΛར༻ͨ͠৔߹ͷςετͲ͏͢Δ͔ • ϩʔΧϧ(ςετ)؀ڥ͸ϩʔΧϧͰ࠶ݱՄೳͳ΋ͷͰ੾Γସ͑Δ • ྫʣϑΝΠϧͷΞοϓϩʔυͷ৔߹ɺProduction͸S3΁Ξοϓϩʔυ ͞ΕΔ͕ɺϩʔΧϧ(ςετ)؀ڥ͸ϩʔΧϧϚγϯ্ͷσΟϨΫτϦ ʹΞοϓϩʔυ͢Δ •

    ໰୊఺ • ϩʔΧϧͱຊ൪Ͱ࣮૷͕ҟͳͬͯ͠·͏ͷͰ༧ظͤ͵ෆ۩߹͕ൃੜ͢ Δ͓ͦΕ͕͋Δ 21
  22. ղܾҊ2 - ϚωʔδυαʔϏεΛར༻ͨ͠৔߹ͷςετͲ͏͢Δ͔ •ϩʔΧϧ։ൃ޲͚ͱςετ؀ڥ޲͚ͷAWS؀ڥΛ༻ҙ͢Δ •։ൃऀ͝ͱʹAWS؀ڥΛ༻ҙ •ڞ༗ͷ։ൃ؀ڥͱͯ͠AWS؀ڥΛ༻ҙ •໰୊఺ •AWS؀ڥͷIAMͷΞΧ΢ϯτͷ؅ཧ͕େมʹͳΔ •AWSͷར༻ྉ͕ۚൃੜ͢Δɺར༻ྉۚͷ؅ཧ 22

  23. FakeObjectͱ͍͏Ҋ •ղܾҊ1Λ֦ுͨ͠ײ͡ •ຊ෺ͱಉ͡Α͏ͳڍಈΛ͢Δ୯७ͳผ࣮૷ •͢΂ͯͷػೳΛຬ͍ͨͯ͠ΔΘ͚Ͱ͸ͳ͍ •ϩʔΧϧ؀ڥ্ͰϚωʔδυαʔϏεͷػೳ͕࠶ݱͰ͖ΔͷͰ؅ ཧ͢Δඞཁ͕ͳָͯ͘ •ϩʔΧϧͰMySQLαʔόΛ্ཱͪ͛Δͷͱಉ͡Α͏ͳײ͡ 23

  24. LocalStack 24

  25. LocalStackͷରԠαʔϏε 25

  26. LocalStackʹ͍ͭͯ • ಛఆͷϙʔτͰ֤छαʔϏεͷFakeͱͳΔαʔϏε͕ىಈ͢Δ • ΤϯυϙΠϯτΛAWS͔ΒLocalStackͰىಈͨ͠FakeαʔϏεͷΤϯυϙΠ ϯτ΁੾Γସ͑Δ͜ͱͰར༻Մೳ • ʮhttp://localhost:8080ʯʹΞΫηε͢ΔͱσϓϩΠ͍ͯ͠ΔLocalStackͷαʔ ϏεͷμογϡϘʔυ͕ར༻Ͱ͖Δ •

    ϚωδϝϯτίϯιʔϧͷΑ͏ͳUI͸ͳ͍ɻcli ΍ sdk Λ࢖ͬͯૢ࡞͢Δ • AWSΞΧ΢ϯτ͕ͳͯ͘΋؆୯ʹϩʔΧϧͰϚωʔδυαʔϏεΛࢼ͢͜ͱ ͕Ͱ͖Δ 26
  27. LocalStackͷΠϯετʔϧ • Πϯετʔϧํ๏͸READMEʹ • https://github.com/localstack/localstack#installing • DockerΛ࢖ͬͯىಈ͢Δͱ؆୯ʢDocker͸ΠϯετʔϧࡁΈΛ૝ఆʣ • ࣍ͷ2ͭͷίϚϯυΛଧ͚ͭͩ •

    ىಈͨ͠Β http://localhost:8080 ͰμογϡϘʔυʹΞΫηε 27 $ git clone https://github.com/localstack/localstack $ docker-compose up MacΛར༻͍ͯ͠Δ৔߹͸ҎԼͰىಈ͢Δ $ TMPDIR=/private$TMPDIR docker-compose up
  28. LocalStack্ͷS3Λaws-sdkͰ৮ͬͯΈΔ 28 [όέοτΛ࡞Δ] $ aws --endpoint-url=http://localhost:4572 s3 mb s3://mytest/ make_bucket:

    mytest [όέοτͷଘࡏ֬ೝ] $ aws --endpoint-url=http://localhost:4572 s3 ls 2006-02-04 01:45:09 mytest [ϑΝΠϧΛΞοϓϩʔυ] $ aws --endpoint-url=http://localhost:4572 s3 cp test.png s3://mytest upload: ./test.png to s3://mytest/test.png [ϑΝΠϧͷଘࡏ֬ೝ] $ aws --endpoint-url=http://localhost:4572 s3 ls s3://mytest 2017-07-18 21:12:10 0 test.png
  29. 29 μογϡϘʔυ http://localhost:8080

  30. ςετίʔυΛॻ͍ͯΈΑ͏ 30

  31. PythonΛ࢖ͬͨDynamoDB΁ΞΫηε͢Δ(ϓϩμΫγϣϯίʔυ) 31 import boto3 class Dynamodb(): def __init__(self, table_name): self.origindb

    = boto3.resource( 'dynamodb', region_name=‘ap-northeast-1', endpoint_url='http://localhost:4569' ) self.table = self.create_table(table_name) def create_table(self, table_name): table = self.origindb.create_table( #….লུ def put_item(self, item): return self.table.put_item(Item=item) # ଓ͘ # ଓ͖ def get_item(self, year, title): res = self.table.get_item( Key={ 'year': year, 'title': title, }, ) if 'Item' in res: return res['Item'] else: return None def drop_table(self): self.table.delete()
  32. PythonΛ࢖ͬͨDynamoDB΁ΞΫηε͢Δֶशςετ(ςετίʔυ) 32 import unittest import decimal from dynamodb import Dynamodb

    class TestDynamodbOps(unittest.TestCase): def setUp(self): self.db = Dynamodb('Movies') self.db.put_item( { 'year': 2017, 'title': 'The Movie', 'info': { 'rating': decimal.Decimal(0) } } ) def tearDown(self): self.db.drop_table() #ଓ͘ #ଓ͖ def test_valid_get_item(self): item = self.db.get_item(2017, 'The Movie') assert item is not None def test_invalid_get_item(self): item = self.db.get_item(2017, 'InvalidTitle’) assert item is None if __name__ == "__main__": unittest.main()
  33. ֶशςετΛॻ͍ͯΈͯ •ίʔυ͸ҎԼͷgithubʹ͓͍͍ͯ·͢ɻ • https://github.com/yusabana-sandbox/localstack-sample/tree/master/python ʢPython͕ෆ׳Εͳ΋ͷͰ…ʣ •LocalStackΛཱͯΔ͜ͱͰDynamoDBͱ͍͏AWSͷϚωʔδυ αʔϏεΛPython SDK(boto3)Λ؆୯ʹࢼ͢͜ͱ͕Ͱ͖ɺػೳΛς ετ͢Δͷʹద͍ͯ͠Δ •ςετΛॻ͘͜ͱͰ࣮ࡍʹಈ͔ͯ֬͠ೝͰ͖ΔͷͰ҆৺ײ͕͋Δ

    33
  34. ◆αʔόʔϨεςετ؀ڥͷ՝୊ 34

  35. αʔόʔϨεςετ؀ڥͷ՝୊ • ·ͩ·ͩൃల్্ • LocalStack΋2016೥11݄͘Β͍ʹ։ൃ͕࢝·ͬͨ͹͔Γ • όάͬΆ͍ڍಈ΋݁ߏ͋Δ • ೔ຊޠͳͲͷϚϧνόΠτจࣈؔ࿈Λ࢖͏ͱಈ͔ͳ͔ͬͨΓ… •

    ίϯτϦϏϡʔτνϟϯε • ඞཁʹԠͯ͡ςετίʔυ্Ͱ͸ϞοΫ΍ελϒΛ࢖͏ • ελϒɾϑΣΠΫɾϞοΫͱ͸ http://d.hatena.ne.jp/a-hisame/20090212/1234450327 35
  36. ◆·ͱΊ 36

  37. ·ͱΊ •αʔόʔϨεʢϚωʔδυʣαʔϏεΛར༻͢Δ৔߹Ͱ΋ࣗಈςετ ͕Ͱ͖Δͱ͍͍Ͱ͢Ͷ •FakeObjectʢLocalStackʣΛར༻ͯ͠ςετͰ͖Δͱָ •·ͩൃల్্ͳͱ͜Ζ΋͕͋ΔͷͰ՝୊͸ଟ͍ •t-wadaࢯͷAWSαϛοτ2017ͰͷಈըͱεϥΠυ͸͍͢͝Θ͔Γ΍ ͘͢ɺͱͯ΋ࢀߟʹͳΔ εϥΠυ: https://speakerdeck.com/twada/testable-lambda-working-effectively-with-legacy-lambda ಈը:

    https://www.youtube.com/watch?v=C0zNc4bdWhY 37
  38. t-wadaࢯͷݟղ 38 ग़య: https://speakerdeck.com/twada/testable-lambda-working-effectively-with-legacy-lambda

  39. © Serverworks Co., Ltd. All right reserved. ͋Γ͕ͱ͏͍͟͝·ͨ͠ ʢαʔόʔϫʔΫεͷେࡕͰ͸AWSΛ࢖ͬͯҰॹʹಇ͘ํΛืू͍ͯ͠·͢ʣ