Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

Yoshiaki Yoshida

March 10, 2017
Tweet

More Decks by Yoshiaki Yoshida

Other Decks in Technology

Transcript

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

    $FSUJpFE4DSVN.BTUFS ‣ ϓϩάϥϛϯάߨࢣͷ࢓ࣄΛ࢝Ί·ͨ͠ ‣ IUUQLBLBLBLBLLVIBUFOBCMPHDPN
  2. .PO 5VF 5IV 8FE 'SJ 4BU 4VO   

    *OTUBODFIPVSTXFFL ࡟ݮͯ͠·͢
  3. ΠϯελϯεΛૢ࡞͢Δ ‣ "844%,GPS+BWB4DSJQU ‣ TUBSU*OTUBODFT QBSBNT\^ DBMMCBDL  ‣ TUPQ*OTUBODFT

    QBSBNT\^ DBMMCBDL  ‣ -BNCEB'VODUJPOͰΠϕϯτ͝ͱʹॲཧΛ෼ذ͍ͯ͠Δ ‣ \BDUJPOlTUBSU^ˠΠϯελϯεىಈ ‣ \BDUJPOlTUPQ^ˠΠϯελϯεఀࢭ QBSBNTʹ*OTUBODF*ETΛઃఆͯ͠ ෳ਺ΠϯελϯεΛૢ࡞͢Δ
  4. { "name": "project-name", "description": "project-name", "memory": 128, "timeout": 300, "role":

    "", "environment": { "DEBUG_SHIM": "true" } } "QFYઃఆϑΝΠϧ project.json -BNCEB'VODUJPOͷϝϞϦͱλΠϜΞ΢τ΋ઃఆͰ͖Δ
  5. "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ϥΠϒϥϦΛؚΊΔ͜ͱ΋Ͱ͖Δ
  6. $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Λ ࣮ߦ͢Δ
  7. -BNCEB $MPVE8BUDI &WFOUT DSPO࣮ߦ &$ &$ &$ TUBSUTUPQ ϝϯςφϯεPOPGG NFSHF

    IPPL BQFYEFQMPZ <࣮ߦϑϩʔ> <σϓϩΠϑϩʔ> 4FSWFSMFTT "SDIJUFDUVSF
  8. -BNCEBαϙʔτݴޠ ‣ /PEFKT ‣ +BWB ‣ 1ZUIPO ‣ $ SF*OWFOUͰ

    3VCZͱ(PMBOHαϙʔτͷ ൃද͕ແͯ͘ ΨοΧϦͨ͠ਓ΋ଟ͍Ͱ͠ΐ͏
  9. 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
  10. /** * 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 ^ ^ ։ൃத͸σόοάϞʔυ ʹ͓ͯ͘͠ͱศར