Lambda + Apex 事例紹介 ~ 全ては AWS コスト削減のために ~ / Case Study of Lambda and Apex

Lambda + Apex 事例紹介 ~ 全ては AWS コスト削減のために ~ / Case Study of Lambda and Apex

Lambda + Apex 事例紹介
~ 全ては AWS コスト削減のために ~

3da5b00de1285b12a17d730262cc4824?s=128

Yoshiaki Yoshida

March 10, 2017
Tweet

Transcript

  1. -BNCEB "QFYࣄྫ঺հ dશͯ͸"84ίετ࡟ݮͷͨΊʹd !LBLBLBLBLLV ࣾ಺ษڧձ

  2. :PTIJBLJ:PTIJEB !LBLBLBLBLLV ‣ .BLVBLF$ZCFS"HFOU$SPXE'VOEJOH *OD ‣ 8FC0QFSBUJPOT&OHJOFFSٕ݉ज़޿ใ ‣ "84 3VCZ

    $FSUJpFE4DSVN.BTUFS ‣ ϓϩάϥϛϯάߨࢣͷ࢓ࣄΛ࢝Ί·ͨ͠ ‣ IUUQLBLBLBLBLLVIBUFOBCMPHDPN
  3. ʘ࠷ۙ$ZCFS"HFOU%FWFMPQFST#MPHʹهࣄग़ͯ͠·͢ʗ IUUQTEFWFMPQFSTDZCFSBHFOUDPKQCMPHBSDIJWFT IUUQTEFWFMPQFSTDZCFSBHFOUDPKQCMPHBSDIJWFT

  4. ʘϙουΩϟετσϏϡʔ͠·ͨ͠ʗ IUUQMFBOBHJMFGNFQJTPEF

  5. None
  6. "84-BNCEB ࢖ͬͯ·͔͢ʁ

  7. ։ൃ؀ڥͷ "84ίετ࡟ݮʹ࢖ͬͯ·͢

  8. ։ൃ؀ڥͷ "84ίετ࡟ݮʹ࢖ͬͯ·͢ ʁʁʁ

  9. "848FMM"SDIJUFDUFE'SBNFXPSL

  10. "848FMM"SDIJUFDUFE'SBNFXPSL $PTU0QUJNJ[BUJPO1JMMBS 5JNFCBTFEBQQSPBDI &YBNQMFTPGBUJNFCBTFEBQQSPBDIJODMVEFGPMMPXJOH UIFTVO UVSOJOHPGG%FWFMPQNFOUBOE5FTU JOTUBODFTPWFSUIFXFFLFOE GPMM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&WFOUT ϞχλϦϯά ͚ͩ͡Όͳ͍Αʁ

  20. εέδϡʔϦϯά࣮ߦ ‣ ୤+FOLJOT͍ͨ͠ ‣ "84$MPVE8BUDI&WFOUT ‣ DSPOࣜͰઃఆͨ͠εέδϡʔϦϯάͰ-BNCEBΛىಈͰ͖Δ ‣ ཁ஫ҙϙΠϯτ ‣

    DSPOࣜ ‣ 65$લఏ
  21. DSPOࣜ ෼࣌ؒ೔݄༵೔੡଄೥   .0/'3* ੡଄೥  ͸ΤϥʔʹͳΔ
 ยํΛ ʹ͢Δඞཁ͕͋Δ

  22. 65$લఏ ೔ຊ࣌ؒlՐਫ໦ۚ౔z TUPQ*OTUBODFT

  23. .PO 5VF 5IV 8FE 'SJ 4BU 4VO   

  24. 65$લఏ   .0/'3* ௚ײతʹཧղ͠ʹ͍͘ ೔ຊ࣌ؒlՐਫ໦ۚ౔z TUPQ*OTUBODFT

  25. -BNCEB $MPVE8BUDI &WFOUT DSPO࣮ߦ &$ &$ &$ TUBSUTUPQ ϝϯςφϯεPOPGG <࣮ߦϑϩʔ>

  26. "84-BNCEB Ͳ͏΍ͬͯ σϓϩΠ͢Δʁ

  27. -BNCEB $MPVE8BUDI &WFOUT DSPO࣮ߦ &$ &$ &$ TUBSUTUPQ ϝϯςφϯεPOPGG NFSHF

    IPPL BQFYEFQMPZ <࣮ߦϑϩʔ> <σϓϩΠϑϩʔ>
  28. None
  29. "QFY ‣ -BNCEB؅ཧπʔϧʢϏϧυˍσϓϩΠʣ ‣ ؔ࿈4FSWFSMFTT'SBNFXPSL -BNWFSZ "844".ͳͲ ‣ -BNCEBʹಛԽ͍ͯ͠ΔͨΊඇৗʹബ࣮͘૷͞Ε͍ͯΔ ‣

    υΩϡϝϯτ΋ॆ࣮͍ͯ͠Δ ‣ 5FSSBGPSN࿈ܞ ‣ ABQFYJOGSBAίϚϯυ͸AUFSSBGPSNAίϚϯυͷϥούʔ
  30. "QFYίϚϯυ঺հ ‣ BQFYJOJUॳظԽ͢Δʢ*".3PMFͱ*".1PMJDZͷࣗಈੜ੒΋ؚΉʣ ‣ BQFYEFQMPZ-BNCEB'VODUJPOΛσϓϩΠ͢Δ ‣ BQFYJOWPLF-BNCEB'VODUJPOΛ࣮ߦ͢Δ ‣ BQFYMPHT G

    $MPVE8BUDI-PHTΛ֬ೝ͢Δ ‣ BQFYEPDTυΩϡϝϯτΛࢀর͢Δ
  31. { "name": "project-name", "description": "project-name", "memory": 128, "timeout": 300, "role":

    "", "environment": { "DEBUG_SHIM": "true" } } "QFYઃఆϑΝΠϧ project.json -BNCEB'VODUJPOͷϝϞϦͱλΠϜΞ΢τ΋ઃఆͰ͖Δ
  32. "QFYઃఆϑΝΠϧ { "name": "project-name", "description": "project-name", "memory": 128, "timeout": 300,

    "role": "", "environment": { "DEBUG_SHIM": "true" } "hooks": { "build": "cp ../../package.json . && npm install" } } project.json OQNϥΠϒϥϦΛؚΊΔ͜ͱ΋Ͱ͖Δ
  33. $JSDMF$*͔ΒσϓϩΠ͢Δ machine: timezone: Asia/Tokyo dependencies: post: - curl https://raw.githubusercontent.com/apex/ apex/master/install.sh

    | sudo sh deployment: master: branch: master commands: - apex deploy test: pre: - npm install eslint --save-dev override: - ./node_modules/.bin/eslint . circle.yml NBTUFSʹϚʔδ͞ΕͨΒ ࣗಈతʹσϓϩΠ͢Δ &4-JOUΛ ࣮ߦ͢Δ
  34. -BNCEB $MPVE8BUDI &WFOUT DSPO࣮ߦ &$ &$ &$ TUBSUTUPQ ϝϯςφϯεPOPGG NFSHF

    IPPL BQFYEFQMPZ <࣮ߦϑϩʔ> <σϓϩΠϑϩʔ>
  35. ؾ෇͖·ͨ͠ʁ

  36. -BNCEB $MPVE8BUDI &WFOUT DSPO࣮ߦ &$ &$ &$ TUBSUTUPQ ϝϯςφϯεPOPGG NFSHF

    IPPL BQFYEFQMPZ <࣮ߦϑϩʔ> <σϓϩΠϑϩʔ> 4FSWFSMFTT "SDIJUFDUVSF
  37. ΋ͬͱৄ͘͠ʂ

  38. BQFYBQFY

  39. BQFYBQFY

  40. BQFYBQFY

  41. XJUI(PTVQQPSU

  42. -BNCEBαϙʔτݴޠ ‣ /PEFKT ‣ +BWB ‣ 1ZUIPO ‣ $ SF*OWFOUͰ

    3VCZͱ(PMBOHαϙʔτͷ ൃද͕ແͯ͘ ΨοΧϦͨ͠ਓ΋ଟ͍Ͱ͠ΐ͏
  43. JOEFYKTΛಡΜͩ

  44. var child = require('child_process') var byline = require('./byline') /** *

    Callback for the request. */ var callback /** * Child process for binary I/O. */ var proc = child.spawn('./main', { stdio: ['pipe', 'pipe', process.stderr] }) proc.on('error', function(err){ console.error('[shim] error: %s', err) process.exit(1) }) proc.on('exit', function(code, signal){ console.error('[shim] exit: code=%s signal=%s', code, signal) process.exit(1) }) index.js ࢠϓϩηεͰ (P NBJO Λಈ͔ͯ͠ ϓϩηεؒ௨৴Λ͢Δ ඪ४ೖྗ TUEJO  ඪ४ग़ྗ TUEPVU  ඪ४Τϥʔग़ྗ TUEFSS
  45. /** * Newline-delimited JSON stdout. */ var out = byline(proc.stdout)

    out.on('data', function(line){ if (process.env.DEBUG_SHIM) console.log('[shim] parsing: `%s`', line) var msg = JSON.parse(line) callback(msg.error, msg.value) }) /** * Handle events. */ exports.handle = function(event, ctx, cb) { callback = cb ctx.callbackWaitsForEmptyEventLoop = false proc.stdin.write(JSON.stringify({ "event": event, "context": ctx })+'\n'); } index.js (Pͷඪ४ग़ྗΛฦ͢ \ ʢதུʣ FOWJSPONFOU\ %&#6(@4)*.USVF ^ ^ ։ൃத͸σόοάϞʔυ ʹ͓ͯ͘͠ͱศར
  46. ؆ུԽ͢Δͱ

  47. JOEFYKT BQFYTIJN NBJOHP 45%*/ 45%065 JOWPLF DIJME@QSPDFTTTQBXO 45%&33

  48. Ͳ͏ͯ͠΋-BNCEBͰ (PΛಈ͔͍ͨ͠ͳΒ࢖͑Δ͔΋ʁ ʢެࣜαϙʔτΛ଴ͬͯ΋ྑͦ͞͏ʣ

  49. "845JQT঺հ ࣌ؒ༨ͬͨΒ

  50. BXTMPHT $MPVE8BUDI-PHTΛ؆୯ʹॲཧͰ͖Δ$-* ʢϑΟϧλ ΢Υον ೔࣌ࢦఆͳͲʣ

  51. HJUTFDSFUT *".ΫϨσϯγϟϧͷޡίϛοτΛ๷ࢭ͢Δπʔϧ ʢҰݴͰݴ͑͹HJUIPPLTʣ

  52. SPBEXPSLFS 3PVUFͷઃఆΛ*OGSBTUSVDUVSFBT$PEFԽ͢Δ ʢݸਓυϝΠϯͷ؅ཧʹ࢖ͬͯΔʣ

  53. QJDVMFU ηΩϡϦςΟάϧʔϓΛ*OGSBTUSVDUVSFBT$PEFԽ͢Δ

  54. ·ͱΊ

  55. -BNCEB "QFYศརʂ